Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import pytest
import typesystem
from bocadillo import configure, create_client
class Todo(typesystem.Schema):
title = typesystem.String(max_length=50)
done = typesystem.Boolean(default=False)
def test_handle_validation_errors(app, client):
@app.route("/todos")
class TodoList:
async def post(self, req, res):
todo = Todo.validate(await req.json())
res.json = dict(todo)
r = client.post("/todos", json={"title": "Make sugar"})
assert r.status_code == 200
assert r.json() == {"title": "Make sugar", "done": False}
r = client.post("/todos", json={"title": "Very long string" * 10})
assert r.status_code == 400
assert r.json()["detail"] == {
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")):
return Response(dict(booking))
class BandMemberSchema(marshmallow.Schema):
name = marshmallow.fields.String(required=True)
email = marshmallow.fields.Email()
"""
import typesystem
class TriggerSchema(typesystem.Schema):
"""
Schema to define the structure of a Trigger
"""
description = typesystem.String(title="Description", max_length=200)
rss_url = typesystem.String(title="RSS URL", max_length=255)
joplin_folder = typesystem.String(title="Joplin Folder", max_length=80, allow_blank=True)
reddit = typesystem.String(title="Subreddit", max_length=80, allow_blank=True)
localstorage = typesystem.String(title="Markdown Folder", allow_blank=True)
mail = typesystem.Boolean(title="Send a mail ?", default=False)
mastodon = typesystem.Boolean(title="Publish on Mastodon ?", default=False)
status = typesystem.Boolean(title="Status", default=False)
data = {
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()
def redis_to_value(field, redis_value):
if isinstance(field, typesystem.Boolean):
return redis_to_boolean(redis_value)
if isinstance(field, typesystem.Reference):
return field.target.from_redis(redis_value)
return ensure_string(redis_value)
def supported(self):
return False
def isinstance(self, datum):
return False
def isdataset(self, data):
return False
class Null(NumpySchema, typesystem.Null):
def supported(self):
return False
def isinstance(self, datum):
return False
def isdataset(self, data):
return False
class Boolean(NumpySchema, typesystem.Boolean):
def supported(self):
return True
def isinstance(self, datum):
if not isinstance(datum, numpy.generic):
# if not Numpy, check as pure Python
return typesystem.Boolean().isinstance(datum)
return isinstance(datum, numpy.bool_)
def isdataset(self, data):
if isinstance(data, numpy.ndarray):
return issubclass(data.dtype.type, numpy.bool_)
else:
return False
class Number(NumpySchema, typesystem.Number):
def supported(self):
if not self.whole and not self.signed:
def isinstance(self, datum):
if not isinstance(datum, numpy.generic):
# if not Numpy, check as pure Python
return typesystem.Boolean().isinstance(datum)
return isinstance(datum, numpy.bool_)
def isdataset(self, data):
class TriggerSchema(typesystem.Schema):
"""
Schema to define the structure of a Trigger
"""
description = typesystem.String(title="Description", max_length=200)
rss_url = typesystem.String(title="RSS URL", max_length=255)
joplin_folder = typesystem.String(title="Joplin Folder", max_length=80, allow_blank=True)
reddit = typesystem.String(title="Subreddit", max_length=80, allow_blank=True)
localstorage = typesystem.String(title="Markdown Folder", allow_blank=True)
mail = typesystem.Boolean(title="Send a mail ?", default=False)
mastodon = typesystem.Boolean(title="Publish on Mastodon ?", default=False)
status = typesystem.Boolean(title="Status", default=False)
# the model class
model: typing.Type[Base]
# the list of field names to include from the model
model_fields: typing.List[str] = []
# base set of mapped fields
# fields will only be added if they are not already defined
fields: typing.Dict[str, typesystem.Field] = {}
# a dict of mapped sqlalchemy column type class names
# and the class to validate in typesystem
type_mapping = {
"BigInteger": typesystem.Integer,
"Boolean": typesystem.Boolean,
"Date": typesystem.Date,
"DateTime": typesystem.DateTime,
"Float": Float,
"Integer": typesystem.Integer,
"Numeric": Decimal,
"SmallInteger": typesystem.Integer,
"String": typesystem.String,
"Text": typesystem.Text,
"Time": typesystem.Time,
"Unicode": typesystem.String,
"UnicodeText": typesystem.Text,
"EmailType": Email,
}
def __new__(cls) -> "ModelSchemaGenerator":
# loop over the class attrs and add to the fields dict