Skip to content

asdf.schema

Functions:

Name Description
check_schema

Check a given schema to make sure it is valid YAML schema.

fill_defaults

For any default values in the schema, add them to the tree if they

load_schema

Load a schema from the given URL.

remove_defaults

For any values in the tree that are the same as the default values

validate

Validate the given instance (which must be a tagged tree) against

check_schema

check_schema(schema, validate_default=True)

Check a given schema to make sure it is valid YAML schema.

Parameters:

Name Type Description Default
schema dict

The schema object, as returned by load_schema.

required
validate_default bool

Set to True to validate the content of the default field against the schema.

True
Source code in asdf/schema.py
def check_schema(schema, validate_default=True):
    """
    Check a given schema to make sure it is valid YAML schema.

    Parameters
    ----------
    schema : dict
        The schema object, as returned by ``load_schema``.
    validate_default : bool, optional
        Set to `True` to validate the content of the default
        field against the schema.
    """
    validators = util.HashableDict(mvalidators.Draft4Validator.VALIDATORS.copy())

    if validate_default:
        # The jsonschema library doesn't validate defaults
        # on its own.
        instance_validator = get_validator(schema)
        instance_scope = schema.get("id", "")

        def _validate_default(validator, default, instance, schema):
            if not validator.is_type(instance, "object"):
                return

            if "default" in instance:
                instance_validator.resolver.push_scope(instance_scope)
                try:
                    yield from instance_validator.descend(instance["default"], instance)
                finally:
                    instance_validator.resolver.pop_scope()

        validators.update({"default": _validate_default})

        def applicable_validators(schema):
            items = list(schema.items())
            items.append(("default", ""))
            return items

    else:
        applicable_validators = methodcaller("items")

    meta_schema_id = schema.get("$schema", YAML_SCHEMA_METASCHEMA_ID)
    meta_schema = _load_schema_cached(meta_schema_id, False)

    resolver = _make_jsonschema_refresolver()

    cls = mvalidators.create(
        meta_schema=meta_schema,
        validators=validators,
        type_checker=mvalidators.Draft4Validator.TYPE_CHECKER,
        id_of=mvalidators.Draft4Validator.ID_OF,
        applicable_validators=applicable_validators,
    )
    validator = cls(meta_schema, resolver=resolver)
    validator.validate(schema)

fill_defaults

fill_defaults(instance, ctx, reading=False)

For any default values in the schema, add them to the tree if they don’t exist.

Parameters:

Name Type Description Default
instance tagged tree
required
ctx AsdfFile context

Used to resolve tags and urls

required
reading

Indicates whether the ASDF file is being read (in contrast to being written).

False
Source code in asdf/schema.py
def fill_defaults(instance, ctx, reading=False):
    """
    For any default values in the schema, add them to the tree if they
    don't exist.

    Parameters
    ----------
    instance : tagged tree

    ctx : AsdfFile context
        Used to resolve tags and urls

    reading: bool, optional
        Indicates whether the ASDF file is being read (in contrast to being
        written).
    """
    validate(instance, ctx, validators=FILL_DEFAULTS, reading=reading)

load_schema

load_schema(url, resolve_references=False)

Load a schema from the given URL.

Parameters:

Name Type Description Default
url str

The path to the schema

required
resolve_references bool

If True, resolve all $ref references.

False
Source code in asdf/schema.py
def load_schema(url, resolve_references=False):
    """
    Load a schema from the given URL.

    Parameters
    ----------
    url : str
        The path to the schema

    resolve_references : bool, optional
        If ``True``, resolve all ``$ref`` references.

    """
    # We want to cache the work that went into constructing the schema, but returning
    # the same object is treacherous, because users who mutate the result will not
    # expect that they're changing the schema everywhere.
    return copy.deepcopy(_load_schema_cached(url, resolve_references))

remove_defaults

remove_defaults(instance, ctx)

For any values in the tree that are the same as the default values specified in the schema, remove them from the tree.

Parameters:

Name Type Description Default
instance tagged tree
required
ctx AsdfFile context

Used to resolve tags and urls

required
Source code in asdf/schema.py
def remove_defaults(instance, ctx):
    """
    For any values in the tree that are the same as the default values
    specified in the schema, remove them from the tree.

    Parameters
    ----------
    instance : tagged tree

    ctx : AsdfFile context
        Used to resolve tags and urls
    """
    validate(instance, ctx, validators=REMOVE_DEFAULTS)

validate

validate(
    instance, ctx=None, schema=None, validators=None, reading=False, *args, **kwargs
)

Validate the given instance (which must be a tagged tree) against the appropriate schema. The schema itself is located using the tag on the instance.

The additional *args and **kwargs are passed along to the validator.

Parameters:

Name Type Description Default
instance tagged tree
required
ctx AsdfFile context

Used to resolve tags and urls

None
schema schema

Explicit schema to use. If not provided, the schema to use is determined by the tag on instance (or subinstance) and any custom schema provided to ctx.

None
validators dict

A dictionary mapping properties to validators to use (instead of the built-in ones and ones provided by extension types).

None
reading

Indicates whether validation is being performed when the file is being read. This is useful to allow for different validation behavior when reading vs writing files.

False
Source code in asdf/schema.py
def validate(instance, ctx=None, schema=None, validators=None, reading=False, *args, **kwargs):
    """
    Validate the given instance (which must be a tagged tree) against
    the appropriate schema.  The schema itself is located using the
    tag on the instance.

    The additional ``*args`` and ``**kwargs`` are passed along to
    the validator.

    Parameters
    ----------
    instance : tagged tree

    ctx : AsdfFile context
        Used to resolve tags and urls

    schema : schema, optional
        Explicit schema to use.  If not provided, the schema to use
        is determined by the tag on instance (or subinstance) and
        any custom schema provided to ``ctx``.

    validators : dict, optional
        A dictionary mapping properties to validators to use (instead
        of the built-in ones and ones provided by extension types).

    reading: bool, optional
        Indicates whether validation is being performed when the file is being
        read. This is useful to allow for different validation behavior when
        reading vs writing files.
    """
    if ctx is None:
        from ._asdf import AsdfFile

        ctx = AsdfFile()

    if schema is None and ctx._custom_schema:
        schema = ctx._custom_schema
    validator = get_validator({} if schema is None else schema, ctx, validators, *args, **kwargs)
    validator.validate(instance)

    additional_validators = [_validate_large_literals]
    if ctx.version >= versioning.RESTRICTED_KEYS_MIN_VERSION:
        additional_validators.append(_validate_mapping_keys)

    def _callback(instance):
        for validator in additional_validators:
            validator(instance, reading)

    treeutil.walk(instance, _callback)