Validators¶
This module implements JSON Schema draft03 and draft04.
Basic¶
from jsonspec.validators import load
# data will validate against this schema
validator = load({
'title': 'Example Schema',
'type': 'object',
'properties': {
'age': {
'description': 'Age in years',
'minimum': 0,
'type': 'integer'
},
'firstName': {
'type': 'string'
},
'lastName': {
'type': 'string'
}
},
'required': [
'firstName',
'lastName'
]
})
# validate this data
validator.validate({
'firstName': 'John',
'lastName': 'Noone',
'age': 33,
})
Choose specification¶
Schemas will be parsed by the draft04 specification by default. You can setup, or even better mix between draft03 and draft04.
Show these examples:
validator = load({
'id': 'foo',
'properties': {
'bar': {
'id': 'baz'
},
},
})
foo schema: | parsed with draft04 |
---|---|
baz schema: | parsed with draft04 |
validator = load({
'id': 'foo',
'properties': {
'bar': {
'id': 'baz'
},
},
}, spec='http://json-schema.org/draft-03/schema#')
foo schema: | parsed with draft03 |
---|---|
baz schema: | parsed with draft03 |
validator = load({
'id': 'foo',
'properties': {
'bar': {
'$schema': 'http://json-schema.org/draft-03/schema#',
'id': 'baz'
},
},
})
foo schema: | parsed with draft04 |
---|---|
baz schema: | parsed with draft03 |
About format¶
This module implements a lot of formats, exposed to every draft:
name | description | enabling |
---|---|---|
validate email | ||
hostname | validate hostname | |
ipv4 | validate ipv4 | pip install json-spec[ip] |
ipv6 | validate ipv6 | pip install json-spec[ip] |
regex | validate regex | |
uri | validate uri | |
css.color | validate css color | |
rfc3339.datetime | see rfc3339 | |
utc.datetime | YYYY-MM-ddThh:mm:SSZ | |
utc.date | YYYY-MM-dd | |
utc.time | hh:mm:SS | |
utc.millisec | any integer, float |
Some formats rely on external modules, and they are not enabled by default.
Each draft validator aliases they formats to these formats. See draft04
and draft03
methods for more details.
Regarding your needs, you can register your own formats. Use entry_points
in your setup.py
. for example:
[entry_points]
jsonspec.validators.formats =
my:format = my.module:validate_format
API¶
-
validators.
load
(schema, uri=None, spec=None, provider=None)¶ Scaffold a validator against a schema.
Parameters: - schema (Mapping) – the schema to compile into a Validator
- uri (Pointer, str) – the uri of the schema. it may be ignored in case of not cross referencing.
- spec (str) – fallback to this spec if the schema does not provides ts own
- provider (Mapping, Provider...) – the other schemas, in case of cross referencing
-
validators.draft04.
compile
(schema, pointer, context, scope=None)¶ Compiles schema with JSON Schema draft-04.
Parameters:
-
validators.
register
(compiler=None, spec=None)¶ Expose compiler to factory.
Parameters: - compiler (callable) – the callable to expose
- spec (str) – name of the spec
It can be used as a decorator:
@register(spec='my:first:spec') def my_compiler(schema, pointer, context): return Validator(schema)
or as a function:
def my_compiler(schema, pointer, context): return Validator(schema) register(my_compiler, 'my:second:spec')
-
class
validators.
ReferenceValidator
(pointer, context)¶ Reference a validator to his pointer.
Variables: - pointer – the pointer to the validator
- context – the context object
- default – return the default validator
- validator – return the lazy loaded validator
>>> validator = ReferenceValidator('http://json-schema.org/geo#', context) >>> assert validator({ >>> 'latitude': 0.0124, >>> 'longitude': 1.2345 >>> })
-
validate
(obj, pointer=None)¶ Validate object against validator.
Parameters: - obj – the object to validate
- pointer – the object pointer
-
class
validators.
Draft03Validator
(attrs, uri=None, formats=None)¶ Implements JSON Schema draft-03 validation.
Variables: - attrs – attributes to validate against
- uri – uri of the current validator
- formats – mapping of available formats
>>> validator = Draft03Validator({'min_length': 4}) >>> assert validator('this is sparta')
-
fail
(reason, obj, pointer=None)¶ Called when validation fails.
-
has_default
()¶ docstring for has_default
-
is_optional
()¶ True by default.
-
validate
(obj, pointer=None)¶ Validate object against validator
Parameters: obj – the object to validate
-
validate_format
(obj, pointer=None)¶ Expected draft03 Alias of color css.color date-time utc.datetime date utc.date time utc.time utc-millisec utc.millisec regex regex style css.style phone phone uri uri email email ip-address ipv4 ipv6 ipv6 host-name hostname
-
class
validators.
Draft04Validator
(attrs, uri=None, formats=None)¶ Implements JSON Schema draft-04 validation.
Variables: - attrs – attributes to validate against
- uri – uri of the current validator
- formats – mapping of available formats
>>> validator = Draft04Validator({'min_length': 4}) >>> assert validator('this is sparta')
-
fail
(reason, obj, pointer=None)¶ Called when validation fails.
-
is_optional
()¶ Returns True, beceause it is meaningless in draft04.
-
validate
(obj, pointer=None)¶ Validate object against validator
Parameters: obj – the object to validate
-
validate_format
(obj, pointer=None)¶ Expected draft04 Alias of date-time rfc3339.datetime email email hostname hostname ipv4 ipv4 ipv6 ipv6 uri uri
-
class
validators.
Context
(factory, registry, spec=None, formats=None)¶ Variables: - factory – global factory
- registry – the current registry
- spec – the current spec
- formats – the current formats exposed
-
class
validators.
Factory
(provider=None, spec=None, formats=None)¶ Variables: - provider – global registry
- spec – default spec