Skip to content

Commit b26386a

Browse files
Merge pull request #542 from yubarajshrestha/fix/541
fixes #541, by returning value instead of indexed value
2 parents af7b934 + 3f0b029 commit b26386a

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

Diff for: src/masonite/input/InputBag.py

+27
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,34 @@ def get(self, name, default=None, clean=True, quote=True):
120120
return rendered
121121
elif hasattr(input, "value"):
122122
if isinstance(input.value, list) and len(input.value) == 1:
123+
"""
123124
return input.value[0]
125+
126+
This line is converting request input list to object if the input is a list having only one item
127+
128+
Problem:
129+
1. This will make validations and request input confusing as a developer is sending array where as
130+
they will get dict in controller, this is actually a bug rather than a feature
131+
2. In case of nested validations, this will make the validation to fail
132+
Example:
133+
input => {
134+
"test": [
135+
{
136+
"foo": "bar"
137+
}
138+
],
139+
"test_1": {
140+
"foo": "bar"
141+
}
142+
}
143+
validation => validate.required(["test.*.foo"])
144+
145+
In above example `test` and `test_1` are not same but this code `input.value[0]` will make them treat as same
146+
147+
Solution:
148+
return the input value without removing anything...
149+
"""
150+
return input.value
124151
elif isinstance(input.value, dict):
125152
return input.value
126153
return input.value

Diff for: tests/features/validation/test_validation.py

+14
Original file line numberDiff line numberDiff line change
@@ -1753,3 +1753,17 @@ def test_required_with_string_validation(self):
17531753
"The email is required because one in first_name,nick_name is present.",
17541754
validate.get("email"),
17551755
)
1756+
1757+
def test_required_with_nested_validation(self):
1758+
# with one argument
1759+
validate = Validator().validate(
1760+
{"key": [{"foo": "bar"}]},
1761+
required(['key.*.foo'])
1762+
)
1763+
self.assertEqual(len(validate), 0)
1764+
validate = Validator().validate(
1765+
{"key": [{"foo": "bar"}, {"foo1": "bar1"}]},
1766+
required(['key.*.foo'])
1767+
)
1768+
self.assertEqual(len(validate), 1)
1769+
self.assertEqual(validate.all()['key.*.foo'], ['The key.*.foo field is required.'])

0 commit comments

Comments
 (0)