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_dates_array_serialization():
class BlogPost(typesystem.Schema):
text = typesystem.String()
modified = typesystem.Array(typesystem.Date())
post = BlogPost(text="Hi", modified=[datetime.date.today()])
data = dict(post)
assert data["text"] == "Hi"
assert data["modified"] == [datetime.date.today().isoformat()]
def test_schema_to_json_schema():
class BookingSchema(typesystem.Schema):
start_date = typesystem.Date(title="Start date")
end_date = typesystem.Date(title="End date")
room = typesystem.Choice(
title="Room type",
choices=[
("double", "Double room"),
("twin", "Twin room"),
("single", "Single room"),
],
)
include_breakfast = typesystem.Boolean(title="Include breakfast", default=False)
schema = to_json_schema(BookingSchema)
assert schema == {
"type": "object",
"properties": {
def test_schema_to_json_schema():
class BookingSchema(typesystem.Schema):
start_date = typesystem.Date(title="Start date")
end_date = typesystem.Date(title="End date")
room = typesystem.Choice(
title="Room type",
choices=[
("double", "Double room"),
("twin", "Twin room"),
("single", "Single room"),
],
)
include_breakfast = typesystem.Boolean(title="Include breakfast", default=False)
schema = to_json_schema(BookingSchema)
assert schema == {
"type": "object",
"properties": {
"start_date": {"type": "string", "format": "date", "minLength": 1},
def test_definitions_as_mapping():
"""
Ensure that definitions support a mapping interface.
"""
definitions = typesystem.SchemaDefinitions()
class Album(typesystem.Schema, definitions=definitions):
title = typesystem.String(max_length=100)
release_date = typesystem.Date()
artist = typesystem.Reference("Artist")
class Artist(typesystem.Schema, definitions=definitions):
name = typesystem.String(max_length=100)
assert definitions["Album"] == Album
assert definitions["Artist"] == Artist
assert dict(definitions) == {"Album": Album, "Artist": Artist}
assert len(definitions) == 2
del definitions["Artist"]
def test_reference():
definitions = typesystem.SchemaDefinitions()
class Album(typesystem.Schema, definitions=definitions):
title = typesystem.String(max_length=100)
release_date = typesystem.Date()
artist = typesystem.Reference("Artist")
class Artist(typesystem.Schema, definitions=definitions):
name = typesystem.String(max_length=100)
album = Album.validate(
{
"title": "Double Negative",
"release_date": "2018-09-14",
"artist": {"name": "Low"},
}
)
assert album == Album(
title="Double Negative",
release_date=datetime.date(2018, 9, 14),
artist=Artist(name="Low"),
def test_schema_with_callable_default():
class Example(typesystem.Schema):
created = typesystem.Date(default=datetime.date.today)
value, error = Example.validate_or_error({})
assert value.created == datetime.date.today()
def test_nested_schema_to_json_schema():
class Artist(typesystem.Schema):
name = typesystem.String(max_length=100)
class Album(typesystem.Schema):
title = typesystem.String(max_length=100)
release_date = typesystem.Date()
artist = typesystem.Reference(Artist)
schema = typesystem.to_json_schema(Album)
assert schema == {
"type": "object",
"properties": {
"title": {"type": "string", "minLength": 1, "maxLength": 100},
"release_date": {"type": "string", "minLength": 1, "format": "date"},
"artist": {"$ref": "#/definitions/Artist"},
},
"required": ["title", "release_date", "artist"],
"definitions": {
"Artist": {
"type": "object",
"properties": {
def test_definitions_to_json_schema():
definitions = typesystem.SchemaDefinitions()
class Artist(typesystem.Schema, definitions=definitions):
name = typesystem.String(max_length=100)
class Album(typesystem.Schema, definitions=definitions):
title = typesystem.String(max_length=100)
release_date = typesystem.Date()
artist = typesystem.Reference("Artist")
schema = typesystem.to_json_schema(definitions)
assert schema == {
"definitions": {
"Artist": {
"type": "object",
"properties": {
"name": {"type": "string", "minLength": 1, "maxLength": 100}
},
"required": ["name"],
},
"Album": {
"type": "object",
"properties": {
class BagOptions(str, Enum):
paper = "paper"
plastic = "plastic"
class SuperUser(BaseModel):
id: int
name = "John Doe"
signup_ts: datetime = None
friends: List[int] = []
class Booking(typesystem.Schema):
start_date = typesystem.Date()
end_date = typesystem.Date()
room = typesystem.Choice(
choices=[
("double", "Double room"),
("twin", "Twin room"),
("single", "Single room"),
]
)
include_breakfast = typesystem.Boolean(title="Include breakfast", default=False)
@typed_api_view(["POST"])
def create_user(user: SuperUser):
return Response(dict(user))
@typed_api_view(["POST"])
import decimal
import inspect
from datetime import date, datetime, time
from functools import wraps
import typing
import typesystem
FIELD_ALIASES: typing.Dict[typing.Type, typesystem.Field] = {
int: typesystem.Integer,
float: typesystem.Float,
bool: typesystem.Boolean,
decimal.Decimal: typesystem.Decimal,
date: typesystem.Date,
time: typesystem.Time,
datetime: typesystem.DateTime,
}
class PathConversionError(typesystem.ValidationError):
pass
class Converter:
__slots__ = ("func", "signature", "annotations", "required_params")
def __init__(self, func: typing.Callable):
self.func = func
self.signature = inspect.signature(self.func)