Skip to content

Commit

Permalink
fix: pass the valid null object (#17) (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
daigotanaka authored Jul 12, 2021
1 parent 3de4012 commit 1069ca1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions getschema/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,13 @@ def fix_type(obj, schema, dict_path=[], on_invalid_property="raise",
if not nullable:
if on_invalid_property == "raise":
raise ValueError("Null object given at %s" % dict_path)
return None
return None

# Recurse if object or array types
if obj_type == "object":
if type(obj) is not dict:
raise KeyError("property type (object) Expected a dict object." +
"Got: %s %s" % (type(obj), str(obj)))
"Got: %s %s at %s" % (type(obj), str(obj), str(dict_path)))
cleaned = dict()
keys = obj.keys()
for key in keys:
Expand Down
20 changes: 17 additions & 3 deletions tests/test_fix_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@
"some_prop": "1",
},
}

null_entries = {
"index": None,
"array": [
"1.5",
None,
],
"nested_field": {
"some_prop": "3",
},
"nested_field": None,
"boolean_field": None,
"number_field": None,
"string_field": None,
}

invalid_datetime_record = {
"index": 2,
"array": [
Expand Down Expand Up @@ -236,3 +236,17 @@ def test_reject_null_string():
assert(str(e).startswith("Null object given at"))
else:
raise Exception("Supposed to fail with null value")


def test_reject_null_object():
schema = getschema.infer_schema(records)
# This will pass
_ = getschema.fix_type(null_entries, schema)

schema["properties"]["nested_field"]["type"] = ["object"]
try:
_ = getschema.fix_type(null_entries, schema)
except Exception as e:
assert(str(e).startswith("Null object given at"))
else:
raise Exception("Supposed to fail with null value")

0 comments on commit 1069ca1

Please sign in to comment.