Skip to content

Commit

Permalink
Merge pull request #542 from yubarajshrestha/fix/541
Browse files Browse the repository at this point in the history
fixes #541, by returning value instead of indexed value
  • Loading branch information
josephmancuso committed Feb 27, 2022
2 parents af7b934 + 3f0b029 commit b26386a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/masonite/input/InputBag.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,34 @@ def get(self, name, default=None, clean=True, quote=True):
return rendered
elif hasattr(input, "value"):
if isinstance(input.value, list) and len(input.value) == 1:
"""
return input.value[0]
This line is converting request input list to object if the input is a list having only one item
Problem:
1. This will make validations and request input confusing as a developer is sending array where as
they will get dict in controller, this is actually a bug rather than a feature
2. In case of nested validations, this will make the validation to fail
Example:
input => {
"test": [
{
"foo": "bar"
}
],
"test_1": {
"foo": "bar"
}
}
validation => validate.required(["test.*.foo"])
In above example `test` and `test_1` are not same but this code `input.value[0]` will make them treat as same
Solution:
return the input value without removing anything...
"""
return input.value
elif isinstance(input.value, dict):
return input.value
return input.value
Expand Down
14 changes: 14 additions & 0 deletions tests/features/validation/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1753,3 +1753,17 @@ def test_required_with_string_validation(self):
"The email is required because one in first_name,nick_name is present.",
validate.get("email"),
)

def test_required_with_nested_validation(self):
# with one argument
validate = Validator().validate(
{"key": [{"foo": "bar"}]},
required(['key.*.foo'])
)
self.assertEqual(len(validate), 0)
validate = Validator().validate(
{"key": [{"foo": "bar"}, {"foo1": "bar1"}]},
required(['key.*.foo'])
)
self.assertEqual(len(validate), 1)
self.assertEqual(validate.all()['key.*.foo'], ['The key.*.foo field is required.'])

0 comments on commit b26386a

Please sign in to comment.