How to use the typesystem.fields.Array 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
value, error = validator.validate_or_error(["1", 2, "3"])
    assert value == [1, 2, 3]

    validator = Array(items=Integer())
    value, error = validator.validate_or_error(["a", 2, "c"])
    assert dict(error) == {0: "Must be a number.", 2: "Must be a number."}

    validator = Array(items=[String(), Integer()])
    value, error = validator.validate_or_error(["a", "b", "c"])
    assert error == ValidationError(text="Must have 2 items.", code="exact_items")

    validator = Array(items=[String(), Integer()])
    value, error = validator.validate_or_error(["a", "123"])
    assert value == ["a", 123]

    validator = Array(items=[String(), Integer()], additional_items=Integer())
    value, error = validator.validate_or_error(["a", "123", "456"])
    assert value == ["a", 123, 456]

    validator = Array(items=String(), unique_items=True)
    value, error = validator.validate_or_error(["a", "b"])
    assert value == ["a", "b"]

    validator = Array(items=String(), unique_items=True)
    value, error = validator.validate_or_error(["a", "a"])
    assert dict(error) == {1: "Items must be unique."}

    validator = Array(items=[String(), Integer(), Boolean()], min_items=1)
    value, error = validator.validate_or_error(["a"])
    assert value == ["a"]

    validator = Array(items=[String(), Integer(), Boolean()], min_items=1)
github encode / typesystem / tests / test_fields.py View on Github external
assert error == ValidationError(text="May not be null.", code="null")

    validator = Array()
    value, error = validator.validate_or_error(123)
    assert error == ValidationError(text="Must be an array.", code="type")

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

    validator = Array(min_items=1)
    value, error = validator.validate_or_error([])
    assert error == ValidationError(text="Must not be empty.", code="empty")

    validator = Array(min_items=1)
    value, error = validator.validate_or_error([1])
    assert value == [1]

    validator = Array(min_items=2)
    value, error = validator.validate_or_error([])
    assert error == ValidationError(
        text="Must have at least 2 items.", code="min_items"
    )

    validator = Array(min_items=2)
    value, error = validator.validate_or_error([1, 2])
    assert value == [1, 2]

    validator = Array(max_items=2)
    value, error = validator.validate_or_error([])
    assert value == []
github encode / typesystem / tests / test_fields.py View on Github external
value, error = validator.validate_or_error(["a", "123"])
    assert value == ["a", 123]

    validator = Array(items=[String(), Integer()], additional_items=Integer())
    value, error = validator.validate_or_error(["a", "123", "456"])
    assert value == ["a", 123, 456]

    validator = Array(items=String(), unique_items=True)
    value, error = validator.validate_or_error(["a", "b"])
    assert value == ["a", "b"]

    validator = Array(items=String(), unique_items=True)
    value, error = validator.validate_or_error(["a", "a"])
    assert dict(error) == {1: "Items must be unique."}

    validator = Array(items=[String(), Integer(), Boolean()], min_items=1)
    value, error = validator.validate_or_error(["a"])
    assert value == ["a"]

    validator = Array(items=[String(), Integer(), Boolean()], min_items=1)
    value, error = validator.validate_or_error(["a", "123"])
    assert value == ["a", 123]

    validator = Array(items=[String(), Integer(), Boolean()], min_items=1)
    value, error = validator.validate_or_error([])
    assert error == ValidationError(text="Must not be empty.", code="empty")

    validator = Array(items=[String(), Integer(), Boolean()], min_items=2)
    value, error = validator.validate_or_error([])
    assert error == ValidationError(
        text="Must have at least 2 items.", code="min_items"
    )
github encode / typesystem / tests / test_fields.py View on Github external
def test_array():
    validator = Array()
    value, error = validator.validate_or_error([])
    assert value == []

    validator = Array()
    value, error = validator.validate_or_error(None)
    assert error == ValidationError(text="May not be null.", code="null")

    validator = Array()
    value, error = validator.validate_or_error(123)
    assert error == ValidationError(text="Must be an array.", code="type")

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

    validator = Array(min_items=1)
    value, error = validator.validate_or_error([])
    assert error == ValidationError(text="Must not be empty.", code="empty")

    validator = Array(min_items=1)
    value, error = validator.validate_or_error([1])
    assert value == [1]

    validator = Array(min_items=2)
    value, error = validator.validate_or_error([])
    assert error == ValidationError(
        text="Must have at least 2 items.", code="min_items"
github encode / typesystem / typesystem / json_schema.py View on Github external
"patternProperties",
    "properties",
    "propertyNames",
    "required",
    "type",
    "uniqueItems",
}


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
github encode / typesystem / typesystem / json_schema.py View on Github external
"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()
            ),
            "minItems": Integer(minimum=0),
            "maxItems": Integer(minimum=0),
            "uniqueItems": Boolean(),
        }
    )
    | Boolean()
)

definitions["JSONSchema"] = JSONSchema
github encode / typesystem / typesystem / json_schema.py View on Github external
"properties",
    "propertyNames",
    "required",
    "type",
    "uniqueItems",
}


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(
github encode / typesystem / typesystem / json_schema.py View on Github external
"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()
            ),
            "minItems": Integer(minimum=0),
            "maxItems": Integer(minimum=0),
            "uniqueItems": Boolean(),
        }
    )
    | Boolean()
github encode / typesystem / typesystem / schemas.py View on Github external
def set_definitions(field: Field, definitions: SchemaDefinitions) -> None:
    """
    Recursively set the definitions that string-referenced `Reference` fields
    should use.
    """
    if isinstance(field, Reference) and field.definitions is None:
        field.definitions = definitions
    elif isinstance(field, Array):
        if field.items is not None:
            if isinstance(field.items, (tuple, list)):
                for child in field.items:
                    set_definitions(child, definitions)
            else:
                set_definitions(field.items, definitions)
    elif isinstance(field, Object):
        for child in field.properties.values():
            set_definitions(child, definitions)