return b64encode(value).decode("utf-8") def to_python(self, value: str) -> bytes: return b64decode(value) @property def json_schema(self) -> JsonDict: return { "type": "string", "pattern": r"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$", } JsonSchemaMixin.register_field_encoders({Binary: BinaryField()}) class DateTimeField(FieldEncoder): """Wrapper around Python datetime.datetime object""" def to_wire(self, value: DateTime) -> float: if isinstance(value, float): return value return value.timestamp() def to_python(self, value: float) -> DateTime: return DateTime(datetime.fromtimestamp(value)) @property def json_schema(self) -> JsonDict: return {"type": "number"}
class ParameterKeyField(FieldEncoder): @property def json_schema(self): return { "type": "string", "pattern": r"[a-zA-Z0-9]*^$", "Description": "CloudFormation parameter name, can contain letters and " "numbers only", } JsonSchemaMixin.register_field_encoders({ParameterKey: ParameterKeyField()}) class RegionField(FieldEncoder): @property def json_schema(self): return { "type": "string", "pattern": r"^(ap|eu|us|sa|ca|cn|af|me|us-gov)-(central|south|north|east|" r"west|southeast|southwest|northeast|northwest)-[0-9]$", "description": "AWS Region name eg.: 'us-east-1'", }
return {"type": "string"} class DecimalField(FieldEncoder): def to_wire(self, value): return value.to_decimal() def to_python(self, value): return Decimal128(str(value)) @property def json_schema(self): return {"type": "number"} JsonSchemaMixin.register_field_encoders({ObjectId: ObjectIdField()}) JsonSchemaMixin.register_field_encoders({Decimal128: DecimalField()}) class MongoJSONEncoder(JSONEncoder): def default(self, obj): if isinstance(obj, ObjectId): return str(obj) elif isinstance(obj, Decimal): return float(obj) elif isinstance(obj, Decimal128): return float(obj.to_decimal()) else: return JSONEncoder.default(self, obj)
from enum import Enum from datetime import datetime from uuid import UUID from dataclasses_jsonschema import JsonSchemaMixin, FieldEncoder Postcode = NewType('Postcode', str) class PostcodeField(FieldEncoder): @property def json_schema(self): return {'type': 'string', 'minLength': 5, 'maxLength': 8} JsonSchemaMixin.register_field_encoders({Postcode: PostcodeField()}) class SubSchemas(JsonSchemaMixin): pass class Weekday(Enum): MON = 'Monday' TUE = 'Tuesday' WED = 'Wednesday' THU = 'Thursday' FRI = 'Friday' @dataclass(eq=True)
from dataclasses_jsonschema.field_types import DateTimeFieldEncoder from slugify import slugify from yrest.utils import get_url Email = NewType("Email", str) class EmailField(FieldEncoder): @property def json_schema(self): return {"type": "string", "format": "email"} JsonSchemaMixin.register_field_encoders({Email: EmailField()}) Phone = NewType("Phone", str) class PhoneField(FieldEncoder): @property def json_schema(self): return {"type": "string", "format": "phone"} JsonSchemaMixin.register_field_encoders({Phone: PhoneField()}) Password = NewType("Password", str)