asdf_pydantic.schema

Adding existing ASDF tags as a field

Type annotation must be added to the field to specify the ASDF tag to use in the ASDF schema. There are a few options to do this:

  • Use AsdfTag to specify the tag URI.

  • Use WithAsdfSchema and pass in a dictionary to extend the schema with additional properties. The key "$ref" can be used to specify the tag URI.

from asdf_pydantic import AsdfPydanticModel
from asdf_pydantic.schema import AsdfTag
from astropy.table import Table

class MyModel(AsdfPydanticModel):
    table: Annotated[Table, AsdfTag("http://stsci.edu/schemas/asdf.org/table/table-1.1.0")]

For more customization of the ASDF schema output, you can use WithAsdfSchema to extend the schema with additional properties.

# Changing the title of the field
table: Annotated[
    Table,
    WithAsdfSchema({
        "title": "TABLE",
        "$ref": "http://stsci.edu/schemas/asdf.org/table/table-1.1.0"
    }),
]

Module Contents

Classes

GenerateAsdfSchema

Generates ASDF-compatible schema from Pydantic’s default JSON schema generator.

WithAsdfSchema

Functions

Data

API

asdf_pydantic.schema.DEFAULT_ASDF_SCHEMA_REF_TEMPLATE

‘#/definitions/{model}’

asdf_pydantic.schema.DESIRED_ASDF_SCHEMA_KEY_ORDER

(‘$schema’, ‘id’, ‘title’, ‘type’, ‘properties’, ‘allOf’, ‘anyOf’, ‘required’, ‘definitions’)

class asdf_pydantic.schema.GenerateAsdfSchema(by_alias: bool = True, ref_template: str = DEFAULT_ASDF_SCHEMA_REF_TEMPLATE, tag_uri: Optional[str] = None)

Bases: pydantic.json_schema.GenerateJsonSchema

Generates ASDF-compatible schema from Pydantic’s default JSON schema generator.

Caution

Experimental This schema generator is not complete. It currently creates JSON 2020-12 schema (despite $schema says it’s asdf-schema-1.0.0) which are not compatible with ASDF.

Initialization

schema_dialect

‘http://stsci.edu/schemas/asdf/asdf-schema-1.0.0’

generate(schema, mode='validation')
class asdf_pydantic.schema.WithAsdfSchema(asdf_schema: dict, **kwargs)

Bases: pydantic.WithJsonSchema

Initialization

asdf_pydantic.schema.AsdfTag(tag: str, mode: Literal[auto, ref, asdf_pydantic.schema.AsdfTag.tag] = 'auto') asdf_pydantic.schema.WithAsdfSchema