API Documentation

This document specifies SGen’s APIs.

Schema

class SGen
fields(is_positive: bool) List[SchemaField]

Returns a list of positive field generators if is_positive=True

Parameters:

is_positive (bool) – True if positive data generators need to be returned

Returns:

List of objects of type SchemaField

positive() Generator:
Returns:

List of valid dictionaries

Return type:

Generator object

negative() Generator:
Returns:

List of not valid dictionaries

Return type:

Generator object

Class SGen can be used to:

  • Description of the data structure

  • Generating valid data sets

  • Generating not valid data sets

Example:

from sgen import Sgen, fields


class User(SGen):
    name = fields.String(required=True)
    age = fields.Integer(allow_none=False)

user_sgen = User()
user_sgen.positive()  # Will return a set of valid data
user_sgen.negative()  # Will return a set of not valid data

Fields

class Field
__init__(validate: ValidatorABC | Iterable[ValidatorABC] | None = None, positive_data_from: Callable[[], Iterable] = None, negative_data_from: Callable[[], Iterable] = None, allow_none: bool = True, required: bool = False, default: Any = None)
Parameters:
  • validate (Any) – Data validator

  • positive_data_from (Callable[[], Iterable]) – Function that returns a tuple of positive data

  • negative_data_from (Callable[[], Iterable]) – Function that returns a tuple of negative data

  • allow_none (bool) – True if the field can accept the value None

  • required (bool) – True if the field is required

  • default (Any) – Default value

Initializes an instance of a class

positive()

Generates a common set of positive values for all internal field types

negative()

Generates a common set of negative values for all internal field types

set_positive_values()
Raises:

NotImplemented – If a method is not implemented in parent class

Note

Implement this method in your data type. It should register positive values via self._register(‘some_value’)

Generates a common set of negative values for all internal field types

set_negative_values()
Raises:

NotImplemented – If a method is not implemented in parent class

Note

Implement this method in your data type. It should register negative values via self._register(‘some_value’)

Generates a common set of negative values for all internal field types

generate(length: int)
Raises:

NotImplemented – If a method is not implemented in iterable data types when using a validator Length

Returns an iterable object of length length

get_step()
Raises:

NotImplemented – If the method is not implemented in numeric data types when using a validator Range

Returns the step by which the numeric value will be shifted to obtain negative values (out of the acceptable range of the Range validator)

get_other_value(value: Any)
Raises:

NotImplemented – If the method is not implemented in the data type when using validators Equal, OneOf, NoneOf

Returns a value of the same type as value, but not equal to value

_register(for_register: Any | List[Any])
Parameters:

for_register (Union[Any, List[Any]]) – Field value or list of values

Stores for_register in the list of field values

Note

When creating your own numeric data types, implement their method get_precision

Note

When creating your own iterable data types, implement their method generate

Class Field is the base class for all internal data types

Example:

from sgen import fields


class MyInt(fields.Field)

    def set_positive_values(self):
        self._register(1)  # Add a valid type value

    def set_negative_values(self):
        self._register('not_int')  # Add a value that is not an instance of your data type

    def get_other_value(self, value: int) -> int:
        if value is None:
            return 1
        return value + 1

    def __add__():
        pass

    def __sub__():
        pass
class String
set_positive_values()
Return type:

None

Registers a set of positive values for a type String

set_negative_values()
Return type:

None

Registers a set of negative values for a type String

generate(length: int)
Parameters:

length (int) – Length of the generated string

Return type:

str

Generates a string of the specified length.

get_other_value(value: str | None)
Parameters:

value (str) – Undesired string value

Return type:

str

Returns a value of type str that is not equal to value

Class String is a representation of string data types

Example:

from sgen import fields, SGen, validate


class User(SGen)
    name = fields.String(allow_none=False)
    address = fields.String(
        validate=validate.Equal(comparable='Pepega street')
    )
class Integer
__init__(step: int = 1, *args, **kwargs)
Parameters:

step (int) – Step to generate values

set_positive_values()
Return type:

None

Registers a set of positive values for a type Integer

set_negative_values()
Return type:

None

Registers a set of negative values for a type Integer

get_step()
Return type:

int

Returns the step

get_other_value(value: int | None)
Parameters:

value (int) – Undesired number value

Return type:

int

Returns a value of type int that is not equal to value

Class Integer is a representation of an integer data type

Example:

