How to use the typesystem.fields.Integer function in typesystem

To help you get started, we’ve selected a few typesystem examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github encode / typesystem / tests / test_fields.py View on Github external
validator = Integer(maximum=10)
    value, error = validator.validate_or_error(10)
    assert value == 10

    validator = Integer(minimum=3)
    value, error = validator.validate_or_error(1)
    assert error == ValidationError(
        text="Must be greater than or equal to 3.", code="minimum"
    )

    validator = Integer(minimum=3)
    value, error = validator.validate_or_error(3)
    assert value == 3

    validator = Integer(exclusive_maximum=10)
    value, error = validator.validate_or_error(10)
    assert error == ValidationError(
        text="Must be less than 10.", code="exclusive_maximum"
    )

    validator = Integer(exclusive_minimum=3)
    value, error = validator.validate_or_error(3)
    assert error == ValidationError(
        text="Must be greater than 3.", code="exclusive_minimum"
    )

    validator = Integer(multiple_of=10)
    value, error = validator.validate_or_error(5)
    assert error == ValidationError(
        text="Must be a multiple of 10.", code="multiple_of"
    )
github encode / typesystem / tests / test_fields.py View on Github external
value, error = validator.validate_or_error({})
    assert value == {}

    validator = Object(max_properties=2)
    value, error = validator.validate_or_error({"a": 1, "b": 2, "c": 3})
    assert dict(error) == {"": "Must have no more than 2 properties."}

    validator = Object(required=["example"])
    value, error = validator.validate_or_error({"example": 123})
    assert value == {"example": 123}

    validator = Object(required=["example"])
    value, error = validator.validate_or_error({})
    assert dict(error) == {"example": "This field is required."}

    validator = Object(properties={"example": Integer()})
    value, error = validator.validate_or_error({"example": "123"})
    assert value == {"example": 123}

    validator = Object(properties={"example": Integer()})
    value, error = validator.validate_or_error({"example": "abc"})
    assert dict(error) == {"example": "Must be a number."}

    validator = Object(pattern_properties={"^x-.*$": Integer()})
    value, error = validator.validate_or_error({"x-example": "123"})
    assert value == {"x-example": 123}

    validator = Object(pattern_properties={"^x-.*$": Integer()})
    value, error = validator.validate_or_error({"x-example": "abc"})
    assert dict(error) == {"x-example": "Must be a number."}

    validator = Object(properties={"example": Integer(default=0)})
github encode / typesystem / tests / test_fields.py View on Github external
value, error = validator.validate_or_error({"example": "123"})
    assert value == {"example": 123}

    validator = Object(properties=Integer())
    value, error = validator.validate_or_error({"example": "abc"})
    assert dict(error) == {"example": "Must be a number."}

    validator = Object(properties={"example": Integer()})
    value, error = validator.validate_or_error({"example": "123"})
    assert value == {"example": 123}

    validator = Object(properties={"example": Integer()})
    value, error = validator.validate_or_error({"example": "abc"})
    assert dict(error) == {"example": "Must be a number."}

    validator = Object(additional_properties=Object(additional_properties=Integer()))
    value, error = validator.validate_or_error({"example": {"nested": "123"}})
    assert value == {"example": {"nested": 123}}

    validator = Object(additional_properties=Object(additional_properties=Integer()))
    value, error = validator.validate_or_error({"example": {"nested": "abc"}})
    assert dict(error) == {"example": {"nested": "Must be a number."}}
