Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Fixes issue 81, model instance in new #82

Merged
merged 6 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ class ValidationMetadata(frozendict.frozendict):
return self.get('validated_path_to_schemas')


def add_deeper_validated_schemas(validation_metadata: ValidationMetadata, path_to_schemas: dict):
# this is called if validation_ran_earlier and current and deeper locations need to be added
current_path_to_item = validation_metadata.path_to_item
other_path_to_schemas = {}
for path_to_item, schemas in validation_metadata.validated_path_to_schemas.items():
if len(path_to_item) < len(current_path_to_item):
continue
path_begins_with_current_path = path_to_item[:len(current_path_to_item)] == current_path_to_item
if path_begins_with_current_path:
other_path_to_schemas[path_to_item] = schemas
update(path_to_schemas, other_path_to_schemas)


class Singleton:
"""
Enums and singletons are the same
Expand Down Expand Up @@ -385,9 +398,9 @@ class Schema:
because value is of the correct type, and validation was run earlier when the instance was created
"""
_path_to_schemas = {}
if validation_metadata.validated_path_to_schemas:
update(_path_to_schemas, validation_metadata.validated_path_to_schemas)
if not validation_metadata.validation_ran_earlier(cls):
if validation_metadata.validation_ran_earlier(cls):
add_deeper_validated_schemas(validation_metadata, _path_to_schemas)
else:
other_path_to_schemas = cls._validate_oapg(arg, validation_metadata=validation_metadata)
update(_path_to_schemas, other_path_to_schemas)
# loop through it make a new class for each entry
Expand Down Expand Up @@ -1325,6 +1338,7 @@ class ListBase(ValidatorBase):
validated_path_to_schemas=validation_metadata.validated_path_to_schemas
)
if item_validation_metadata.validation_ran_earlier(item_cls):
add_deeper_validated_schemas(item_validation_metadata, path_to_schemas)
continue
other_path_to_schemas = item_cls._validate_oapg(
value, validation_metadata=item_validation_metadata)
Expand Down Expand Up @@ -1588,6 +1602,7 @@ class DictBase(Discriminable, ValidatorBase):
validated_path_to_schemas=validation_metadata.validated_path_to_schemas
)
if arg_validation_metadata.validation_ran_earlier(schema):
add_deeper_validated_schemas(arg_validation_metadata, path_to_schemas)
continue
other_path_to_schemas = schema._validate_oapg(value, validation_metadata=arg_validation_metadata)
update(path_to_schemas, other_path_to_schemas)
Expand Down Expand Up @@ -1676,6 +1691,7 @@ class DictBase(Discriminable, ValidatorBase):
validated_path_to_schemas=validation_metadata.validated_path_to_schemas
)
if updated_vm.validation_ran_earlier(discriminated_cls):
add_deeper_validated_schemas(updated_vm, _path_to_schemas)
return _path_to_schemas
other_path_to_schemas = discriminated_cls._validate_oapg(arg, validation_metadata=updated_vm)
update(_path_to_schemas, other_path_to_schemas)
Expand Down Expand Up @@ -1774,18 +1790,11 @@ def cast_to_allowed_types(
if isinstance(arg, Schema):
# store the already run validations
schema_classes = set()
source_schema_was_unset = len(arg.__class__.__bases__) == 2 and UnsetAnyTypeSchema in arg.__class__.__bases__
if not source_schema_was_unset:
"""
Do not include UnsetAnyTypeSchema and its base class because
it did not exist in the original spec schema definition
It was added to ensure that all instances are of type Schema and the allowed base types
"""
for cls in arg.__class__.__bases__:
if cls is Singleton:
# Skip Singleton
continue
schema_classes.add(cls)
for cls in arg.__class__.__bases__:
if cls is Singleton:
# Skip Singleton
continue
schema_classes.add(cls)
validated_path_to_schemas[path_to_item] = schema_classes

type_error = ApiTypeError(f"Invalid type. Required value type is str and passed type was {type(arg)} at {path_to_item}")
Expand Down Expand Up @@ -1838,6 +1847,7 @@ class ComposedBase(Discriminable):
path_to_schemas = defaultdict(set)
for allof_cls in cls.MetaOapg.all_of():
if validation_metadata.validation_ran_earlier(allof_cls):
add_deeper_validated_schemas(validation_metadata, path_to_schemas)
continue
other_path_to_schemas = allof_cls._validate_oapg(arg, validation_metadata=validation_metadata)
update(path_to_schemas, other_path_to_schemas)
Expand All @@ -1858,6 +1868,7 @@ class ComposedBase(Discriminable):
continue
if validation_metadata.validation_ran_earlier(oneof_cls):
oneof_classes.append(oneof_cls)
add_deeper_validated_schemas(validation_metadata, path_to_schemas)
continue
try:
path_to_schemas = oneof_cls._validate_oapg(arg, validation_metadata=validation_metadata)
Expand Down Expand Up @@ -1915,6 +1926,7 @@ class ComposedBase(Discriminable):
for anyof_cls in cls.MetaOapg.any_of():
if validation_metadata.validation_ran_earlier(anyof_cls):
anyof_classes.append(anyof_cls)
add_deeper_validated_schemas(validation_metadata, path_to_schemas)
continue

try:
Expand Down Expand Up @@ -2044,6 +2056,7 @@ class ComposedBase(Discriminable):

if discriminated_cls is not None and not updated_vm.validation_ran_earlier(discriminated_cls):
# TODO use an exception from this package here
add_deeper_validated_schemas(updated_vm, path_to_schemas)
assert discriminated_cls in path_to_schemas[updated_vm.path_to_item]
return path_to_schemas

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ def validated_path_to_schemas(self) -> typing.Dict[typing.Tuple[typing.Union[str
return self.get('validated_path_to_schemas')


def add_deeper_validated_schemas(validation_metadata: ValidationMetadata, path_to_schemas: dict):
# this is called if validation_ran_earlier and current and deeper locations need to be added
current_path_to_item = validation_metadata.path_to_item
other_path_to_schemas = {}
for path_to_item, schemas in validation_metadata.validated_path_to_schemas.items():
if len(path_to_item) < len(current_path_to_item):
continue
path_begins_with_current_path = path_to_item[:len(current_path_to_item)] == current_path_to_item
if path_begins_with_current_path:
other_path_to_schemas[path_to_item] = schemas
update(path_to_schemas, other_path_to_schemas)


class Singleton:
"""
Enums and singletons are the same
Expand Down Expand Up @@ -392,9 +405,9 @@ def __get_new_cls(
because value is of the correct type, and validation was run earlier when the instance was created
"""
_path_to_schemas = {}
if validation_metadata.validated_path_to_schemas:
update(_path_to_schemas, validation_metadata.validated_path_to_schemas)
if not validation_metadata.validation_ran_earlier(cls):
if validation_metadata.validation_ran_earlier(cls):
add_deeper_validated_schemas(validation_metadata, _path_to_schemas)
else:
other_path_to_schemas = cls._validate_oapg(arg, validation_metadata=validation_metadata)
update(_path_to_schemas, other_path_to_schemas)
# loop through it make a new class for each entry
Expand Down Expand Up @@ -1332,6 +1345,7 @@ def __validate_items(cls, list_items, validation_metadata: ValidationMetadata):
validated_path_to_schemas=validation_metadata.validated_path_to_schemas
)
if item_validation_metadata.validation_ran_earlier(item_cls):
add_deeper_validated_schemas(item_validation_metadata, path_to_schemas)
continue
other_path_to_schemas = item_cls._validate_oapg(
value, validation_metadata=item_validation_metadata)
Expand Down Expand Up @@ -1595,6 +1609,7 @@ def __validate_args(cls, arg, validation_metadata: ValidationMetadata):
validated_path_to_schemas=validation_metadata.validated_path_to_schemas
)
if arg_validation_metadata.validation_ran_earlier(schema):
add_deeper_validated_schemas(arg_validation_metadata, path_to_schemas)
continue
other_path_to_schemas = schema._validate_oapg(value, validation_metadata=arg_validation_metadata)
update(path_to_schemas, other_path_to_schemas)
Expand Down Expand Up @@ -1683,6 +1698,7 @@ def _validate_oapg(
validated_path_to_schemas=validation_metadata.validated_path_to_schemas
)
if updated_vm.validation_ran_earlier(discriminated_cls):
add_deeper_validated_schemas(updated_vm, _path_to_schemas)
return _path_to_schemas
other_path_to_schemas = discriminated_cls._validate_oapg(arg, validation_metadata=updated_vm)
update(_path_to_schemas, other_path_to_schemas)
Expand Down Expand Up @@ -1781,18 +1797,11 @@ def cast_to_allowed_types(
if isinstance(arg, Schema):
# store the already run validations
schema_classes = set()
source_schema_was_unset = len(arg.__class__.__bases__) == 2 and UnsetAnyTypeSchema in arg.__class__.__bases__
if not source_schema_was_unset:
"""
Do not include UnsetAnyTypeSchema and its base class because
it did not exist in the original spec schema definition
It was added to ensure that all instances are of type Schema and the allowed base types
"""
for cls in arg.__class__.__bases__:
if cls is Singleton:
# Skip Singleton
continue
schema_classes.add(cls)
for cls in arg.__class__.__bases__:
if cls is Singleton:
# Skip Singleton
continue
schema_classes.add(cls)
validated_path_to_schemas[path_to_item] = schema_classes

type_error = ApiTypeError(f"Invalid type. Required value type is str and passed type was {type(arg)} at {path_to_item}")
Expand Down Expand Up @@ -1845,6 +1854,7 @@ def __get_allof_classes(cls, arg, validation_metadata: ValidationMetadata):
path_to_schemas = defaultdict(set)
for allof_cls in cls.MetaOapg.all_of():
if validation_metadata.validation_ran_earlier(allof_cls):
add_deeper_validated_schemas(validation_metadata, path_to_schemas)
continue
other_path_to_schemas = allof_cls._validate_oapg(arg, validation_metadata=validation_metadata)
update(path_to_schemas, other_path_to_schemas)
Expand All @@ -1865,6 +1875,7 @@ def __get_oneof_class(
continue
if validation_metadata.validation_ran_earlier(oneof_cls):
oneof_classes.append(oneof_cls)
add_deeper_validated_schemas(validation_metadata, path_to_schemas)
continue
try:
path_to_schemas = oneof_cls._validate_oapg(arg, validation_metadata=validation_metadata)
Expand Down Expand Up @@ -1898,6 +1909,7 @@ def __get_anyof_classes(
for anyof_cls in cls.MetaOapg.any_of():
if validation_metadata.validation_ran_earlier(anyof_cls):
anyof_classes.append(anyof_cls)
add_deeper_validated_schemas(validation_metadata, path_to_schemas)
continue

try:
Expand Down Expand Up @@ -2011,6 +2023,7 @@ def _validate_oapg(

if discriminated_cls is not None and not updated_vm.validation_ran_earlier(discriminated_cls):
# TODO use an exception from this package here
add_deeper_validated_schemas(updated_vm, path_to_schemas)
assert discriminated_cls in path_to_schemas[updated_vm.path_to_item]
return path_to_schemas

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ def validated_path_to_schemas(self) -> typing.Dict[typing.Tuple[typing.Union[str
return self.get('validated_path_to_schemas')


def add_deeper_validated_schemas(validation_metadata: ValidationMetadata, path_to_schemas: dict):
# this is called if validation_ran_earlier and current and deeper locations need to be added
current_path_to_item = validation_metadata.path_to_item
other_path_to_schemas = {}
for path_to_item, schemas in validation_metadata.validated_path_to_schemas.items():
if len(path_to_item) < len(current_path_to_item):
continue
path_begins_with_current_path = path_to_item[:len(current_path_to_item)] == current_path_to_item
if path_begins_with_current_path:
other_path_to_schemas[path_to_item] = schemas
update(path_to_schemas, other_path_to_schemas)


class Singleton:
"""
Enums and singletons are the same
Expand Down Expand Up @@ -392,9 +405,9 @@ def __get_new_cls(
because value is of the correct type, and validation was run earlier when the instance was created
"""
_path_to_schemas = {}
if validation_metadata.validated_path_to_schemas:
update(_path_to_schemas, validation_metadata.validated_path_to_schemas)
if not validation_metadata.validation_ran_earlier(cls):
if validation_metadata.validation_ran_earlier(cls):
add_deeper_validated_schemas(validation_metadata, _path_to_schemas)
else:
other_path_to_schemas = cls._validate_oapg(arg, validation_metadata=validation_metadata)
update(_path_to_schemas, other_path_to_schemas)
# loop through it make a new class for each entry
Expand Down Expand Up @@ -1332,6 +1345,7 @@ def __validate_items(cls, list_items, validation_metadata: ValidationMetadata):
validated_path_to_schemas=validation_metadata.validated_path_to_schemas
)
if item_validation_metadata.validation_ran_earlier(item_cls):
add_deeper_validated_schemas(item_validation_metadata, path_to_schemas)
continue
other_path_to_schemas = item_cls._validate_oapg(
value, validation_metadata=item_validation_metadata)
Expand Down Expand Up @@ -1595,6 +1609,7 @@ def __validate_args(cls, arg, validation_metadata: ValidationMetadata):
validated_path_to_schemas=validation_metadata.validated_path_to_schemas
)
if arg_validation_metadata.validation_ran_earlier(schema):
add_deeper_validated_schemas(arg_validation_metadata, path_to_schemas)
continue
other_path_to_schemas = schema._validate_oapg(value, validation_metadata=arg_validation_metadata)
update(path_to_schemas, other_path_to_schemas)
Expand Down Expand Up @@ -1683,6 +1698,7 @@ def _validate_oapg(
validated_path_to_schemas=validation_metadata.validated_path_to_schemas
)
if updated_vm.validation_ran_earlier(discriminated_cls):
add_deeper_validated_schemas(updated_vm, _path_to_schemas)
return _path_to_schemas
other_path_to_schemas = discriminated_cls._validate_oapg(arg, validation_metadata=updated_vm)
update(_path_to_schemas, other_path_to_schemas)
Expand Down Expand Up @@ -1781,18 +1797,11 @@ def cast_to_allowed_types(
if isinstance(arg, Schema):
# store the already run validations
schema_classes = set()
source_schema_was_unset = len(arg.__class__.__bases__) == 2 and UnsetAnyTypeSchema in arg.__class__.__bases__
if not source_schema_was_unset:
"""
Do not include UnsetAnyTypeSchema and its base class because
it did not exist in the original spec schema definition
It was added to ensure that all instances are of type Schema and the allowed base types
"""
for cls in arg.__class__.__bases__:
if cls is Singleton:
# Skip Singleton
continue
schema_classes.add(cls)
for cls in arg.__class__.__bases__:
if cls is Singleton:
# Skip Singleton
continue
schema_classes.add(cls)
validated_path_to_schemas[path_to_item] = schema_classes

type_error = ApiTypeError(f"Invalid type. Required value type is str and passed type was {type(arg)} at {path_to_item}")
Expand Down Expand Up @@ -1845,6 +1854,7 @@ def __get_allof_classes(cls, arg, validation_metadata: ValidationMetadata):
path_to_schemas = defaultdict(set)
for allof_cls in cls.MetaOapg.all_of():
if validation_metadata.validation_ran_earlier(allof_cls):
add_deeper_validated_schemas(validation_metadata, path_to_schemas)
continue
other_path_to_schemas = allof_cls._validate_oapg(arg, validation_metadata=validation_metadata)
update(path_to_schemas, other_path_to_schemas)
Expand All @@ -1865,6 +1875,7 @@ def __get_oneof_class(
continue
if validation_metadata.validation_ran_earlier(oneof_cls):
oneof_classes.append(oneof_cls)
add_deeper_validated_schemas(validation_metadata, path_to_schemas)
continue
try:
path_to_schemas = oneof_cls._validate_oapg(arg, validation_metadata=validation_metadata)
Expand Down Expand Up @@ -1914,6 +1925,7 @@ def __get_anyof_classes(
for anyof_cls in cls.MetaOapg.any_of():
if validation_metadata.validation_ran_earlier(anyof_cls):
anyof_classes.append(anyof_cls)
add_deeper_validated_schemas(validation_metadata, path_to_schemas)
continue

try:
Expand Down Expand Up @@ -2037,6 +2049,7 @@ def _validate_oapg(

if discriminated_cls is not None and not updated_vm.validation_ran_earlier(discriminated_cls):
# TODO use an exception from this package here
add_deeper_validated_schemas(updated_vm, path_to_schemas)
assert discriminated_cls in path_to_schemas[updated_vm.path_to_item]
return path_to_schemas

Expand Down
Loading