from sgen import fields, SGen, validate


class User(SGen)
    age = fields.Integer(validate=validate.Range(min=21))
class Float
__init__(step: float = 1, *args, **kwargs)
Parameters:

step (int) – Step to generate values

set_positive_values()
Return type:

None

Registers a set of positive values for a type Float

set_negative_values()
Return type:

None

Registers a set of negative values for a type Float

get_step()
Return type:

float

Returns the step

get_other_value(value: float | None)
Parameters:

value (float) – Undesired number value

Return type:

float

Returns a value of type float that is not equal to value

Class Float is a floating point representation

Example:

from sgen import fields, SGen, validate


class User(SGen)
    balance = fields.Float(
        validate=validate.Range(min=0),
        step=0.0001
    )
class Boolean
set_positive_values()
Return type:

None

Registers a set of positive values for a type Boolean

set_negative_values()
Return type:

None

Registers a set of negative values for a type Boolean

get_other_value(value: bool | None)
Parameters:

value (bool) – Undesired number value

Return type:

bool

Returns a value of type bool that is not equal to value

Class Boolean is a representation of the boolean data type

Example:

from sgen import fields, SGen, validate


class User(SGen)
    is_admin = fields.Boolean()
class DateTime
__init__(step: timedelta = timedelta(days=1), *args, **kwargs)
Parameters:

step (int) – Step to generate values

set_positive_values()
Return type:

None

Registers a set of positive values for a type DateTime

set_negative_values()
Return type:

None

Registers a set of negative values for a type DateTime

get_other_value(value: datetime | None)
Parameters:

value (datetime) – Undesired number value

Return type:

datetime

Returns a value of type datetime that is not equal to value

get_step()
Return type:

timedelta

Returns the step

Class DateTime is a representation of the data type datetime

Example:

from sgen import fields, SGen, validate


class User(SGen)
    created_at = fields.DateTime()
class Date
__init__(step: timedelta = timedelta(days=1), *args, **kwargs)
Parameters:

step (int) – Step to generate values

set_positive_values()
Return type:

None

Registers a set of positive values for a type Date

set_negative_values()
Return type:

None

Registers a set of negative values for a type Date

get_other_value(value: date | None)
Parameters:

value (date) – Undesired number value

Return type:

date

Returns a value of type date that is not equal to value

get_step()
Return type:

timedelta

Returns the step

Class Date is a date representation

Example:

from sgen import fields, SGen, validate


class User(SGen)
    birth_date = fields.Date()
class Collection
__init__(data_type: FieldABC | 'SGen', *args, **kwargs)
Parameters:

data_type (Union[FieldABC, 'SGen']) – Collection data type

_register(for_register: Any | List[Any])
Parameters:

for_register (Union[Any, List[Any]]) – Logged value or list of logged values

Adds a new value/values to the field’s list of values if it is not already present Additionally, it clears lists of the value Missing, since doing this at the class level SGen is inconvenient

set_positive_values()
Return type:

None

Registers a set of positive values for a type Collection

set_negative_values()
Return type:

None

Registers a set of negative values for a type Collection

generate(length: int)
Parameters:

length (int) – Length of the generated collection

Return type:

List[Any]

Generates a collection

get_other_value(value: list | None)
Parameters:

value (list) – Undesired number value

Return type:

list

Returns a value of type list that is not equal to value

Class Collection is a list representation

Example:

from sgen import fields, SGen, validate


class Storage(SGen)
    user_ids = fields.Collection(
        data_type=fields.Integer()
    )
class Nested
__init__(data_type: 'SGen', *args, **kwargs)
Parameters:

data_type (SGen) – Schema data type

set_positive_values()
Returns:

None

Registers a set of positive values for a type Nested

set_negative_values()
Returns:

None

Registers a set of negative values for a type Nested

Class Nested is an implementation of nested entities

Example:

from sgen import fields, SGen, validate

class Wallet(SGen):
    currency = fields.String(
        validate=validate.OneOf(choices=['RUB', 'EU', '$']),
        required=True,
        allow_none=False,
    )
    amount = fields.Integer(
        validate=validate.Range(min=0),
        required=True,
        allow_none=False,
    )

class User(SGen):
    wallet = fields.Nested(data_type=Wallet(), required=True)

Validators

