Schemas

class doctor.schema.Schema(schema, schema_path=None)[source]

This class is used to manipulate JSON schemas and validate values against the schema.

Parameters:
  • schema (dict) – The loaded schema.
  • schema_path (str) – The absolute path to the directory of local schemas.
classmethod from_file(schema_filepath, *args, **kwargs)[source]

Create an instance from a YAML or JSON schema file.

Any additional args or kwargs will be passed on when constructing the new schema instance (useful for subclasses).

Parameters:schema_filepath (str) – Path to the schema file.
Returns:an instance of the class.
Raises:SchemaLoadingError – for invalid input files.
get_validator(schema=None)[source]

Get a jsonschema validator.

Parameters:schema (dict) – A custom schema to validate against.
Returns:an instance of jsonschema Draft4Validator.
resolve(ref, document=None)[source]

Resolve a ref within the schema.

This is just a convenience method, since RefResolver returns both a URI and the resolved value, and we usually just need the resolved value.

Parameters:
  • ref (str) – URI to resolve.
  • document (dict) – Optional schema in which to resolve the URI.
Returns:

the portion of the schema that the URI references.

See:

SchemaRefResolver.resolve()

resolver

jsonschema RefResolver object for the base schema.

validate(value, validator)[source]

Validates and returns the value.

If the value does not validate against the schema, SchemaValidationError will be raised.

Parameters:
  • value – A value to validate (usually a dict).
  • validator – An instance of a jsonschema validator class, as created by Schema.get_validator().
Returns:

the passed value.

Raises:
validate_json(json_value, validator)[source]

Validates and returns the parsed JSON string.

If the value is not valid JSON, ParseError will be raised. If it is valid JSON, but does not validate against the schema, SchemaValidationError will be raised.

Parameters:
  • json_value (str) – JSON value.
  • validator – An instance of a jsonschema validator class, as created by Schema.get_validator().
Returns:

the parsed JSON value.

class doctor.schema.SchemaRefResolver(base_uri, referrer, store=(), cache_remote=True, handlers=(), urljoin_cache=None, remote_cache=None)[source]

Subclass in order to provide support for loading YAML files.

_format_stack(stack, current=None)[source]

Prettifies a scope stack for use in error messages.

Parameters:
  • stack (list(str)) – List of scopes.
  • current (str) – The current scope. If specified, will be appended onto the stack before formatting.
Returns:

str

resolve(ref, document=None)[source]

Resolve a fragment within the schema.

If the resolved value contains a $ref, it will attempt to resolve that as well, until it gets something that is not a reference. Circular references will raise a SchemaError.

Parameters:
  • ref (str) – URI to resolve.
  • document (dict) – Optional schema in which to resolve the URI.
Returns:

a tuple of the final, resolved URI (after any recursion) and resolved value in the schema that the URI references.

Raises:

SchemaError

resolve_remote(uri)[source]

Add support to load YAML files.

This will attempt to load a YAML file first, and then go back to the default behavior.

Parameters:uri (str) – the URI to resolve
Returns:the retrieved document