Skip to content

Custom Schemas

Custom Schemas

By default, pyrs-yaml uses the YAML 1.2 Core schema for implicit type resolution. With the YAML Schema Language, you can define custom schemas that control how plain scalars are resolved to Python types.

Why Custom Schemas?

The Core schema resolves 0xFF to int(255), 2026-08-11 to int(2026) and hello to "hello". Sometimes you want different behavior:

  • Keep dates as strings ("2026-08-11" instead of 2026)
  • Parse hex/binary literals as integers
  • Add YAML 1.1-style boolean lexemes (yes/no)
  • Use a JSON-only subset (no inf, nan, 0x)

Schema Definition Format

A schema is defined as a YAML file with a rules list. Each rule has a pattern (regex) and a type (one of null, bool, int, float, str).

hex_schema.yaml
# hex_schema.yaml
name: hex
extends: core
rules:
  - pattern: ^0x[0-9a-fA-F]+$
    type: int
  - pattern: ^0b[01]+$
    type: int

extends — Optional base schema. Rules are checked first; if none match, the extends schema handles resolution. Default: core. The name must be a built-in (failsafe, json, core, yaml1.1) or a registered schema; an unknown name is refused with the known set listed in the error.

rules — Required, non-empty ordered list. The first matching pattern determines the type. Both the text door (register_schema) and the inline-dict door refuse a schema whose rules is absent or empty. Supported types and their values:

type Python result Example
null None ~
bool True / False true, yes, on
int int 42, 0xFF, 0o77, 0b1010
float float 3.14, 1e10
str str 2026-08-11

Registering and Using a Schema

Register and use a schema
import pyrs_yaml

# Register from a YAML string
pyrs_yaml.register_schema(
    "hex",
    """
name: hex
extends: core
rules:
  - pattern: ^0x[0-9a-fA-F]+$
    type: int
""",
)

# Use with YAML instance
y = pyrs_yaml.YAML(schema="hex")
doc = y.parse("addr: 0xFF")
assert doc.get("addr") == 255

# Use with module-level functions
d = pyrs_yaml.safe_load("addr: 0x1F", schema="hex")
assert d["addr"] == 31

Loading a schema from a file

load_schema() reads a schema definition from a file path and registers it:

Load schema from file
# hex.yaml contains the schema YAML shown above
pyrs_yaml.load_schema("hex", "path/to/hex.yaml")

Listing registered schemas

list_schemas() returns all registered schema names (built-in + custom):

List registered schemas
print(pyrs_yaml.list_schemas())
# ['failsafe', 'json', 'core', 'yaml1.1', 'hex', ...]

Structural validation

Structural checks are not part of scalar resolution. Document.validate() is the package's only validation surface: it accepts a JSON Schema and delegates evaluation to Python's jsonschema. A validate: section in a schema definition is not read.

Validate with JSON Schema
import pyrs_yaml

doc = pyrs_yaml.parse("port: 8080\ntags: [web, api]\n")
schema = {
    "type": "object",
    "properties": {"port": {"type": "integer"}},
    "required": ["port"],
}
doc.validate(schema)  # None; raises YamlValidateError when the document fails

The error carries the reference engine's message ('port' is a required property), and jsonschema.validate() on the same schema and instance reaches the same verdict. The CLI guide validates files against a schema file.

One boundary comes with that delegation, and this release keeps it: it is a known boundary the project has registered, not a defect, and building an evaluator of the project's own is a separate design question.

No install route here brings in an evaluator. pyrs-yaml depends on typing-extensions alone and its extras are cli, pydantic and settings, so run pip install jsonschema first. In an environment holding only the library doc.validate(schema) raises YamlValidateError whose message names that install route and carries the import error — even for a document the schema would pass.

Schema text is JSON, never YAML. Unlike the definitions register_schema() and load_schema() read on this page, a str handed to validate() goes through json.loads before any evaluator is consulted, so doc.validate("type: object\nrequired: [port]\n") raises YamlValidateError: schema text must be JSON (Expecting value: line 1 column 1 (char 0)); pass the schema as a dict if it is not JSON — with or without jsonschema installed. The refusal stays inside the documented Yaml* family, so the CLI prints one error: line; pyrs-yaml validate doc.yaml --schema-file schema.yaml exits 1 with that line on stderr and an empty stdout.

Inline Dict Schema

Instead of registering separately, pass a dict directly:

Inline dict schema
d = pyrs_yaml.safe_load(
    "addr: 0xFF",
    schema={
        "extends": "core",
        "rules": [{"pattern": "^0x[0-9a-fA-F]+$", "type": "int"}],
    },
)
assert d["addr"] == 255

Common Patterns

schema = {
    "extends": "core",
    "rules": [{"pattern": "^\\d{4}-\\d{2}-\\d{2}$", "type": "str"}],
}
schema = {
    "extends": "core",
    "rules": [{"pattern": "^(yes|no|Yes|No|YES|NO)$", "type": "bool"}],
}
schema = {
    "extends": "failsafe",
    "rules": [
        {"pattern": "^null$|^~$", "type": "null"},
        {"pattern": "^(true|false)$", "type": "bool"},
        {"pattern": "^-?\\d+$", "type": "int"},
        {"pattern": "^-?\\d+\\.\\d+$", "type": "float"},
    ],
}

Performance

Custom schemas use a regex-based rule engine. For each scalar, rules are checked in order until a match is found. For best performance:

  • Keep rule count under 20
  • Put the most common patterns first
  • Use extends: core to avoid re-implementing the full Core resolution

The built-in Core schema is unaffected — it still uses the zero-cost match dispatch and is not impacted by custom schema registration.