Validation by Schema¶
python-jsonschema is the library to ingest the associated JSON Schema at runtime and validate inputs.
In [1]:
Copied!
schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://eoap.github.io/cwl2ogc/array-inputs/inputs.yaml",
"description": "The schema to represent a array-inputs inputs definition",
"type": "object",
"required": ["filesA", "filesB", "filesC"],
"properties": {
"filesA": {"$ref": "#/$defs/filesA"},
"filesB": {"$ref": "#/$defs/filesB"},
"filesC": {"$ref": "#/$defs/filesC"},
},
"additionalProperties": False,
"$defs": {
"filesA": {"type": "array", "items": {"type": "string"}},
"filesB": {"type": "array", "items": {"type": "string"}},
"filesC": {"type": "array", "items": {"type": "string"}},
},
}
schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://eoap.github.io/cwl2ogc/array-inputs/inputs.yaml",
"description": "The schema to represent a array-inputs inputs definition",
"type": "object",
"required": ["filesA", "filesB", "filesC"],
"properties": {
"filesA": {"$ref": "#/$defs/filesA"},
"filesB": {"$ref": "#/$defs/filesB"},
"filesC": {"$ref": "#/$defs/filesC"},
},
"additionalProperties": False,
"$defs": {
"filesA": {"type": "array", "items": {"type": "string"}},
"filesB": {"type": "array", "items": {"type": "string"}},
"filesC": {"type": "array", "items": {"type": "string"}},
},
}
1. Parse the JSON Schema¶
In [2]:
Copied!
from assertions_mate.jsonschema_validator import JSONSchemaValidator
from assertions_mate.error_models import ErrorDetail, ServerError
from jsonschema.exceptions import SchemaError
import json
validator = None
try:
validator = JSONSchemaValidator(schema)
except SchemaError as schema_error:
print(
json.dumps(
ServerError(errors=[ErrorDetail(detail=schema_error.message)]), indent=2
)
)
from assertions_mate.jsonschema_validator import JSONSchemaValidator
from assertions_mate.error_models import ErrorDetail, ServerError
from jsonschema.exceptions import SchemaError
import json
validator = None
try:
validator = JSONSchemaValidator(schema)
except SchemaError as schema_error:
print(
json.dumps(
ServerError(errors=[ErrorDetail(detail=schema_error.message)]), indent=2
)
)
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) Cell In[2], line 1 ----> 1 from assertions_mate.jsonschema_validator import JSONSchemaValidator 2 from assertions_mate.error_models import ErrorDetail, ServerError 3 from jsonschema.exceptions import SchemaError 4 import json ModuleNotFoundError: No module named 'assertions_mate'
2. Define your inputs set¶
In [3]:
Copied!
inputs = {
"aoi": "-118.985,38.432,-118.183,38.938",
"filesB": "EPSG:4326",
"bands": ["green", "nir08"],
"item": {
"class": "https://raw.githubusercontent.com/eoap/schemas/main/url.yaml#URL",
"value": "https://planetarycomputer.microsoft.com/api/stac/v1/collections/landsat-c2-l2/items/LC08_L2SP_042033_20231007_02_T1",
},
}
inputs = {
"aoi": "-118.985,38.432,-118.183,38.938",
"filesB": "EPSG:4326",
"bands": ["green", "nir08"],
"item": {
"class": "https://raw.githubusercontent.com/eoap/schemas/main/url.yaml#URL",
"value": "https://planetarycomputer.microsoft.com/api/stac/v1/collections/landsat-c2-l2/items/LC08_L2SP_042033_20231007_02_T1",
},
}
3. Validate the inputs and report the errors¶
In [4]:
Copied!
if validator:
error = validator.validate_inputs(inputs)
if error:
print(error.model_dump_json(indent=2, exclude_none=True))
else:
# No violations — choose what to output. A simple success marker is fine:
print("Go ahead on submitting the request to the next level validation")
if validator:
error = validator.validate_inputs(inputs)
if error:
print(error.model_dump_json(indent=2, exclude_none=True))
else:
# No violations — choose what to output. A simple success marker is fine:
print("Go ahead on submitting the request to the next level validation")
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[4], line 1 ----> 1 if validator: 2 error = validator.validate_inputs(inputs) 3 4 if error: NameError: name 'validator' is not defined
In [ ]:
Copied!