Skip to content

aea.helpers.json_schema

Inlined JSON Schema Draft-04 validator.

Replaces the external jsonschema package. Implements only the subset of Draft-04 keywords used by the AEA configuration schemas.

Supported keywords: type, properties, patternProperties, propertyNames, required, additionalProperties, items, enum, pattern, minimum, maximum, exclusiveMinimum, exclusiveMaximum, minLength, maxLength, minItems, maxItems, oneOf, anyOf, allOf, not, uniqueItems, dependencies, additionalItems, $ref, definitions, default, description.

ValidationError Objects

class ValidationError(Exception)

JSON Schema validation error with path tracking.

__init__

def __init__(message: str, path: Optional[Sequence] = None) -> None

Initialize ValidationError.

Arguments:

  • message: error description.
  • path: path to the failing element in the instance.

RefResolver Objects

class RefResolver()

Resolve JSON $ref pointers against a base URI and local schema store.

__init__

def __init__(base_uri: str, referrer: Dict) -> None

Initialize resolver.

Arguments:

  • base_uri: the base URI (file:// or http://) for cross-file refs.
  • referrer: the root schema document.

resolve

def resolve(ref: str, current_schema: Optional[Dict] = None) -> Dict

Resolve a $ref string to the target sub-schema.

Arguments:

  • ref: the $ref value, e.g. #/definitions/foo or other.json#/definitions/bar.
  • current_schema: the schema containing this $ref (defaults to the root referrer document).

Returns:

the resolved sub-schema dict.

TypeChecker Objects

class TypeChecker()

Default type checker using the Draft-04 type map.

is_type

def is_type(instance: Any, type_name: str) -> bool

Check if instance matches the given type name.

Arguments:

  • instance: the value to check.
  • type_name: a JSON Schema type name.

Returns:

True if the instance matches.

find_additional_properties

def find_additional_properties(instance: Dict, schema: Dict) -> Iterator[str]

Yield property names in instance not covered by the schema.

Arguments:

  • instance: the object instance.
  • schema: the schema with properties and/or patternProperties.

Returns:

property names not covered by the schema.

Draft4Validator Objects

class Draft4Validator()

JSON Schema Draft-04 validator.

Supports all Draft-04 keywords: type, properties, patternProperties, propertyNames, required, additionalProperties, items, additionalItems, enum, pattern, minimum, maximum, exclusiveMinimum, exclusiveMaximum, minLength, maxLength, minItems, maxItems, oneOf, anyOf, allOf, not, uniqueItems, dependencies, $ref, definitions.

check_schema

@classmethod
def check_schema(cls, schema: Dict) -> None

Validate that a schema is a well-formed JSON Schema document.

Validates schema against the Draft-04 meta-schema.

Arguments:

  • schema: the schema dict.

__init__

def __init__(schema: Dict, resolver: Optional[RefResolver] = None) -> None

Initialize validator.

Arguments:

  • schema: the JSON schema dict.
  • resolver: optional RefResolver for cross-file $ref.

validate

def validate(instance: Any) -> None

Validate an instance and raise on the first error.

Arguments:

  • instance: the data to validate.

iter_errors

def iter_errors(instance: Any) -> Iterator[ValidationError]

Yield all validation errors for the given instance.

Arguments:

  • instance: the data to validate.

Returns:

validation errors.

extend

def extend(validator: Type[Draft4Validator],
           validators: Optional[Dict[str, ValidatorFn]] = None,
           type_checker: Optional[Any] = None) -> Type[Draft4Validator]

Create a new validator class by extending an existing one.

Arguments:

  • validator: the base validator class.
  • validators: dict of keyword -> validator function overrides.
  • type_checker: optional custom type checker.

Returns:

a new validator class.