github encode / typesystem / tests / test_fields.py View on Github external
value, error = validator.validate_or_error(123.1)
    assert error == ValidationError(text="Must be an integer.", code="integer")

    validator = Integer()
    value, error = validator.validate_or_error(float("inf"))
    assert error == ValidationError(text="Must be an integer.", code="integer")

    validator = Integer()
    value, error = validator.validate_or_error(float("nan"))
    assert error == ValidationError(text="Must be an integer.", code="integer")

    validator = Integer()
    value, error = validator.validate_or_error("123", strict=True)
    assert error == ValidationError(text="Must be a number.", code="type")

    validator = Integer(allow_null=True)
    value, error = validator.validate_or_error(None)
    assert value is None
    assert error is None

    validator = Integer(allow_null=True)
    value, error = validator.validate_or_error("")
    assert value is None
    assert error is None

    validator = Integer(allow_null=True)
    value, error = validator.validate_or_error("", strict=True)
    assert error == ValidationError(text="Must be a number.", code="type")

    validator = Integer(maximum=10)
    value, error = validator.validate_or_error(100)
    assert error == ValidationError(
github encode / typesystem / tests / test_fields.py View on Github external
assert error is None
    assert value is None

    validator = Union(any_of=[Integer(), String()], allow_null=True)
    value, error = validator.validate_or_error(None)
    assert error is None
    assert value is None

    validator = Union(any_of=[Integer(maximum=1000), String()])
    value, error = validator.validate_or_error(9999)
    assert value is None
    assert error == ValidationError(
        text="Must be less than or equal to 1000.", code="maximum"
    )

    validator = Integer() | String() | Boolean()
    value, error = validator.validate_or_error(123)
    assert value == 123

    validator = Integer() | (String() | Boolean())
    value, error = validator.validate_or_error(123)
    assert value == 123
github encode / typesystem / tests / test_fields.py View on Github external
def test_error_messages_interface():
    """
    `errors.messages()` should return a list of Message instances.
    """
    validator = Integer()
    value, error = validator.validate_or_error("abc")
    assert error.messages() == [Message(text="Must be a number.", code="type")]
github encode / typesystem / tests / test_fields.py View on Github external
value, error = validator.validate_or_error(True)
    assert error == ValidationError(text="Must be a number.", code="type")

    validator = Integer()
    value, error = validator.validate_or_error(123.1)
    assert error == ValidationError(text="Must be an integer.", code="integer")

    validator = Integer()
    value, error = validator.validate_or_error(float("inf"))
    assert error == ValidationError(text="Must be an integer.", code="integer")

    validator = Integer()
    value, error = validator.validate_or_error(float("nan"))
    assert error == ValidationError(text="Must be an integer.", code="integer")

    validator = Integer()
    value, error = validator.validate_or_error("123", strict=True)
    assert error == ValidationError(text="Must be a number.", code="type")

    validator = Integer(allow_null=True)
    value, error = validator.validate_or_error(None)
    assert value is None
    assert error is None

    validator = Integer(allow_null=True)
    value, error = validator.validate_or_error("")
    assert value is None
    assert error is None

    validator = Integer(allow_null=True)
    value, error = validator.validate_or_error("", strict=True)
    assert error == ValidationError(text="Must be a number.", code="type")
github encode / typesystem / typesystem / json_schema.py View on Github external
definitions = SchemaDefinitions()

JSONSchema = (
    Object(
        properties={
            "$ref": String(),
            "type": String() | Array(items=String()),
            "enum": Array(unique_items=True, min_items=1),
            "definitions": Object(
                additional_properties=Reference("JSONSchema", definitions=definitions)
            ),
            # String
            "minLength": Integer(minimum=0),
            "maxLength": Integer(minimum=0),
            "pattern": String(format="regex"),
            "format": String(),
            # Numeric
            "minimum": Number(),
            "maximum": Number(),
            "exclusiveMinimum": Number(),
            "exclusiveMaximum": Number(),
            "multipleOf": Number(exclusive_minimum=0),
            # Object
            "properties": Object(
                additional_properties=Reference("JSONSchema", definitions=definitions)
            ),
            "minProperties": Integer(minimum=0),
            "maxProperties": Integer(minimum=0),
            "patternProperties": Object(
                additional_properties=Reference("JSONSchema", definitions=definitions)
github encode / typesystem / typesystem / json_schema.py View on Github external
"minLength": Integer(minimum=0),
            "maxLength": Integer(minimum=0),
            "pattern": String(format="regex"),
            "format": String(),
            # Numeric
            "minimum": Number(),
            "maximum": Number(),
            "exclusiveMinimum": Number(),
            "exclusiveMaximum": Number(),
            "multipleOf": Number(exclusive_minimum=0),
            # Object
            "properties": Object(
                additional_properties=Reference("JSONSchema", definitions=definitions)
            ),
            "minProperties": Integer(minimum=0),
            "maxProperties": Integer(minimum=0),
            "patternProperties": Object(
                additional_properties=Reference("JSONSchema", definitions=definitions)
            ),
            "additionalProperties": (
                Reference("JSONSchema", definitions=definitions) | Boolean()
            ),
            "required": Array(items=String(), unique_items=True),
            # Array
            "items": (
                Reference("JSONSchema", definitions=definitions)
                | Array(
                    items=Reference("JSONSchema", definitions=definitions), min_items=1
                )
            ),
            "additionalItems": (
                Reference("JSONSchema", definitions=definitions) | Boolean()