def validate_or_error( cls: typing.Type["Schema"], value: typing.Any, *, strict: bool = False ) -> ValidationResult: try: value = cls.validate(value, strict=strict) except ValidationError as error: return ValidationResult(value=None, error=error) return ValidationResult(value=value, error=None)
def validate_or_error(self, value: typing.Any, *, strict: bool = False) -> ValidationResult: try: value = self.validate(value, strict=strict) except ValidationError as error: return ValidationResult(value=None, error=error) return ValidationResult(value=value, error=None)
async def validate(self, request: Request) -> ValidationResult: data = None # type: Optional[dict] content_type = request.headers.get("Content-Type") if content_type == "application/x-www-form-urlencoded": form = await request.form() data = dict(form) elif content_type == "application/json": data = await request.json() else: return ValidationResult( value=None, error=ValidationError(messages=[ Message( text="Invalid Content-Type value.", code="invalid", index=["content_type"], ) ]), ) return self.schema.validate_or_error(data)
def __init__(self): self.elements = {} self.uuid = uuid.uuid4() self.validation_result = ValidationResult() self.widgets = {} self.stylesheets = {}