Logo
  • Home

Tutorials

  • First Validated Workflow

How-to Guides

  • Validation by Schema
    • 1. Parse the JSON Schema
    • 2. Define your inputs set
    • 3. Validate the inputs and report the errors
  • Validate Polygon Parameter
  • Check Polygon Area (CQL2)
  • Add CQL2 Area Function
  • Validate URI Input
  • Validate Datetime Input
  • Validate Date Range Input
  • Validation by Policy
  • Validation by Filtering
  • Validate Point In Polygon (CQL2)
  • Validate Polygon Intersects AOI (CQL2)
  • Validate BBOX Overlap (CQL2)
  • Validate Required Property (Rego)
  • Validate Numeric Range (Rego)
  • Validate Array Cardinality (JSON Schema)
  • Combine Multiple Hints
  • Author CQL2 JSON Encoding
  • Use Packed vs Single CWL
  • Troubleshoot Rego Invalid Literal
  • Troubleshoot CWL Loader Errors
  • Validate DateTime Window (Rego)
  • Validate URI Host Constraints (Rego)
  • Create Reusable Rego Snippets
  • Test Hints With pytest
  • Validate Disjoint Geometries (CQL2)
  • Validate Optional Input (Rego)
  • Validate Enum Values (JSON Schema)
  • Validate Cross-Field Dependency (Rego)
  • Validate Conditional Required Field (Rego)

Reference

  • CLI
  • Assertion Hints
    • Hint Reference
    • Schema
  • Runtime Compatibility
  • Errors

Explanation

  • Architecture
  • Compatibility and Limitations
  • Testing and Reliability
  • Diataxis Mapping
Assertions Mate
  • How-to Guides
  • Validation by Schema
  • Edit on Terradue/assertions-mate

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!

Previous Next

License CC BY-SA 4.0, by Creative Commons

Built with MkDocs using a theme provided by Read the Docs.
Terradue/assertions-mate « Previous Next »