angular-schema-form icon indicating copy to clipboard operation
angular-schema-form copied to clipboard

minLength validation not working in array items

Open frontendbeast opened this issue 10 years ago • 9 comments

In the example below, a minLength of 1 is set on the array items, but the form allows submission with an empty array item field.

http://schemaform.io/examples/bootstrap-example.html#/67dd0bb3fbacee29cd39

However, if I set items to be objects and set the name field to be required, then it will work, but that leaves the form looking odd.

http://schemaform.io/examples/bootstrap-example.html#/c6e3ec60d055408d74e9

frontendbeast avatar Jul 27 '15 16:07 frontendbeast

Looks to me like it's working for array items the same as everywhere else. The problem seems to be that it doesn't report an empty string field as failing the validation. It only starts checking minLength when at least one character has been entered.

gazpachoking avatar Jul 27 '15 21:07 gazpachoking

There is a bug in minLength validation (I've submitted pull request #486 for that) but even with that fixed the problem persists.

frontendbeast avatar Jul 27 '15 21:07 frontendbeast

OK, so I've found the problem. The validator sets 0 length string values to be undefined. I added console.log(data, typeof data, data.length); at the top of the tv4 validateStringLength function and found that strings are passed as expected when they have length:

{0: "test"} "object" undefined
test string 4

But when the undefined is passed, it creates a 0 length object, which tv4 returns null.

{} "object" undefined

Changing the angular-schema-form validate function to remove the empty string undefined conversion solves the problem:

{0: ""} "object" undefined
 string 0

It does not seem to break validation on any of the other required fields as the comments above the conversion suggest, but I don't have an in depth knowledge of the plugin, so I could be missing something.

frontendbeast avatar Jul 28 '15 08:07 frontendbeast

So the above only fixes minLength checks on keyboard input, the issue is still present on form submission.

http://schemaform.io/examples/bootstrap-example.html#/a8016f12f91cc910b115

The form above does not follow the JSON Schema as it allows the empty Column field to be submitted. If you paste the schema and the model into http://jsonschemalint.com it correctly identifies the error, even if you change the model value from null to "".

frontendbeast avatar Jul 28 '15 14:07 frontendbeast

+1 for this fix

Alttaf avatar Oct 05 '15 16:10 Alttaf

Another +1

AndrewIsh avatar Nov 09 '15 16:11 AndrewIsh

+1

melnikaite avatar Jan 18 '16 17:01 melnikaite

+1

williamhgough avatar Jan 20 '16 13:01 williamhgough

+1 for someone submitting a PR. :-)

nicklasb avatar Apr 15 '16 15:04 nicklasb