Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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"])
def create_booking(booking: Booking = Body(source="_data.item")):
import jinja2
import typesystem
class Contact(typesystem.Schema):
a = typesystem.Boolean()
b = typesystem.String(max_length=10)
c = typesystem.Text()
d = typesystem.Choice(choices=[("abc", "Abc"), ("def", "Def"), ("ghi", "Ghi")])
forms = typesystem.Jinja2Forms(package="typesystem")
def test_form_rendering():
form = forms.Form(Contact)
html = str(form)
assert html.count('
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},
"end_date": {"type": "string", "format": "date", "minLength": 1},
ensure_string(key): redis_to_value(cls.fields[ensure_string(key)], value)
for key, value in redis_data.items()
}
return cls.validate(data)
def to_redis(self):
return {
ensure_string(key): value_to_redis(self.fields[key], value)
for key, value in self.items()
if value is not None
}
class Settings(BaseSchema):
enforce_https = typesystem.Boolean(default=False)
mode = typesystem.Choice(
choices=(
("proxy", "Proxy"),
("redirect", "Redirect"),
),
default="proxy",
)
certificate_path = typesystem.String(allow_null=True)
key_path = typesystem.String(allow_null=True)
class Route(BaseSchema):
DEFAULT_SETTINGS = dict(Settings.validate({}))
source = typesystem.String()
target = typesystem.String()
settings = typesystem.Reference(Settings, default=DEFAULT_SETTINGS)
)
RESPONSE_REF = typesystem.Object(
properties={"$ref": typesystem.String(pattern="^#/responses/")}
)
definitions = typesystem.SchemaDefinitions()
SWAGGER = typesystem.Object(
title="Swagger",
properties={
"swagger": typesystem.String(),
"info": typesystem.Reference("Info", definitions=definitions),
"paths": typesystem.Reference("Paths", definitions=definitions),
"host": typesystem.String(),
"basePath": typesystem.String(pattern="^/"),
"schemes": typesystem.Array(items=typesystem.Choice(choices=["http", "https", "ws", "wss"])),
"consumes": typesystem.Array(items=typesystem.String()),
"produces": typesystem.Array(items=typesystem.String()),
"definitions": typesystem.Object(additional_properties=typesystem.Any()),
"parameters": typesystem.Object(
additional_properties=typesystem.Reference(
"Parameters", definitions=definitions
)
),
"responses": typesystem.Object(
additional_properties=typesystem.Reference(
"Responses", definitions=definitions
)
),
"securityDefinitions": typesystem.Object(
additional_properties=typesystem.Reference(
"SecurityScheme", definitions=definitions
APISTAR_CONFIG = typesystem.Object(
properties={
"schema": typesystem.Object(
properties={
"path": typesystem.String(),
"format": typesystem.Choice(choices=["openapi", "swagger"]),
"base_format": typesystem.Choice(choices=["json", "yaml"]),
},
additional_properties=False,
required=["path", "format"],
),
"docs": typesystem.Object(
properties={
"output_dir": typesystem.String(),
"theme": typesystem.Choice(choices=["apistar", "redoc", "swaggerui"]),
},
additional_properties=False,
),
},
additional_properties=False,
required=["schema"],
)
"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=["apiKey", "http", "oauth2", "openIdConnect"]
),
"description": typesystem.Text(allow_blank=True),
"name": typesystem.String(),
"in": typesystem.Choice(choices=["query", "header", "cookie"]),
"scheme": typesystem.String(),
"bearerFormat": typesystem.String(),
"flows": typesystem.Any(), # TODO: OAuthFlows
"openIdConnectUrl": typesystem.String(format="url"),
},
pattern_properties={"^x-": typesystem.Any()},
additional_properties=False,
required=["type"],
)
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(),
"consumes": typesystem.Array(items=typesystem.String()),
"produces": typesystem.Array(items=typesystem.String()),
"parameters": typesystem.Array(
items=typesystem.Reference("Parameter", definitions=definitions)
), # TODO: | ReferenceObject
"responses": typesystem.Reference("Responses", definitions=definitions),
"schemes": typesystem.Array(items=typesystem.Choice(choices=["http", "https", "ws", "wss"])),
"deprecated": typesystem.Boolean(),
"security": typesystem.Array(
typesystem.Reference("SecurityRequirement", definitions=definitions)
),
},
pattern_properties={"^x-": typesystem.Any()},
additional_properties=False,
)
definitions["ExternalDocumentation"] = typesystem.Object(
properties={
"description": typesystem.Text(),
"url": typesystem.String(format="url"),
},
pattern_properties={"^x-": typesystem.Any()},
additional_properties=False,