Skip to content

Commit

Permalink
Validate content value (#371)
Browse files Browse the repository at this point in the history
* schema requires specific data.content values

* add inplace_volumes to allowed contents

* test that ALLOWED_CONTENTS is in synch with schema.
  • Loading branch information
perolavsvendsen authored Sep 28, 2023
1 parent 38ac2dc commit 8bd0308
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion schema/definitions/0.8.0/examples/table_wellpicks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ file:

data: # The data block describes the actual data (e.g. surface). Only present in data objects

content: well_picks # white-listed and standardized
content: wellpicks # white-listed and standardized

name: wellpicks
stratigraphic: false
Expand Down
25 changes: 24 additions & 1 deletion schema/definitions/0.8.0/schema/fmu_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,31 @@
"content": {
"type": "string",
"description": "The contents of this data object",
"enum": [
"depth",
"time",
"thickness",
"property",
"seismic",
"fluid_contact",
"field_outline",
"field_region",
"regions",
"pinchout",
"subcrop",
"fault_lines",
"velocity",
"volumes",
"volumetrics",
"inplace_volumes",
"khproduct",
"timeseries",
"wellpicks",
"parameters"
],
"examples": [
"depth"
"depth",
"time"
]
},
"tagname": {
Expand Down
1 change: 1 addition & 0 deletions src/fmu/dataio/_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def __post_init__(self):
"velocity": None,
"volumes": None,
"volumetrics": None, # or?
"inplace_volumes": None, # or?
"khproduct": None,
"timeseries": None,
"wellpicks": None,
Expand Down
31 changes: 31 additions & 0 deletions tests/test_schema/test_schema_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pytest

from fmu.dataio._definitions import ALLOWED_CONTENTS

# pylint: disable=no-member

Expand Down Expand Up @@ -383,3 +384,33 @@ def test_schema_logic_data_spec(schema_080, metadata_examples):

# assert data.spec not required when class === dictionary
jsonschema.validate(instance=example_dict, schema=schema_080)


def test_schema_logic_content_whitelist(schema_080, metadata_examples):
"""Test that validation fails when value of data.content is not in
the whitelist."""

# fetch surface example
example_surface = deepcopy(metadata_examples["surface_depth.yml"])

# assert validation with no changes
jsonschema.validate(instance=example_surface, schema=schema_080)

# shall fail when content is not in whitelist
example_surface["data"]["content"] = "not_valid_content"
with pytest.raises(jsonschema.exceptions.ValidationError):
jsonschema.validate(instance=example_surface, schema=schema_080)


def test_schema_content_synch_with_code(schema_080):
"""Currently, the whitelist for content is maintained both in the schema
and in the code. This test asserts that list used in the code is in synch
with schema. Note! This is one-way, and will not fail if additional
elements are added to the schema only."""

schema_allowed = schema_080["definitions"]["data"]["properties"]["content"]["enum"]
for allowed_content in ALLOWED_CONTENTS:
if allowed_content not in schema_allowed:
raise ValueError(
f"content '{allowed_content}' allowed in code, but not schema."
)

0 comments on commit 8bd0308

Please sign in to comment.