Skip to content

Commit

Permalink
when calling Datamodel.validate use a schema if available (#140)
Browse files Browse the repository at this point in the history
* add test for validate when datamodel modified as dict

adds a minimal case for RCAL-538

* have validate use a schema if available

fixes RCAL-538
  • Loading branch information
braingram committed Mar 29, 2023
1 parent 36b556e commit cadbc4f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

- update minimum version of ``numpy`` to ``1.20`` and add minimum dependency testing to CI [#114]

- Use available tag schema if available during datamodels.validate [#140]

0.14.1 (2023-01-31)
===================

Expand Down
7 changes: 5 additions & 2 deletions src/roman_datamodels/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def value_change(value, pass_invalid_values, strict_validation):
if pass_invalid_values:
update = True
if strict_validation:
raise jsonschema.ValidationError(errmsg)
raise errmsg
else:
warnings.warn(errmsg, ValidationWarning)
return update
Expand Down Expand Up @@ -61,7 +61,10 @@ def _check_value(value):

validator_context = AsdfFile()

temp_schema = {"$schema": "http://stsci.edu/schemas/asdf-schema/0.1.0/asdf-schema"}
if hasattr(value, "_schema"):
temp_schema = value._schema()
else:
temp_schema = {"$schema": "http://stsci.edu/schemas/asdf-schema/0.1.0/asdf-schema"}
validator = asdf_schema.get_validator(temp_schema, validator_context, validators=validator_callbacks)

value = yamlutil.custom_tree_to_tagged_tree(value, validator_context)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,3 +750,17 @@ def test_crds_parameters(tmp_path):

crds_pars = ramp.get_crds_parameters()
assert "roman.meta.exposure.start_time" in crds_pars


def test_model_validate_without_save():
# regression test for rcal-538
img = utils.mk_level1_science_raw()
m = datamodels.ImageModel(img)

# invalidate pointing without using the
# data model/node api to avoid a validation
# failure here
m.meta["pointing"] = {}

with pytest.raises(ValidationError):
m.validate()

0 comments on commit cadbc4f

Please sign in to comment.