Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
validate_yaml(text, validator=validator)
exc = exc_info.value
assert exc.messages() == [
Message(
text="Must be a number.",
code="type",
index=["b"],
start_position=Position(line_no=2, column_no=4, char_index=10),
end_position=Position(line_no=2, column_no=6, char_index=12),
)
]
class Validator(Schema):
a = Integer()
b = Integer()
text = "a: 123\nb: abc\n"
with pytest.raises(ValidationError) as exc_info:
validate_yaml(text, validator=Validator)
exc = exc_info.value
assert exc.messages() == [
Message(
text="Must be a number.",
code="type",
index=["b"],
start_position=Position(line_no=2, column_no=4, char_index=10),
end_position=Position(line_no=2, column_no=6, char_index=12),
)
]
import datetime
import decimal
import uuid
import pytest
import typesystem
import typesystem.formats
class Person(typesystem.Schema):
name = typesystem.String(max_length=100, allow_blank=False)
age = typesystem.Integer()
class Product(typesystem.Schema):
name = typesystem.String(max_length=100, allow_blank=False)
rating = typesystem.Integer(default=None)
def test_required():
class Example(typesystem.Schema):
field = typesystem.Integer()
value, error = Example.validate_or_error({})
assert dict(error) == {"field": "This field is required."}
class Example(typesystem.Schema):
field = typesystem.Integer(allow_null=True)
import typesystem
class Example(typesystem.Schema):
a = typesystem.String(max_length=10)
b = typesystem.Integer(maximum=5)
def test_validation_result_repr():
result = Example.validate_or_error({"a": "a", "b": 1})
assert repr(result) == "ValidationResult(value=Example(a='a', b=1))"
result = Example.validate_or_error({"a": "a"})
assert (
repr(result)
== "ValidationResult(error=ValidationError([Message(text='This field is required.', code='required', index=['b'])]))"
)
def test_validation_error_repr():
result = Example.validate_or_error({"a": "a"})
assert (
assert value == ExampleA(field_on_a=123, example_b=ExampleB(field_on_b=456))
class ExampleC(typesystem.Schema, definitions=definitions):
field_on_c = typesystem.Integer()
example_d = typesystem.Array(items=typesystem.Reference("ExampleD"))
class ExampleD(typesystem.Schema, definitions=definitions):
field_on_d = typesystem.Integer()
value = ExampleC.validate(
{"field_on_c": "123", "example_d": [{"field_on_d": "456"}]}
)
assert value == ExampleC(field_on_c=123, example_d=[ExampleD(field_on_d=456)])
class ExampleE(typesystem.Schema, definitions=definitions):
field_on_e = typesystem.Integer()
example_f = typesystem.Array(items=[typesystem.Reference("ExampleF")])
class ExampleF(typesystem.Schema, definitions=definitions):
field_on_f = typesystem.Integer()
value = ExampleE.validate(
{"field_on_e": "123", "example_f": [{"field_on_f": "456"}]}
)
assert value == ExampleE(field_on_e=123, example_f=[ExampleF(field_on_f=456)])
class ExampleG(typesystem.Schema, definitions=definitions):
field_on_g = typesystem.Integer()
example_h = typesystem.Object(
properties={"h": typesystem.Reference("ExampleH")}
)
class ExampleA(typesystem.Schema, definitions=definitions):
field_on_a = typesystem.Integer()
example_b = typesystem.Reference("ExampleB")
class ExampleB(typesystem.Schema, definitions=definitions):
field_on_b = typesystem.Integer()
value = ExampleA.validate({"field_on_a": "123", "example_b": {"field_on_b": "456"}})
assert value == ExampleA(field_on_a=123, example_b=ExampleB(field_on_b=456))
class ExampleC(typesystem.Schema, definitions=definitions):
field_on_c = typesystem.Integer()
example_d = typesystem.Array(items=typesystem.Reference("ExampleD"))
class ExampleD(typesystem.Schema, definitions=definitions):
field_on_d = typesystem.Integer()
value = ExampleC.validate(
{"field_on_c": "123", "example_d": [{"field_on_d": "456"}]}
)
assert value == ExampleC(field_on_c=123, example_d=[ExampleD(field_on_d=456)])
class ExampleE(typesystem.Schema, definitions=definitions):
field_on_e = typesystem.Integer()
example_f = typesystem.Array(items=[typesystem.Reference("ExampleF")])
class ExampleF(typesystem.Schema, definitions=definitions):
field_on_f = typesystem.Integer()
value = ExampleE.validate(
{"field_on_e": "123", "example_f": [{"field_on_f": "456"}]}
)
| typesystem.Boolean()
),
"required": typesystem.Array(items=typesystem.String(), unique_items=True),
# Array
"items": (
typesystem.Reference("JSONSchema", definitions=definitions)
| typesystem.Array(
items=typesystem.Reference("JSONSchema", definitions=definitions),
min_items=1,
)
),
"additionalItems": (
typesystem.Reference("JSONSchema", definitions=definitions)
| typesystem.Boolean()
),
"minItems": typesystem.Integer(minimum=0),
"maxItems": typesystem.Integer(minimum=0),
"uniqueItems": typesystem.Boolean(),
}
)
| typesystem.Boolean()
)
definitions["JSONSchema"] = JSON_SCHEMA
import typesystem
class ErrorSchema(typesystem.Schema):
code = typesystem.Integer()
message = typesystem.String()
data = typesystem.Object(additional_properties=True)
class JSONRPCRequest(typesystem.Schema):
jsonrpc = typesystem.String(pattern="2.0", trim_whitespace=False)
id = typesystem.Union(
any_of=[
typesystem.String(allow_null=True, min_length=1, trim_whitespace=False),
typesystem.Integer(allow_null=True),
]
)
params = typesystem.Union(
any_of=[typesystem.Object(additional_properties=True), typesystem.Array()]
)
method = typesystem.String()
definitions = typesystem.SchemaDefinitions()
JSON_SCHEMA = (
typesystem.Object(
properties={
"$ref": typesystem.String(),
"type": typesystem.String() | typesystem.Array(items=typesystem.String()),
"enum": typesystem.Array(unique_items=True, min_items=1),
"definitions": typesystem.Object(
additional_properties=typesystem.Reference(
"JSONSchema", definitions=definitions
)
),
# String
"minLength": typesystem.Integer(minimum=0),
"maxLength": typesystem.Integer(minimum=0),
"pattern": typesystem.String(format="regex"),
"format": typesystem.String(),
# Numeric
"minimum": typesystem.Number(),
"maximum": typesystem.Number(),
"exclusiveMinimum": typesystem.Number(),
"exclusiveMaximum": typesystem.Number(),
"multipleOf": typesystem.Number(exclusive_minimum=0),
# Object
"properties": typesystem.Object(
additional_properties=typesystem.Reference(
"JSONSchema", definitions=definitions
)
),
"minProperties": typesystem.Integer(minimum=0),
"maxProperties": typesystem.Integer(minimum=0),
import decimal
import inspect
from datetime import date, datetime, time
from functools import wraps
import typing
import typesystem
FIELD_ALIASES: typing.Dict[typing.Type, typesystem.Field] = {
int: typesystem.Integer,
float: typesystem.Float,
bool: typesystem.Boolean,
decimal.Decimal: typesystem.Decimal,
date: typesystem.Date,
time: typesystem.Time,
datetime: typesystem.DateTime,
}
class PathConversionError(typesystem.ValidationError):
pass
class Converter:
__slots__ = ("func", "signature", "annotations", "required_params")