Skip to main content

Anonymous datatype validation

Project description

Examples

>>> from datatype.validation import failures

>>> datatype = {'foo': [{'bar': 'int'}]}
>>> bad_value = {'foo': [{'bar': 'baz'}], 'bif': 'pow!'}

>>> failures(datatype, bad_value)
['unexpected property "bif"', 'foo[0].bar: expected int, got str']

Wildcard dictionary keys:

>>> datatype = {'_any_': ['int']}
>>> good_value = {'foo': [1, 2, 3], 'bar': [3, 4, 5]}

>>> failures(datatype, good_value)
[]

Coercion:

>>> from datatype.coercion import coerce_value

>>> coerce_value(['str'], [1, 2, 3])
['1', '2', '3']

Datatype Definitions

Datatype definitions are represented with a small set of types that should be built-in for most languages.

Required types for proper validation:

  • int

  • float

  • string

  • boolean

  • dictionary (or anonymous object)

  • list (or array)

Specification

DEFINITION = PRIMITIVE | LIST | DICTIONARY | TUPLE
PRIMITIVE = ["nullable "] + ("int" | "str" | "float" | "bool")
DICTIONARY = (dictionary of) key: DICTIONARY-KEY, value: DEFINITION
DICTIONARY-KEY = (["optional "] + DICTIONARY-KEY-NAME) | "_any_"
DICTIONARY-KEY-NAME = [A-Za-z0-9_]+
LIST = (list of one) DEFINITION
TUPLE = (list of more than one) DEFINITION

Definition Examples (in python)

definition: "int"
example value: 5

definition: {"foo": "int"}
example value: {"foo": 5}

definition: [{"foo": ["bool"]}]
example value: [{"foo": [True, False]}, {"foo": [False, False]}]

definition: {"_any_": "int"}
example value: {"foo": 5, "bar": 7}

definition: ["int", "str"]
example value: [5, "foo"]

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

datatype-0.8.0.tar.gz (4.9 kB view details)

Uploaded Source

File details

Details for the file datatype-0.8.0.tar.gz.

File metadata

  • Download URL: datatype-0.8.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for datatype-0.8.0.tar.gz
Algorithm Hash digest
SHA256 236fa51ba07f4ca2314829322aa1826ea761bb7e73517cf489d9487e7e64cc51
MD5 49f8b5aaa3eb9c082caf700a6056c3b1
BLAKE2b-256 9b660ec42a92d5a0a62bc1e5fda244f97e8f8b24cfc3179c63924b030259d0a7

See more details on using hashes here.

Supported by

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