Fist Initial and Working Version of JsonSchema to Pydantic
This commit is contained in:
@@ -1,13 +1,19 @@
|
||||
from jsonschema_pydantic import jsonschema_to_pydantic
|
||||
from jsonschema_pydantic.types import JSONSchema
|
||||
from jsonschema_pydantic import ModelSchemaBuilder
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from unittest import TestCase
|
||||
|
||||
|
||||
def is_pydantic_model(cls):
|
||||
return isinstance(cls, type) and issubclass(cls, BaseModel)
|
||||
|
||||
|
||||
class TestConversion(TestCase):
|
||||
def test_jsonschema_to_pydantic(self):
|
||||
|
||||
schema: JSONSchema = {
|
||||
schema = {
|
||||
"title": "Person",
|
||||
"description": "A person",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {"type": "string"},
|
||||
@@ -16,10 +22,112 @@ class TestConversion(TestCase):
|
||||
"required": ["name"],
|
||||
}
|
||||
|
||||
model = jsonschema_to_pydantic(schema)
|
||||
model = ModelSchemaBuilder.build(schema)
|
||||
|
||||
assert model.__fields__.keys() == {"name", "age"}
|
||||
assert model.__fields__["name"].required is True
|
||||
assert model.__fields__["age"].required is False
|
||||
assert model.__fields__["name"].type_ == str
|
||||
assert model.__fields__["age"].type_ == int
|
||||
self.assertTrue(is_pydantic_model(model))
|
||||
|
||||
def test_validation_string(self):
|
||||
schema = {
|
||||
"title": "Person",
|
||||
"description": "A person",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {"type": "string"},
|
||||
},
|
||||
"required": ["name"],
|
||||
}
|
||||
|
||||
model = ModelSchemaBuilder.build(schema)
|
||||
|
||||
self.assertEqual(model(name="John", age=30).name, "John")
|
||||
|
||||
def test_validation_integer(self):
|
||||
schema = {
|
||||
"title": "Person",
|
||||
"description": "A person",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"age": {"type": "integer"},
|
||||
},
|
||||
"required": ["age"],
|
||||
}
|
||||
|
||||
model = ModelSchemaBuilder.build(schema)
|
||||
|
||||
self.assertEqual(model(age=30).age, 30)
|
||||
|
||||
self.assertEqual(model(age="30").age, 30)
|
||||
|
||||
def test_validation_float(self):
|
||||
schema = {
|
||||
"title": "Person",
|
||||
"description": "A person",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"age": {"type": "number"},
|
||||
},
|
||||
"required": ["age"],
|
||||
}
|
||||
|
||||
model = ModelSchemaBuilder.build(schema)
|
||||
|
||||
self.assertEqual(model(age=30).age, 30.0)
|
||||
|
||||
self.assertEqual(model(age="30").age, 30.0)
|
||||
|
||||
def test_validation_boolean(self):
|
||||
schema = {
|
||||
"title": "Person",
|
||||
"description": "A person",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"is_active": {"type": "boolean"},
|
||||
},
|
||||
"required": ["is_active"],
|
||||
}
|
||||
|
||||
model = ModelSchemaBuilder.build(schema)
|
||||
|
||||
self.assertEqual(model(is_active=True).is_active, True)
|
||||
|
||||
self.assertEqual(model(is_active="true").is_active, True)
|
||||
|
||||
def test_validation_list(self):
|
||||
schema = {
|
||||
"title": "Person",
|
||||
"description": "A person",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"friends": {"type": "array", "items": {"type": "string"}},
|
||||
},
|
||||
"required": ["friends"],
|
||||
}
|
||||
|
||||
model = ModelSchemaBuilder.build(schema)
|
||||
|
||||
self.assertEqual(model(friends=["John", "Jane"]).friends, ["John", "Jane"])
|
||||
|
||||
def test_validation_object(self):
|
||||
schema = {
|
||||
"title": "Person",
|
||||
"description": "A person",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"address": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"street": {"type": "string"},
|
||||
"city": {"type": "string"},
|
||||
},
|
||||
"required": ["street", "city"],
|
||||
},
|
||||
},
|
||||
"required": ["address"],
|
||||
}
|
||||
|
||||
model = ModelSchemaBuilder.build(schema)
|
||||
|
||||
obj = model(address={"street": "123 Main St", "city": "Springfield"})
|
||||
|
||||
self.assertEqual(obj.address.street, "123 Main St")
|
||||
self.assertEqual(obj.address.city, "Springfield")
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
from jsonschema_pydantic.types import JSONSchema, JSONSchemaValidator
|
||||
|
||||
from pydantic import ValidationError
|
||||
|
||||
from unittest import TestCase
|
||||
|
||||
|
||||
class TestJsonSchemaValidator(TestCase):
|
||||
def test_jsonschema_validator(self):
|
||||
schema: JSONSchema = {
|
||||
"$schema": "http://json-schema.org/schema",
|
||||
"$id": "http://example.com/schema",
|
||||
"title": "Person",
|
||||
"description": "A person",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {"type": "string"},
|
||||
"age": {"type": "integer"},
|
||||
},
|
||||
"required": ["name"],
|
||||
}
|
||||
|
||||
JSONSchemaValidator.validate_python(schema)
|
||||
|
||||
def test_jsonschema_validator_fails(self):
|
||||
schema: JSONSchema = {
|
||||
"$schema": "http://json-schema.org/schema",
|
||||
"$id": "http://example.com/schema",
|
||||
"title": "Person",
|
||||
"description": "A person",
|
||||
}
|
||||
|
||||
with self.assertRaises(ValidationError):
|
||||
JSONSchemaValidator.validate_python(schema)
|
||||
Reference in New Issue
Block a user