Validator for district42 schema
Project description
valera
Validator for district42 schema
(!) Work in progress, breaking changes are possible until v2.0 is released
Installation
pip3 install valera
Usage
eq
returns True
or False
from district42 import schema
from valera import eq
assert eq(schema.int, 42)
# syntax sugar
assert schema.int == 42
validate
returns ValidationResult
from district42 import schema
from valera import validate
res = validate(schema.int, 42)
assert res.get_errors() == []
validate_or_fail
returns True
or raise ValidationException
from district42 import schema
from valera import validate_or_fail
assert validate_or_fail(schema.int, 42)
Documentation
None
schema.none
sch = schema.none
assert sch == None
assert sch != False # incorrect type
Bool
schema.bool
sch = schema.bool
assert sch == True
assert sch == False
assert sch != None # incorrect type
schema.bool(value
)
sch = schema.bool(True)
assert sch == True
assert sch != False # incorrect value
Int
schema.int
sch = schema.int
assert sch == 42
assert sch != 3.14 # incorrect type
assert sch != "42" # incorrect type
schema.int(value
)
sch = schema.int(42)
assert sch == 42
assert sch != 43 # incorrect value
schema.int.min(value
)
sch = schema.int.min(0)
assert sch == 0
assert sch == 1
assert sch != -1 # < min
schema.int.max(value
)
sch = schema.int.max(0)
assert sch == 0
assert sch == -1
assert sch != 1 # > max
Float
schema.float
sch = schema.float
assert sch == 3.14
assert sch != 3 # incorrect type
assert sch != "3.14" # incorrect type
schema.float(value
)
sch = schema.float(3.14)
assert sch == 3.14
assert sch != 3.15 # incorrect value
schema.float.min(value
)
sch = schema.float.min(0.0)
assert sch == 0.0
assert sch == 0.1
assert sch != -0.1 # < min
schema.float.max(value
)
sch = schema.float.max(0.0)
assert sch == 0.0
assert sch == -0.1
assert sch != 0.1 # > max
Str
schema.str
sch = schema.str
assert sch == ""
assert sch == "banana"
assert sch != None # incorrect type
schema.str.len(length
)
sch = schema.str.len(2)
assert sch == "ab"
assert sch != "a" # missing symbol
schema.str.len(min_length
, max_length
)
sch = schema.str.len(1, ...)
assert sch == "a"
assert sch == "ab"
assert sch != "" # missing symbol
sch = schema.str.len(..., 2)
assert sch == ""
assert sch == "ab"
assert sch != "abc" # extra symbol
sch = schema.str.len(1, 2)
assert sch == "a"
assert sch == "ab"
assert sch != "" # missing symbol
assert sch != "abc" # extra symbol
schema.str.alphabet(letters
)
digits = "01234567890"
sch = schema.str.alphabet(digits)
assert sch == "123"
assert sch != "abc" # incorrect alphabet
schema.str.contains(substr
)
sch = schema.str.contains("banana")
assert sch == "banana!"
assert sch != ""
schema.str.regex(pattern
)
sch = schema.str.regex(r"[a-z]+")
assert sch == "abc"
assert sch != "123" # pattern missmatch
List
schema.list
sch = schema.list
assert sch == []
assert sch == [1, 2]
assert sch != {} # incorrect type
schema.list(elements
)
sch = schema.list([schema.int(1), schema.int(2)])
assert sch == [1, 2]
assert sch != [1] # missing element "2"
schema.list(type
)
sch = schema.list(schema.int)
assert sch == [42]
assert sch != ["42"] # incorrect type
schema.list(type
).len(length
)
sch = schema.list(schema.int).len(3)
assert sch == [1, 2, 3]
assert sch != [1, 2] # missing element
schema.list(type
).len(min_length
, max_length
)
sch = schema.list(schema.int).len(1, ...)
assert sch == [1]
assert sch == [1, 2]
assert sch != [] # missing element
sch = schema.list(schema.int).len(..., 2)
assert sch == []
assert sch == [1, 2]
assert sch != [1, 2, 3] # extra element
sch = schema.list(schema.int).len(1, 2)
assert sch == [1]
assert sch == [1, 2]
assert sch != [] # missing element
assert sch != [1, 2, 3] # extra element
Dict
schema.dict
sch = schema.dict
assert sch == {}
assert sch == {"id": 1}
assert sch != []
schema.dict(keys
)
strict
sch = schema.dict({
"id": schema.int,
optional("name"): schema.str
})
assert sch == {"id": 1}
assert sch == {"id": 1, "name": "Bob"}
assert sch != {"id": 1, "field": "value"} # extra key 'field'
assert sch != {"id": 1, "name": None} # incorrect type
relaxed
sch = schema.dict({
"id": schema.int,
optional("name"): schema.str,
...: ...
})
assert sch == {"id": 1}
assert sch == {"id": 1, "name": "Bob"}
assert sch == {"id": 1, "field": "value"}
assert sch == {"id": 1, "name": "Bob", "field": "value"}
assert sch != {"id": 1, "name": None} # incorrect type
Any
schema.any
sch = schema.any
assert sch == None
assert sch == 42
assert sch == "banana"
assert sch == []
assert sch == {}
schema.any(*types
)
sch = schema.any(schema.str, schema.none)
assert sch == None
assert sch == "banana"
assert sch != 42 # incorrect type
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
valera-1.3.1.tar.gz
(18.1 kB
view hashes)
Built Distribution
valera-1.3.1-py3-none-any.whl
(24.0 kB
view hashes)