Skip to content

Commit

Permalink
Rename ValidationError "msg" to "err" (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyfo authored Nov 11, 2021
1 parent fe7586c commit a300a68
Show file tree
Hide file tree
Showing 23 changed files with 36 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: cythonize
if: matrix.compile
run: |
python -m pip install cython ${{ matrix.compile && (matrix.python-version == '3.6' || matrix.python-version == 'pypy3') && 'dataclasses' || '' }}
python -m pip install cython ${{ (matrix.python-version == '3.6' || matrix.python-version == 'pypy3') && 'dataclasses' || '' }}
python scripts/cythonize.py
python setup.py build_ext --inplace
- name: Install tox
Expand Down
6 changes: 3 additions & 3 deletions apischema/validation/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

class LocalizedError(TypedDict):
loc: Sequence[ErrorKey]
msg: ErrorMsg
err: ErrorMsg


except ImportError:
Expand Down Expand Up @@ -80,13 +80,13 @@ def _errors(self) -> Iterator[Tuple[List[ErrorKey], ErrorMsg]]:

@property
def errors(self) -> List[LocalizedError]:
return [{"loc": path, "msg": error} for path, error in self._errors()]
return [{"loc": path, "err": error} for path, error in self._errors()]

@staticmethod
def from_errors(errors: Sequence[LocalizedError]) -> "ValidationError":
return reduce(
merge_errors,
[_rec_build_error(err["loc"], err["msg"]) for err in errors],
[_rec_build_error(err["loc"], err["err"]) for err in errors],
ValidationError(),
)

Expand Down
2 changes: 1 addition & 1 deletion examples/computed_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ def password_match(self):
deserialize(PasswordForm, {"password": "p455w0rd"})
assert err.value.errors == [
# validator is not executed because confirmation is missing
{"loc": ["confirmation"], "msg": "missing property"}
{"loc": ["confirmation"], "err": "missing property"}
]
2 changes: 1 addition & 1 deletion examples/dependent_required.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ class Billing:
assert err.value.errors == [
{
"loc": ["billing_address"],
"msg": "missing property (required by ['credit_card'])",
"err": "missing property (required by ['credit_card'])",
}
]
4 changes: 2 additions & 2 deletions examples/discard.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def bounds_are_sorted_equivalent(bounded: BoundedValues):
with raises(ValidationError) as err:
deserialize(BoundedValues, {"bounds": [10, 0], "values": [-1, 2, 4]})
assert err.value.errors == [
{"loc": ["bounds"], "msg": "bounds are not sorted"}
{"loc": ["bounds"], "err": "bounds are not sorted"}
# Without discard, there would have been an other error
# {"loc": ["values", 1], "msg": "value exceeds bounds"}
# {"loc": ["values", 1], "err": "value exceeds bounds"}
]
6 changes: 4 additions & 2 deletions examples/examples/pydantic_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def deserialize_pydantic(data):
try:
return tp.parse_obj(data)
except pydantic.ValidationError as error:
raise ValidationError.from_errors(error.errors())
raise ValidationError.from_errors(
[{"loc": err["loc"], "err": err["msg"]} for err in error.errors()]
)

return Conversion(
deserialize_pydantic,
Expand Down Expand Up @@ -80,5 +82,5 @@ class Foo(pydantic.BaseModel):
with raises(ValidationError) as err:
deserialize(Foo, {"bar": "not an int"})
assert err.value.errors == [
{"loc": ["bar"], "msg": "value is not a valid integer"} # pydantic error message
{"loc": ["bar"], "err": "value is not a valid integer"} # pydantic error message
]
2 changes: 1 addition & 1 deletion examples/field_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ class Foo:

with raises(ValidationError) as err:
deserialize(Foo, {"bar": "11"})
assert err.value.errors == [{"loc": ["bar"], "msg": "number has duplicate digits"}]
assert err.value.errors == [{"loc": ["bar"], "err": "number has duplicate digits"}]
2 changes: 1 addition & 1 deletion examples/not_null.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ class Foo:
with raises(ValidationError) as err:
deserialize(Foo, {"bar": None})
assert err.value.errors == [
{"loc": ["bar"], "msg": "expected type integer, found null"}
{"loc": ["bar"], "err": "expected type integer, found null"}
]
2 changes: 1 addition & 1 deletion examples/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Resource:
with raises(ValidationError) as err: # pytest checks exception is raised
deserialize(Resource, {"id": "42", "name": "wyfo"})
assert err.value.errors == [
{"loc": ["id"], "msg": "badly formed hexadecimal UUID string"}
{"loc": ["id"], "err": "badly formed hexadecimal UUID string"}
]
# Generate JSON Schema
assert deserialization_schema(Resource) == {
Expand Down
2 changes: 1 addition & 1 deletion examples/required.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ class Foo:

with raises(ValidationError) as err:
deserialize(Foo, {})
assert err.value.errors == [{"loc": ["bar"], "msg": "missing property"}]
assert err.value.errors == [{"loc": ["bar"], "err": "missing property"}]
2 changes: 1 addition & 1 deletion examples/settings_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

with raises(ValidationError) as err:
deserialize(list[int], [0, 1, 2, 3], schema=schema(max_items=3))
assert err.value.errors == [{"loc": [], "msg": "too-many-items: 4 > 3"}]
assert err.value.errors == [{"loc": [], "err": "too-many-items: 4 > 3"}]
4 changes: 2 additions & 2 deletions examples/tagged_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class Foo(TaggedUnion):

with raises(ValidationError) as err:
deserialize(Foo, {"unknown": 42})
assert err.value.errors == [{"loc": ["unknown"], "msg": "unexpected property"}]
assert err.value.errors == [{"loc": ["unknown"], "err": "unexpected property"}]

with raises(ValidationError) as err:
deserialize(Foo, {"bar": {"field": "value"}, "baz": 0})
assert err.value.errors == [
{"loc": [], "msg": "property count greater than 1 (maxProperties)"}
{"loc": [], "err": "property count greater than 1 (maxProperties)"}
]
2 changes: 1 addition & 1 deletion examples/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ def not_equal(self):

with raises(ValidationError) as err:
validate(Foo(2, 2))
assert err.value.errors == [{"loc": [], "msg": "bar cannot be equal to baz"}]
assert err.value.errors == [{"loc": [], "err": "bar cannot be equal to baz"}]
8 changes: 4 additions & 4 deletions examples/validation_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class Resource:
Resource, {"id": 42, "tags": ["tag", "duplicate", "duplicate", "bad&", "_"]}
)
assert err.value.errors == [
{"loc": ["tags"], "msg": "item count greater than 3 (maxItems)"},
{"loc": ["tags"], "msg": "duplicate items (uniqueItems)"},
{"loc": ["tags", 3], "msg": "not matching pattern ^\\w*$ (pattern)"},
{"loc": ["tags", 4], "msg": "string length lower than 3 (minLength)"},
{"loc": ["tags"], "err": "item count greater than 3 (maxItems)"},
{"loc": ["tags"], "err": "duplicate items (uniqueItems)"},
{"loc": ["tags", 3], "err": "not matching pattern ^\\w*$ (pattern)"},
{"loc": ["tags", 4], "err": "string length lower than 3 (minLength)"},
]
2 changes: 1 addition & 1 deletion examples/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ def password_match(self):
with raises(ValidationError) as err:
deserialize(PasswordForm, {"password": "p455w0rd", "confirmation": "..."})
assert err.value.errors == [
{"loc": [], "msg": "password doesn't match its confirmation"}
{"loc": [], "err": "password doesn't match its confirmation"}
]
2 changes: 1 addition & 1 deletion examples/validator_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ def check_parity_other_equivalent(number2: NumberWithParity):

with raises(ValidationError) as err:
deserialize(NumberWithParity, {"parity": "even", "number": 1})
assert err.value.errors == [{"loc": ["number"], "msg": "number doesn't respect parity"}]
assert err.value.errors == [{"loc": ["number"], "err": "number doesn't respect parity"}]
4 changes: 2 additions & 2 deletions examples/validator_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def check_palindrome(s: Palindrome):
assert deserialize(Palindrome, "tacocat") == "tacocat"
with raises(ValidationError) as err:
deserialize(Palindrome, "palindrome")
assert err.value.errors == [{"loc": [], "msg": "Not a palindrome"}]
assert err.value.errors == [{"loc": [], "err": "Not a palindrome"}]

# Using Annotated
with raises(ValidationError) as err:
deserialize(Annotated[str, validators(check_palindrome)], "palindrom")
assert err.value.errors == [{"loc": [], "msg": "Not a palindrome"}]
assert err.value.errors == [{"loc": [], "err": "Not a palindrome"}]
2 changes: 1 addition & 1 deletion examples/validator_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ class CompleteForm(PasswordForm):
{"username": "wyfo", "password": "p455w0rd", "confirmation": "..."},
)
assert err.value.errors == [
{"loc": [], "msg": "password doesn't match its confirmation"}
{"loc": [], "err": "password doesn't match its confirmation"}
]
2 changes: 1 addition & 1 deletion examples/validator_post_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ def validate(self, bar: int):

with raises(ValidationError) as err:
deserialize(Foo, {"bar": -1})
assert err.value.errors == [{"loc": ["bar"], "msg": "negative"}]
assert err.value.errors == [{"loc": ["bar"], "err": "negative"}]
4 changes: 2 additions & 2 deletions examples/validator_yield.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ def check_ips_in_subnet(self):
{"subnet": "126.42.18.0/24", "ips": ["126.42.18.1", "126.42.19.0", "0.0.0.0"]},
)
assert err.value.errors == [
{"loc": ["ips", 1], "msg": "ip not in subnet"},
{"loc": ["ips", 2], "msg": "ip not in subnet"},
{"loc": ["ips", 1], "err": "ip not in subnet"},
{"loc": ["ips", 2], "err": "ip not in subnet"},
]
2 changes: 1 addition & 1 deletion tests/integration/test_unsupported_union_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ def test_unsupported_union_member():
with raises(ValidationError) as err:
deserialize(Foo, {"bar": None})
assert err.value.errors == [
{"loc": ["bar"], "msg": "expected type integer, found null"}
{"loc": ["bar"], "err": "expected type integer, found null"}
]
2 changes: 1 addition & 1 deletion tests/integration/test_validator_aliasing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ def validate_a(self):
def test_validator_aliasing():
with raises(ValidationError) as err:
deserialize(A, {"A": 42}, aliaser=str.upper)
assert err.value.errors == [{"loc": ["A", "A", "b", 0, "C"], "msg": "error 42"}]
assert err.value.errors == [{"loc": ["A", "A", "b", 0, "C"], "err": "error 42"}]
4 changes: 2 additions & 2 deletions tests/validation/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def test_validate():
validate(Data(42, 0))
with raises(ValidationError) as err:
validate(Data(0, 0))
assert err.value.errors == [{"loc": [], "msg": "error"}]
assert err.value.errors == [{"loc": [], "err": "error"}]
with raises(ValidationError) as err:
validate(Data(200, 0))
assert err.value.errors == [{"loc": [], "msg": "error2"}]
assert err.value.errors == [{"loc": [], "err": "error2"}]


def test_non_trivial():
Expand Down

0 comments on commit a300a68

Please sign in to comment.