class Length
__init__(min: int = None, max: int = None, min_inclusive: bool = True, max_inclusive: bool = True)
Parameters:
  • min (int) – Minimum length

  • max (int) – Maximum length

  • min_inclusive (bool) – True, if you need to include min in the range of valid length values

  • max_inclusive (bool) – True, if you need to include max in the range of valid length values

positive(data_type: Field)
Parameters:

data_type (Field) – Type of data to be validated.

Return type:

List[Any]

Generates a positive data set according to the validation parameters.

negative(data_type: Field)
Parameters:

data_type (Field) – Type of data to be validated.

Return type:

List[Any]

Generates a negative data set according to the validation parameters.

Represents a collection or string length validator.

Example:

from sgen import fields, SGen, validate


class User(SGen):
    name = fields.String(
        validate=validate.Length(min=1, max=10)
    )

Note

For the validator to work correctly, the data type must implement the method generate of class Field

class Range
__init__(min: int = None, max: int = None, min_inclusive: bool = True, max_inclusive: bool = True)
Parameters:
  • min (int) – Minimum range limit

  • max (int) – Maximum range limit

  • min_inclusive (bool) – True, if you need to include min in the range of acceptable values

  • max_inclusive (bool) – True, if you need to include max in the range of acceptable values

_get_min(data_type: Field, positive: bool = True)
Parameters:
  • data_type (Field) – Data type.

  • positive (bool) – Positive or negative meaning.

Returns:

Minimum range limit.

Returns the minimum limit of a range

_get_max(data_type: Field, positive: bool = True)
Parameters:
  • data_type (Field) – Data type.

  • positive (bool) – Positive or negative meaning.

Returns:

Maximum range limit.

Returns the maximum limit of a range

positive(data_type: Field)
Parameters:

data_type (Field) – Type of data to be validated.

Return type:

List[Any]

Generates a positive data set according to the validation parameters.

negative(data_type: Field)
Parameters:

data_type (Field) – Type of data to be validated.

Return type:

List[Any]

Generates a negative data set according to the validation parameters.

Represents a number value range validator.

Example:

from sgen import fields, SGen, validate


class User(SGen):
    age = fields.Integer(
        validate=validate.Range(min=18, max=100)
    )

Note

For the validator to work correctly, the data type must implement the method get_step of class Field

class Equal
__init__(comparable: Any)
Parameters:

comparable (Any) – The value that a field will take in a positive data set

positive(data_type: Field)
Parameters:

data_type (Field) – Type of data to be validated.

Return type:

List[Any]

Generates a positive data set according to the validation parameters.

negative(data_type: Field)
Parameters:

data_type (Field) – Type of data to be validated.

Return type:

List[Any]

Generates a negative data set according to the validation parameters.

Represents an equality validator

Example:

from sgen import fields, SGen, validate


class User(SGen):
    age = fields.Integer(
        validate=validate.Equal(comparable=999)
    )

Note

For the validator to work correctly, the data type must implement the method get_other_value of class Field

class OneOf
__init__(choices: List[Any])
Parameters:

choices (List[Any]) – List of valid field values

positive(data_type: Field)
Parameters:

data_type (Field) – Type of data to be validated.

Return type:

List[Any]

Generates a positive data set according to the validation parameters.

negative(data_type: Field)
Parameters:

data_type (Field) – Type of data to be validated.

Return type:

List[Any]

Generates a negative data set according to the validation parameters.

Represents a validator for selecting a valid value from a list

Example:

from sgen import fields, SGen, validate


class User(SGen):
    age = fields.String(
        validate=validate.OneOf(choices=['Pepega', 'Aboba', 'PSFP5'])
    )

Note

For the validator to work correctly, the data type must implement the method get_other_value of class Field

class NoneOf
__init__(invalid_values: List[Any])
Parameters:

invalid_values (List[Any]) – List of not valid field values

positive(data_type: Field)
Parameters:

data_type (Field) – Type of data to be validated.

Return type:

List[Any]

Generates a positive data set according to the validation parameters.

negative(data_type: Field)
Parameters:

data_type (Field) – Type of data to be validated.

Return type:

List[Any]

Generates a negative data set according to the validation parameters.

Allows you to specify not valid values for a field

Example:

from sgen import fields, SGen, validate


class User(SGen):
    is_admin = fields.Bool(
        validate=validate.NoneOf(invalid_values=[False])
    )

Note

For the validator to work correctly, the data type must implement the method get_other_value of class Field