class SourcePrice: """An object containing prices for a single contract""" id: int = fields.integer_field(description="ID", required=True) extra: Dict = fields.data_field(description="JSON blob") prices: Dict[str, Decimal] = fields.data_field( description="source-price mapping", items=fields.decimal_field( min_value=0, max_value=100, precision=4, description="price", ), default_factory=dict, ) foos: List[Foo] = fields.data_field(default_factory=list)
def test_ListValidator_call_invalid(): value = [1, "string"] field = data_field() validator1 = FakeValidator() validator2 = FakeValidatorError() validator = ListValidator((validator1, validator2)) with pytest.raises(ValidationError): validator(field, value)
def test_ListValidator_call_valid(): value = "test" field = data_field() validator1 = FakeValidator() validator2 = FakeValidator() validator = ListValidator((validator1, validator2)) assert validator(field, value) == "test" assert validator.dump(3) == "3"
class MyClass: """Test data""" str_field: str = data_field(required=True, format="uuid", description="String field") int_field: int = data_field(format="uint64", description="Int field") float_field: float = number_field(description="Float field") boolean_field: bool = bool_field(description="Bool field") map_field: Dict[str, int] = data_field(description="Dict field") free_field: Dict[str, str] = data_field(description="Free field") datetime_field: datetime = date_time_field( description="Datetime field") ref_field: OtherClass = field(metadata={ "required": True, "description": "Ref field" }, default=None) list_ref_field: List[OtherClass] = data_field(description="List field")
class OtherClass: str_field: str = data_field(description="String field")
class MyClass: map_field: Dict[int, str] = data_field(description="Map field")
class MyClass: desc_field: str = data_field(description="Valid field") no_desc_field: str = data_field()
def test_base_validator(): validator = Validator() field = data_field() with pytest.raises(ValidationError): validator(field, None)
def test_Choice_call_invalid(): field = data_field() validator = Choice(["a", "b", "c"]) with pytest.raises(ValidationError): validator(field, "d")
def test_Choice_call_valid(): field = data_field() validator = Choice(["a", "b", "c"]) assert validator(field, "a") == "a"
class Role: name: str = fields.str_field(required=True, description="Role name") permissions: List[Permission] = fields.data_field( required=True, description="List of permissions" )
class Permission: paths: List[str] = fields.data_field(description="Permition paths") methods: List[str] = fields.data_field(description="Permition methods") body: Dict[str, str] = fields.json_field(description="Permission body") action: str = fields.str_field(default="allow", description="Permition action")
class BundleUpload: files: List[bytes] = fields.data_field(description="list of bundles to upload")