Normalize

ocdskit.normalize.get_schema_hash(schema, normalizer)[source]
Parameters:
  • schema (dict) – a JSON schema

  • normalizer – a function that accepts a JSON Schema and returns a JSON Schema, with all structurally-irrelevant properties removed

Returns:

a checksum

Return type:

int

ocdskit.normalize.convert_from_oas3(schema, *, get_only=False)[source]

Convert from OpenAPI Specification 3.0 to JSON Schema draft 4.

Warning

Modifies schema $ref values in-place.

Limitations

Unsupported:

  • Schema Objects not under #/components/schemas

  • External $ref to Schema Objects

  • Any $ref to Response Objects

  • Any $ref to Path Item Objects

Parameters:
  • value (dict) – a JSON schema

  • get_only (bool) – whether to convert only schemas used by GET paths

Returns:

a schema using JSON Schema draft 4

Return type:

object

ocdskit.normalize.remove_private_fields(value)

Remove properties members that start with underscores.

Warning

Assumes properties is never a direct member of the properties validation keyword.

Parameters:

value (dict) – a JSON schema

ocdskit.normalize.remove_fields(schema, fields)[source]

Remove the given fields from the properties mapping.

Warning

Assumes properties is never a direct member of the properties validation keyword.

Parameters:
  • value (dict) – a JSON schema

  • fields (set[str]) – fields to remove

ocdskit.normalize.remove_unreachable_definitions(schema, pattern)[source]

Remove any definitions that are unreachable from definitions whose names contain pattern.

Performs a breadth-first search from the root definitions, following $ref edges.

Parameters:
  • schema (dict) – a JSON schema

  • pattern (str) – a substring to case-sensitively match against definition names

ocdskit.normalize.fix_validation_errors(schema, normalizer=None)[source]

Fix validation errors in a JSON Schema.

Changes anyOf from an object to an array, deduplicating values based on their normalized form.

Warning

Assumes anyOf is never a direct member of the properties validation keyword.

Parameters:
  • schema (dict) – a JSON schema

  • normalizer – a function that accepts a JSON Schema and returns a JSON Schema, with all structurally-irrelevant properties removed, for deduplication

ocdskit.normalize.get_normal_schema(value, *, remove_nontype_keywords=False, remove_x_keywords=False, remove_fields=())[source]

Remove metadata and validation keywords, x-* keywords and/or specific fields.

Warning

Assumes properties is never a direct member of the properties validation keyword.

Parameters:
  • value (dict) – a JSON schema

  • remove_nontype_keywords (bool) – whether to remove metadata and validation keywords

  • remove_x_keywords (bool) – whether to remove x-* keywords

  • remove_fields (set[str]) – fields to remove

Returns:

a new schema with keywords removed

Return type:

object

ocdskit.normalize.hoist_deep_properties(schema, normalizer)[source]

Move any sub-schema with a properties keyword to the definitions location.

If neither $defs nor definitions exists, $defs is used.

The schema is named using its title keyword, or its parent property.

Warning

Assumes properties is never a direct member of the properties validation keyword.

Limitations

The schema is named after an earlier ancestor if the parent property has the same name as an applicator keyword.

Parameters:
  • schema (dict) – a JSON schema

  • normalizer – a function that accepts a JSON Schema and returns a JSON Schema, with all structurally-irrelevant properties removed

ocdskit.normalize.normalize_schema(schema, normalizer, get_base_classes)[source]

Extract base classes from a schema’s definitions. Rewrite definitions to use allOf inheritance.

Hashes each properties member using normalizer, calls get_base_classes, then performs greedy set-cover to determine multiple inheritance for both base classes and original definitions.

Warning

Modifies schema in-place.

Limitations

All properties mappings must be at each definition’s top-level. See hoist_deep_properties().

Parameters:
  • schema (dict) – a JSON schema

  • normalizer – a function that accepts a JSON Schema and returns a JSON Schema, with all structurally-irrelevant properties removed

  • get_base_classes

    a function that accepts the schema’s definitions as a mapping of definition names to sets of {prop}:{hash} strings, and returns base classes as a list of dicts with the keys:

    name

    The name of the base class

    members

    A sequence of child classes

    props

    A set of {prop}:{hash} strings