def validate_value(self, json_value): schema = { **self.json_schema, 'definitions': get_definitions() } try: validate(json_value, schema, format_checker=FormatChecker()) except JsonSchemaValidationError as exc: raise ValidationError() from exc
def test_enum_schema(self): class MyEnum(Enum): """ This is my enum. For testing purposes. """ ONE = auto(), TWO = auto(), THREE = auto() enum_type = EnumType(MyEnum, 'title', 'description') self.assertEqual(enum_type.json_schema, { '$ref': '#/definitions/MyEnum', 'title': 'title', 'description': 'description' }) self.assertIn('MyEnum', get_definitions()) self.assertEqual(get_definitions()['MyEnum'], { 'type': 'string', 'enum': ['ONE', 'TWO', 'THREE'], 'title': 'This is my enum.', 'description': 'For testing purposes.' }) MyEnum.__doc__ = None enum_type = EnumType(MyEnum, 'other title', 'other description') self.assertEqual(enum_type.json_schema, { '$ref': '#/definitions/MyEnum', 'title': 'other title', 'description': 'other description' }) self.assertIn('MyEnum', get_definitions()) self.assertEqual(get_definitions()['MyEnum'], { 'type': 'string', 'enum': ['ONE', 'TWO', 'THREE'], 'title': 'This is my enum.', 'description': 'For testing purposes.' })
def test_entity_type_from_json(self): class MyModel3(Entity): @classmethod def json_to_value(cls, value): return value['value'] get_definitions()['MyModel3'] = { 'type': 'object', 'properties': { 'value': { 'type': 'integer' } } } entity_type = EntityType(MyModel3, 'title', 'description') self.assertEqual(entity_type.json_to_value({'value': 4}), 4)
def test_get_definitions(self): from untt import types self.assertTrue(get_definitions() is types.definitions)
def test_entity_root_definitions(self): class MyEnum(Enum): """ This is my enum. It is a very nice enum. """ VERY = auto() NICE = auto() ENUM = auto() class MySubEntity(Entity): """ This is my sub entity. It is a very nice sub entity. """ sub_int_prop: (int, 'some sub number', {'minimum': 0}) sub_date_prop: (datetime, 'the sub date') = datetime(2000, 1, 2, 3, 4, 5) class MyEntity(Entity): """ This is my entity. It is a very nice entity. """ num_prop: (float, 'any number') nullable_optional_prop: ( bool, 'just junk' ) | Null = None optional_str_prop: (str, 'some text') = 'hello' optional_stuff: ( list, 'my stuff', bool, {5, 10} ) = [True, False, True, False, True, False] entity_prop: (MySubEntity, 'my lovely sub') enum_prop: (MyEnum, 'my lovely enum') self.assertEqual(get_definitions(), { 'MyEnum': { 'title': 'This is my enum.', 'description': 'It is a very nice enum.', 'type': 'string', 'enum': ['VERY', 'NICE', 'ENUM'] }, 'MySubEntity': { 'title': 'This is my sub entity.', 'description': 'It is a very nice sub entity.', 'type': 'object', 'additionalProperties': False, 'properties': { 'sub_int_prop': { 'title': 'sub_int_prop', 'description': 'some sub number', 'type': 'integer', 'minimum': 0 }, 'sub_date_prop': { 'title': 'sub_date_prop', 'description': 'the sub date', 'type': 'string', 'format': 'date-time', 'default': '2000-01-02T03:04:05' } }, 'required': ['sub_int_prop'] }, 'MyEntity': { 'title': 'This is my entity.', 'description': 'It is a very nice entity.', 'type': 'object', 'additionalProperties': False, 'properties': { 'num_prop': { 'title': 'num_prop', 'description': 'any number', 'type': 'number' }, 'nullable_optional_prop': { 'oneOf': [ {'type': 'null'}, { 'title': 'nullable_optional_prop', 'description': 'just junk', 'type': 'boolean', 'default': None } ] }, 'optional_str_prop': { 'title': 'optional_str_prop', 'description': 'some text', 'type': 'string', 'default': 'hello' }, 'optional_stuff': { 'title': 'optional_stuff', 'description': 'my stuff', 'type': 'array', 'items': { 'title': 'optional_stuff item', 'type': 'boolean' }, 'minItems': 5, 'maxItems': 10, 'default': [True, False, True, False, True, False] }, 'entity_prop': { '$ref': '#/definitions/MySubEntity', 'title': 'entity_prop', 'description': 'my lovely sub' }, 'enum_prop': { '$ref': '#/definitions/MyEnum', 'title': 'enum_prop', 'description': 'my lovely enum' } }, 'required': ['num_prop', 'entity_prop', 'enum_prop'] } })