Fields¶
Scheme fields are intended to describe the desired result. When you create a schema class, fields are declared as its attributes.
Field initialization arguments¶
Parameters accepted by all field types and their default values:
allow_none = True– Allow null valuesrequired = True– Is the field requireddefault = True– Any value that will be used by defaultvalidate = None– None type or validatorpositive_data_from = None– A function object that will return a list of positive values or Nonenegative_data_from = None– A function object that will return a list of negative values or None
Note
Some field types such as Integer, Float, Collection and Nested
accept an additional argument. For Integer and Float this argument is precision.
It is responsible for the minimum number step that will be used when generating negative values
validator Range. For the Collection and Nested types, this parameter is data_type.
It accepts a nested data type. For Collection - the data type inside the collection,
for Nested - nested schema (class instance inherited from SGen)
Note
When using the positive_data_from and negative_data_from parameters their values
will completely replace the result of generating the positive() and negative methods
String field¶
Represents a string data type. Generates random strings with the ability to install
minimum/maximum length by using the Length validator
from sgen import SGen, validate, fields
class User(SGen):
name = fields.String(
validate=validate.Length(
min=10,
max=100,
min_inclusive=True,
max_inclusive=False,
)
)
user_sgen = User()
user_sgen.positive() # Returns positive data generator
user_sgen.negative() # Returns negative data generator
The min_inclusive and max_inclusive parameters are responsible for including min/max in the range of acceptable values
Integer Field¶
Represents an integer data type. Generates random numbers with the ability to set
min/max value by using Range validator
Additionally, it includes the precision parameter, which is responsible for the step when generating negative values
or positive, provided that at least one of the ...inclusive parameters is False.
Note
I don’t see any obvious cases of using the precision parameter for the Integer type, I left it for flexibility.
from sgen import SGen, validate, fields
class User(SGen):
age = fields.Integer(
validate=validate.Range(
min=10,
max=100,
min_inclusive=True,
max_inclusive=False,
)
)
The min_inclusive and max_inclusive parameters are responsible for including min/max in the range of acceptable values
Float field¶
Represents floating point numbers. Generates settable random floating point numbers
min/max value by using Range validator
Additionally, it includes the precision parameter, which is responsible for the step when generating negative values
or positive, provided that at least one of the ...inclusive parameters is False.
from sgen import SGen, validate, fields
class User(SGen):
balance = fields.Float(
validate=validate.Range(
min=10,
max=100,
min_inclusive=True,
max_inclusive=False,
)
)
The min_inclusive and max_inclusive parameters are responsible for including min/max in the range of acceptable values
Boolean field¶
Represents a Boolean data type.
from sgen import SGen, validate, fields
class User(SGen):
age = fields.Float(
validate=validate.Equal(
comparable=True
)
)
DateTime and Date field¶
Represents time data types.
from sgen import SGen, validate, fields
class User(SGen):
created_at = fields.DateTime()
birth_date = fields.Date()
Collection field¶
Represents lists.
from sgen import SGen, validate, fields
class User(SGen):
numbers = fields.List(
data_type=fields.Integer()
)
Accepts the argument data_type which must be an inheritor of the Field class
Nested field¶
Represents nested schemas.
from sgen import SGen, validate, fields
class Pet(SGen):
name = fields.String()
class User(SGen):
pet = fields.Nested(
Pet(),
required=True,
)
Accepts the argument data_type which must be an inheritor of the class SGen