Container Types: List
schema.list
- declare
- generate
- validate
- substitute
Declares a schema for a list.
from d42 import schema
sch = schema.list
Generates a random list.
from d42 import schema, fake
sch = schema.list
generated = fake(sch)
assert isinstance(generated, list)
In the current implementation, the fake
function always returns []
Validates the value against the list schema. If the type is correct, the schema.list
object can match any list and will always return True
.
from d42 import schema
sch = schema.list
assert sch == []
assert sch == [1, 2, 3]
Substitutes the value into the list schema if the validation has passed.
from d42 import schema
sch = schema.list
assert sch % [] == schema.list([])
assert sch % [1, 2] == schema.list([schema.int(1), schema.int(2)])
The from_native function is used to convert the Python objects to d42 types.
schema.list(elements
)
- declare
- generate
- validate
- substitute
Declares a schema for a list of values that conform to the specified schema.
from d42 import schema
sch = schema.list([schema.int, schema.int])
Generates a random list of values that conform to the specified schema.
from d42 import schema, fake
sch = schema.list([schema.int, schema.int])
generated = fake(sch)
assert isinstance(generated, list)
assert isinstance(generated[0], int)
assert isinstance(generated[1], int)
Validates the value against the schema.
from d42 import schema
sch = schema.list([schema.int, schema.int])
assert sch == [1, 2]
assert sch != [1, 2, 3] # invalid length
Substitutes the value into the schema where validation has passed. If the value doesn't conform to the schema, a SubstitutionError
will be raised.
from d42 import schema
sch = schema.list([schema.int, schema.int])
assert sch % [1, 2] == schema.list([schema.int(1), schema.int(2)])
with assertRaises(SubstitutionError):
sch % ["1", "2"] # invalid type
schema.list(type
)
- declare
- generate
- validate
- substitute
Declares a schema for a list. The constructor takes a schema object representing the type of the list elements.
from d42 import schema
sch = schema.list(schema.int)
Generates a random list of values conforming to the schema.
from d42 import schema, fake
sch = schema.list(schema.int)
generated = fake(sch)
assert isinstance(generated, list)
assert all(isinstance(x, int) for x in generated)
Validates the value against the schema, returning True if all the elements in the list match the schema.
from d42 import schema
sch = schema.list(schema.int)
assert sch == []
assert sch == [1, 2, 3]
assert sch != ["42"] # invalid type
Substitutes a list value into a new list where each value is replaced with its corresponding schema. If the value doesn't match the provided schema, a SubstitutionError
will be raised.
from d42 import schema
sch = schema.list(schema.int)
assert sch % [] == schema.list([])
assert sch % [1, 2] == schema.list([schema.int(1), schema.int(2)])
with assertRaises(SubstitutionError):
sch % ["42"] # invalid type
The schema.list
type also supports the .len(length)
method, similar to the schema.str.len(length)
method.