Skip to main content

Detailed JSON schema validation for YAML

Project description

YAML Schema

A library that implements some amount of jsonschema validation against YAML document ASTs.

Unlike other JSON-schema validation tools which give document path relative errors, this approach allows for line & file errors more appropriate to user-facing tools. yamlschema (PyPi) (Github) (source) just loads the YAML document fully and punts directly to jsonschema; making it difficult-to-impossible to map errors back to source locations.

API Overview

yamlschema.LintRecord(level, node, schema, message)

LintRecords are what linting produces. Each LintRecord contains the YAML AST node which failed validation, the schema it failed to validate against, and some metadata.

level is a LintLevel which attempts to explain what "kind" of error this piece of lint represents. For instance LintLevel.MISSING encodes missing properties. LintLevel.MISSMATCH encodes type mismatches. LintLevel.UNEXPECTED encodes unexpected/disallowed keys and other errors.

yamlschema.YamlLinter(schema)

The linter itself is implemented as a class with a variety of instance methods; allowing the linter to be hacked by users much in the same way that JSON encodiers and decoders can be hacked.

The linter "interface" consists of __init__(schema: dict); being a loaded JSON schema as a dict tree and lint_document(schema, node) -> Iterable[LintRecord] which initiates the recursive linting.

The reference implementation of the linter recursively calls lint_document on every sub-structure in the document.

yamlschema.lint_file(schema, path, cls=YamlLinter)

As conveniences, yamlschema gives you a couple entrypoints that handle constructing the linter class, using yaml.compose() to get an AST and starting linting for you. lint_file and lint_buffer respectively allow the user to either bring a file path or a string of YAML.

Example

>>> from yamlschema import lint_buffer
>>> list(lint_buffer({"type": "integer"}, "---\n1.0"))
[
  LintRecord(
    level=<LintLevel.MISSMATCH: 2>,
    node=ScalarNode(tag='tag:yaml.org,2002:float', value='1.0'),
    schema={'type': 'integer'},
    message="Expected an integer, got a 'tag:yaml.org,2002:float'"
  )
]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

arrdem.yamlschema-0.1.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file arrdem.yamlschema-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: arrdem.yamlschema-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.6

File hashes

Hashes for arrdem.yamlschema-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 12ac5bb1f8b68597fe213cf07958322bccf3968870226c92723782c9140e516b
MD5 8fd57ea874a6138a1885400a3602bbd5
BLAKE2b-256 d304fecdea29116517afe6469296e81281d393d84f891f10daaef65c647f9431

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page