Skip to content

Commit

Permalink
support dropping unknown properties
Browse files Browse the repository at this point in the history
  • Loading branch information
daigotanaka committed Jul 17, 2024
1 parent d75ee88 commit f4395c1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions getschema/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,16 @@ def infer_from_file(filename, fmt="json", skip=0, lower=False,
return schema


class DroppedProperty(object):
pass


def fix_type(
obj,
schema,
dict_path=[],
on_invalid_property="raise",
drop_unknown_properties=False,
lower=False,
replace_special=False,
snake_case=False,
Expand All @@ -306,9 +311,12 @@ def fix_type(
- raise: Raise exception
- null: Impute with null
- force: Keep it as is (string)
- drop_unknown_properties: True/False
If true, the returned object will exclude unknown (sub-)properties
"""
kwargs = {
"on_invalid_property": on_invalid_property,
"drop_unknown_properties": drop_unknown_properties,
"lower": lower,
"replace_special": replace_special,
"snake_case": snake_case,
Expand Down Expand Up @@ -347,6 +355,8 @@ def fix_type(
cleaned = dict()
keys = obj.keys()
for key in keys:
if drop_unknown_properties and not _nested_get(schema, dict_path + ["properties", key] + ["type"]):
continue
try:
ret = fix_type(obj[key], schema, dict_path + ["properties", key],
**kwargs)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_fix_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,12 @@ def test_undefined_sub_property():
assert(str(e).startswith("Unknown property found at: ['properties', 'nested_field', 'properties', 'foo']"))
else:
raise Exception(f"Supposed to fail with null value.")


cleaned = getschema.fix_type(
records[1],
schema,
on_invalid_property="raise",
drop_unknown_properties=True,
)
assert(cleaned["nested_field"].get("foo") == None)

0 comments on commit f4395c1

Please sign in to comment.