Initial Commit

This commit is contained in:
2025-03-21 22:35:26 -03:00
commit 0b11408c8f
11 changed files with 777 additions and 0 deletions

0
tests/__init__.py Normal file
View File

25
tests/test_conversion.py Normal file
View File

@@ -0,0 +1,25 @@
from jsonschema_pydantic import jsonschema_to_pydantic
from jsonschema_pydantic.types import JSONSchema
from unittest import TestCase
class TestConversion(TestCase):
def test_jsonschema_to_pydantic(self):
schema: JSONSchema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
},
"required": ["name"],
}
model = jsonschema_to_pydantic(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

View File

@@ -0,0 +1,34 @@
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)