How to provide custom model validation message in Sails.js?
The validation messages returned by Sails.js is not user friendly so I wanted to provide a custom validation message for rules like required, minLength etc... but don't really know how. It's not in the docs and I also checked the docs of Anchor.js w/c is the validator used by Sails but its also not there.
UPDATE:
Haven't got a response last week so I implemented my own solution, wanted to share it since it might be useful for others - How I use custom validation messages in Sails.js
Another alternative and better way is to use the solution of @Rifat found in the comments below :)
Another very good alternative (credits to: sfb_ ) - https://gist.github.com/basco-johnkevin/8436644
They are expected to return true if valid and false otherwise. Out of the box, Sails.js does not support custom validation messages.
You can find detailed usage documentation for methods like these in Reference > Waterline (ORM) > Models. Every model in Sails has a set of methods exposed on it to allow you to interact with the database in a normalized fashion. This is the primary way of interacting with your app's data.
If an attribute declares itself unique: true, then Sails ensures that no two records will be allowed with the same value. The canonical example is an emailAddress attribute on a User model: Why is unique different from other validations? Imagine you have 1,000,000 user records in your database.
This data type guarantee is used for logical validation and coercion of results and criteria. Here is a list of the data types supported by Sails and Waterline: Any string. Any number. true or false. Any JSON-serializable value, including numbers, booleans, strings, arrays, dictionaries (plain JavaScript objects), and null.
Since Sails.js doesn't yet support using custom model validation messages, we can use these solutions:
1) @johnkevinmbasco's solution
2) @sfb_'s solution
3) @rifats solution
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With