Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import unittest
import colander
from cornice_swagger.swagger import DefinitionHandler
from cornice_swagger.converters import convert_schema as convert
class MyListSchema(colander.SequenceSchema):
entry = colander.SchemaNode(colander.String())
class BoredoomSchema(colander.MappingSchema):
motivators = MyListSchema()
status = colander.SchemaNode(colander.String(),
validator=colander.OneOf(['increasing', 'decrasing']))
class AnxietySchema(colander.MappingSchema):
level = colander.SchemaNode(colander.Integer(),
validator=colander.Range(42, 9000))
class FeelingsSchema(colander.MappingSchema):
bleh = BoredoomSchema()
aaaa = AnxietySchema()
class DefinitionTest(unittest.TestCase):
def setUp(self):
self.handler = DefinitionHandler()
{
"subtype": "UCB1-tuned",
"historical_info": {
"arms_sampled": {
"arm1": {"win": 20, "loss": 5, "total": 25, "variance": 0.1},
"arm2": {"win": 20, "loss": 10, "total": 30, "variance": 0.2},
"arm3": {"win": 0, "loss": 0, "total": 0},
},
},
}
"""
subtype = colander.SchemaNode(
colander.String(),
validator=colander.OneOf(UCB_SUBTYPES),
missing=DEFAULT_UCB_SUBTYPE,
)
historical_info = BanditHistoricalInfo()
dates = DatesSchema()
class FileUploads(schema.SequenceSchema):
file = schema.SchemaNode(schema.FileData())
class MySchema(schema.MappingSchema):
name = schema.SchemaNode(schema.String(), description=LONG_DESC)
title = schema.SchemaNode(schema.String(),
validator=colander.Length(max=10),
description=LONG_DESC)
password = schema.SchemaNode(schema.String(),
validator=colander.Length(min=5))
cool = schema.SchemaNode(schema.Boolean(), default=True)
series = SeriesSchema()
color = schema.SchemaNode(schema.String(),
validator=colander.OneOf(('red', 'blue')))
uploads = FileUploads()
def validate_form(form, value):
if value['name'] != value['title']:
exc = exception.Invalid(form, 'Name does not match title')
exc['title'] = 'Does not match title'
exc['name'] = 'Does not match name'
raise exc
def form_view(request):
# Create a schema; when the form is submitted, we want to assert
# that the name must match the title; we use a validator for the
# entire form by assigning it a validator
schema = MySchema(validator=validate_form)
# Create a form; it will have a single button named submit.
def _get_msdata_schema(self):
def textarea_or_file(node, value):
"""Validator that either textarea or file upload is filled"""
if not(not value['ms_data'] is colander.null or
not value['ms_data_file'] is colander.null):
error = 'Either ms_data or ms_data_file must be set'
exception = colander.Invalid(node)
exception.add(colander.Invalid(msdata_stringSchema, error))
exception.add(colander.Invalid(msdata_fileSchema, error))
raise exception
schema = colander.SchemaNode(colander.Mapping(),
validator=textarea_or_file)
valid_formats = colander.OneOf(['mzxml',
'mass_tree',
'form_tree',
'mgf',
])
schema.add(colander.SchemaNode(colander.String(),
validator=valid_formats,
name='ms_data_format'
))
filled = colander.Length(min=1)
msdata_stringSchema = colander.SchemaNode(colander.String(),
validator=filled,
missing=colander.null,
name='ms_data')
schema.add(msdata_stringSchema)
def deserialize(self, cstruct=colander.null):
# If permissions are not a mapping (e.g null or invalid), try deserializing
if not isinstance(cstruct, dict):
return super().deserialize(cstruct)
# If using application/merge-patch+json we need to allow null values as they
# represent removing a key.
cstruct, removed_keys = self._preprocess_null_perms(cstruct)
# If permissions are listed, check fields and produce fancy error messages
if self.known_perms:
for perm in cstruct:
colander.OneOf(choices=self.known_perms)(self, perm)
permissions = super().deserialize(cstruct)
# Else deserialize the fields that are not on the schema
else:
permissions = {}
perm_schema = colander.SequenceSchema(colander.SchemaNode(colander.String()))
for perm, principals in cstruct.items():
permissions[perm] = perm_schema.deserialize(principals)
return self._postprocess_null_perms(permissions, removed_keys)
class Attributes(colander.SequenceSchema):
attribute = AttributeValidator()
class SchemaValidator(colander.MappingSchema):
project = SchemaNode(ProjectRef())
name = SchemaNode(colander.String(),
validator=database_name)
label = SchemaNode(colander.String(),
validator=colander.Length(min=3))
hidden = SchemaNode(colander.Boolean(),
missing=False)
obj = SchemaNode(colander.String(),
validator=colander.OneOf(['entity', 'relation']))
meta = SchemaNode(colander.Mapping(unknown='preserve'),
missing={})
attributes = Attributes()
def validate(data):
""" Validate the incoming data. """
sane = SchemaValidator().deserialize(data)
sref = SchemaRef(sane.get('project'))
sane['parent'] = sref.decode(None, data.get('parent'))
return sane
def save(data, schema=None):
""" Create a schema. """
data = validate(data)
class BurnUnitsSchema(MappingSchema):
offset = SchemaNode(String(),
description='SI units for distance',
validator=OneOf(_valid_dist_units))
boom_length = SchemaNode(String(),
description='SI units for distance',
validator=OneOf(_valid_dist_units))
boom_draft = SchemaNode(String(),
description='SI units for distance',
validator=OneOf(_valid_dist_units))
speed = SchemaNode(String(),
description='SI units for speed',
validator=OneOf(_valid_vel_units))
class BurnSchema(ResponseSchema):
offset = SchemaNode(
Integer(), save=True, update=True
)
boom_length = SchemaNode(
Integer(), save=True, update=True
)
boom_draft = SchemaNode(
Integer(), save=True, update=True
)
speed = SchemaNode(
Float(), save=True, update=True
)
throughput = SchemaNode(
def validator(self, kw: dict):
"""Validator."""
return OneOf(('direct', 'registration_mail', 'invitation_mail'))