Skip to content

Commit

Permalink
Fixed issue with schema declared built in types
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusfrdk committed Feb 4, 2025
1 parent 6a2decc commit 4bc0d82
Showing 1 changed file with 23 additions and 36 deletions.
59 changes: 23 additions & 36 deletions tomlval/toml_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,46 +107,33 @@ def _get_missing_keys(self) -> list[str]:

def _get_invalid_types(
self,
) -> List[Tuple[str, Tuple[Any, TypeList, TypeList]]]:
) -> dict[str, Tuple[str, Tuple[Any, TypeList, TypeList]]]:
"""Get a list of keys with invalid types."""
invalid_types = []
invalid_types = {}

if not isinstance(self._schema, TOMLSchema):
return invalid_types

for key, value in self._data.items():
if key in self._schema:
# List of types
if isinstance(self._schema[key], list):

# Check if any of the types are valid
if isinstance(value, list):
invalid_list_types = set()
for t in value:
if type(t) not in self._schema[key]:
invalid_list_types.add(type(t))
invalid_list_types = list(invalid_list_types)
else:
invalid_list_types = type(value)

if invalid_list_types:
invalid_types.append(
(
key,
(value, self._schema[key], invalid_list_types),
)
)

# Single type
elif not isinstance(value, self._schema[key]):
types = (
self._schema[key]
if isinstance(self._schema[key], type)
else type(value)
)
invalid_types.append(
(key, (value, self._schema[key], types))
)
for k, h in self._schema.items():
if k not in self._data:
continue

# Built in type
if isinstance(h, type):
value = self._data[k]
if not isinstance(value, h):
invalid_types[k] = ("invalid-type", (value, h, type(value)))
continue

# List of build in types
if isinstance(h, (list, tuple)):
_value = self._data[k]
_type = type(_value)

if not any(isinstance(_value, t) for t in h):
invalid_types[k] = ("invalid-type", (_value, h, _type))

continue

return invalid_types

Expand Down Expand Up @@ -279,7 +266,7 @@ def validate(self) -> ValidatedSchema:

errors = {
**{k: ("missing", None) for k in missing_keys},
**{k: ("invalid-type", v) for k, v in invalid_types},
**invalid_types,
**{
k: ("handler", v)
for k, v in handler_results.items()
Expand Down

0 comments on commit 4bc0d82

Please sign in to comment.