Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_schema_positional_array_serialization():
class NumberName(typesystem.Schema):
pair = typesystem.Array([typesystem.Integer(), typesystem.String()])
name = NumberName(pair=[1, "one"])
data = dict(name)
assert data == {"pair": [1, "one"]}
def test_schema_null_items_array_serialization():
class Product(typesystem.Schema):
names = typesystem.Array()
tshirt = Product(names=[1, "2", {"nested": 3}])
data = dict(tshirt)
assert data == {"names": [1, "2", {"nested": 3}]}
def test_nested_schema_array():
class Artist(typesystem.Schema):
name = typesystem.String(max_length=100)
class Album(typesystem.Schema):
title = typesystem.String(max_length=100)
release_year = typesystem.Integer()
artists = typesystem.Array(items=typesystem.Reference(Artist))
value = Album.validate(
{
"title": "Double Negative",
"release_year": "2018",
"artists": [{"name": "Low"}],
}
)
assert dict(value) == {
"title": "Double Negative",
"release_year": 2018,
"artists": [{"name": "Low"}],
}
assert value == Album(
title="Double Negative", release_year=2018, artists=[Artist(name="Low")]
)
class ExampleC(typesystem.Schema, definitions=definitions):
field_on_c = typesystem.Integer()
example_d = typesystem.Array(items=typesystem.Reference("ExampleD"))
class ExampleD(typesystem.Schema, definitions=definitions):
field_on_d = typesystem.Integer()
value = ExampleC.validate(
{"field_on_c": "123", "example_d": [{"field_on_d": "456"}]}
)
assert value == ExampleC(field_on_c=123, example_d=[ExampleD(field_on_d=456)])
class ExampleE(typesystem.Schema, definitions=definitions):
field_on_e = typesystem.Integer()
example_f = typesystem.Array(items=[typesystem.Reference("ExampleF")])
class ExampleF(typesystem.Schema, definitions=definitions):
field_on_f = typesystem.Integer()
value = ExampleE.validate(
{"field_on_e": "123", "example_f": [{"field_on_f": "456"}]}
)
assert value == ExampleE(field_on_e=123, example_f=[ExampleF(field_on_f=456)])
class ExampleG(typesystem.Schema, definitions=definitions):
field_on_g = typesystem.Integer()
example_h = typesystem.Object(
properties={"h": typesystem.Reference("ExampleH")}
)
class ExampleH(typesystem.Schema, definitions=definitions):
definitions["Tag"] = typesystem.Object(
properties={
"name": typesystem.String(),
"description": typesystem.Text(allow_blank=True),
"externalDocs": typesystem.Reference(
"ExternalDocumentation", definitions=definitions
),
},
pattern_properties={"^x-": typesystem.Any()},
additional_properties=False,
required=["name"],
)
definitions["SecurityRequirement"] = typesystem.Object(
additional_properties=typesystem.Array(items=typesystem.String())
)
definitions["SecurityScheme"] = typesystem.Object(
properties={
"type": typesystem.Choice(choices=["basic", "apiKey", "oauth2"]),
"description": typesystem.Text(allow_blank=True),
"name": typesystem.String(),
"in": typesystem.Choice(choices=["query", "header"]),
"flow": typesystem.Choice(
choices=["implicit", "password", "application", "accessCode"]
),
"authorizationUrl": typesystem.String(format="url"),
"tokenUrl": typesystem.String(format="url"),
"scopes": typesystem.Reference("Scopes", definitions=definitions),
},
pattern_properties={"^x-": typesystem.Any()},
def determine_column_types(rows):
identities = determine_column_identities(rows)
candidate_types = [("integer", typesystem.Integer(allow_null=True))]
column_types = []
fields = {}
for idx, identity in enumerate(identities):
column = [row[idx] for row in rows[1:] if row[idx]]
for name, candidate in candidate_types:
list_validator = typesystem.Array(items=candidate)
validated, errors = list_validator.validate_or_error(column)
if not errors:
column_types.append(name)
fields[identity] = candidate
break
else:
column_types.append("string")
fields[identity] = candidate = typesystem.String(allow_blank=True)
schema = type("Schema", (typesystem.Schema,), fields)
return column_types, schema
"minProperties": typesystem.Integer(minimum=0),
"maxProperties": typesystem.Integer(minimum=0),
"patternProperties": typesystem.Object(
additional_properties=typesystem.Reference(
"JSONSchema", definitions=definitions
)
),
"additionalProperties": (
typesystem.Reference("JSONSchema", definitions=definitions)
| typesystem.Boolean()
),
"required": typesystem.Array(items=typesystem.String(), unique_items=True),
# Array
"items": (
typesystem.Reference("JSONSchema", definitions=definitions)
| typesystem.Array(
items=typesystem.Reference("JSONSchema", definitions=definitions),
min_items=1,
)
),
"additionalItems": (
typesystem.Reference("JSONSchema", definitions=definitions)
| typesystem.Boolean()
),
"minItems": typesystem.Integer(minimum=0),
"maxItems": typesystem.Integer(minimum=0),
"uniqueItems": typesystem.Boolean(),
}
)
| typesystem.Boolean()
)
additional_properties=False,
)
definitions["Path"] = typesystem.Object(
properties={
"summary": typesystem.String(allow_blank=True),
"description": typesystem.Text(allow_blank=True),
"get": typesystem.Reference("Operation", definitions=definitions),
"put": typesystem.Reference("Operation", definitions=definitions),
"post": typesystem.Reference("Operation", definitions=definitions),
"delete": typesystem.Reference("Operation", definitions=definitions),
"options": typesystem.Reference("Operation", definitions=definitions),
"head": typesystem.Reference("Operation", definitions=definitions),
"patch": typesystem.Reference("Operation", definitions=definitions),
"trace": typesystem.Reference("Operation", definitions=definitions),
"parameters": typesystem.Array(
items=typesystem.Reference("Parameter", definitions=definitions)
), # TODO: | ReferenceObject
},
pattern_properties={"^x-": typesystem.Any()},
additional_properties=False,
)
definitions["Operation"] = typesystem.Object(
properties={
"tags": typesystem.Array(items=typesystem.String()),
"summary": typesystem.String(allow_blank=True),
"description": typesystem.Text(allow_blank=True),
"externalDocs": typesystem.Reference(
"ExternalDocumentation", definitions=definitions
),
"operationId": typesystem.String(),
import typesystem
definitions = typesystem.SchemaDefinitions()
JSON_SCHEMA = (
typesystem.Object(
properties={
"$ref": typesystem.String(),
"type": typesystem.String() | typesystem.Array(items=typesystem.String()),
"enum": typesystem.Array(unique_items=True, min_items=1),
"definitions": typesystem.Object(
additional_properties=typesystem.Reference(
"JSONSchema", definitions=definitions
)
),
# String
"minLength": typesystem.Integer(minimum=0),
"maxLength": typesystem.Integer(minimum=0),
"pattern": typesystem.String(format="regex"),
"format": typesystem.String(),
# Numeric
"minimum": typesystem.Number(),
"maximum": typesystem.Number(),
"exclusiveMinimum": typesystem.Number(),
"exclusiveMaximum": typesystem.Number(),
"multipleOf": typesystem.Number(exclusive_minimum=0),