Skip to main content

YAML structure validator

Project description

yspec

yspec is a deadly simple checker for structures. It is especially usefull for validation of different yaml/json files

Usage from cli

yspec ./schema.yaml /tmp/data.json

NOTE: yspec able to take json/yaml/toml for schema and for data

Usage from code

from yspec.checker import process_rule

# Some code that prepares data and rules

process_rule(data, rules, 'root')

Schema Format

Schema is a dict of rules. Every rule do some check according to 'match' field. Schema must include 'root' rule which is applied on top object in structure.

For example structure (in YAML):

---
- 'string1'
- 'string2'

will be valid for schema (in YAML):

---
root:
  match: list
  item: string

string:
  match: string

Match Rules

Simple type matches

String

my_awesome_string:
  match: string

Boolean

my_awesome_bool:
  match: bool

Integer

my_awesome_int:
  match: int

Float

my_awesome_float:
  match: float

Recursion type matches

There are two of them: dict and list. Both has the same recursion logic. At first we apply some checks on object itself, and then apply another (or the same) rule to childrens (list elements, or values of dict).

List

my_list:
  match: list
  item: some_other_rule

List is a recurent type. Fist it checks is the object a list, then it check every element according to rule in 'item' attr.

Dict

my_list:
  match: dict
  items:
    key1: string_rule
    key2: integer_rule
  default_item: some_other_rule
  required_items:
    - key2

That is a rule that describe a dict that has two keys (key1 and key2). One of keys (key2) is mandatory. Key1 should be checked according to 'string_rule' rule, while any other keys with any other (non key1 or key2) name will be checked according to some_other_rule.

If we has remove default_item

my_list:
  match: dict
  items:
    key1: string_rule
    key2: integer_rule
  required_items:
    - key2

A dict could be with two keys (key1, key2) maximum

Sample of simple and recursion matches

Schema:

---
boolean:
  match: bool

string:
  match: string

integer:
  match: int

float:
  match: float

list:
  match: list
  item: string

root:
  match: dict
  items:
    key1: boolean
    key2: string
    key3: integer
    key4: float
    key5: list

Data:

---
key1: true
key2: "That is a string"
key3: 1
key4: 1.0
key5:
  - "One more string"
  - "Another string"

Special matches

OneOf match

constraint_list_item:
  match: one_of
  variants:
    - integer_rule
    - some_other_rule

OneOf match success if any of rules from "variants" success.

Dict key selection

Some times you need to do a separate checks for dict that have some key/value pair.

For example you have the following data:

---
- type: type1
  payload:
    - 1
    - 1
- type: type2
  payload:
    - "that is a string"
    - "that is a string2"

In that example you have a dict wich has diffent payload depends of type key. That is possible to describe with following schema:

---
root:
  match: list
  item: list_item

list_item:
  match: dict_key_selection
  selector: type
  variants:
    type1: dict_with_int
    type2: dict_with_string

dict_with_int:
  match: dict
  items:
    type: string
    payload: list_of_int
  required_items:
    - type
    - payload

dict_with_string:
  match: dict
  items:
    type: string
    payload: list_of_string
  required_items:
    - type
    - payload


list_of_int:
  match: list
  item: int

int:
  match: int

list_of_string:
  match: list
  item: string

string:
  match: string

Project details


Download files

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

Source Distribution

yspec-0.1.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

yspec-0.1.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file yspec-0.1.0.tar.gz.

File metadata

  • Download URL: yspec-0.1.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.8.0

File hashes

Hashes for yspec-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c47258e639f50cde32de2c3f415f7c1ac4d2aab7a898699e3e107282fbd75e71
MD5 8a6b2b82fc30cdef24a9e363b471dcde
BLAKE2b-256 963fc5b78a477ca05edbb990b46e9b78fb3fe7db4ca1f49de2a32fb9616b1807

See more details on using hashes here.

File details

Details for the file yspec-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: yspec-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.8.0

File hashes

Hashes for yspec-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 81d491c4d67f12401c032ef547a3965497b842a2af2181730c4d0989dd8e866a
MD5 ef2fef0c2398eb2e67f7f501b670e86c
BLAKE2b-256 bfd0afcf7874a3a98322ffefb90f0d6cab2cc284f3c1070ba7cedff6c1eb5eaa

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