English

Creating a generic validator for arrays of strings in Lithium

Today i needed to validate some input from a form before passing it into MongoDB in Lithium and found a shortcoming in the inbuilt validation system. It does not support array of values.

Consider the following simple validation rule on a string input that works for blocking empty string for appearing in your database:

Try passing an array to “foo” and it fails nicely. After filing a ticket for this kind of feature and discussed it on the IRC channel, I sat down and created a middleground solution by building my own validator that reused the inbuilt validator patterns for strings, only supporting array usage. Check this out:

I hope we can see this functionality in Lithium itself, but for now this little validator allows you to reuse any (not tested on all) validations found in Validator. Enjoy

About Raymond Julin

Lead developer at Keyteq Labs, a product business in Bergen, Norway with a reputation for modern user friendly solutions.