Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide custom model validation message in Sails.js?

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

like image 439
John Kenn Avatar asked Dec 25 '13 00:12

John Kenn


People also ask

What are validation messages expected to return in sails?

They are expected to return true if valid and false otherwise. Out of the box, Sails.js does not support custom validation messages.

Where can I find the documentation for models in sails?

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.

What is a unique attribute in sails?

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.

What types of data types are supported by sails?

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.


1 Answers

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

like image 133
John Kenn Avatar answered Oct 30 '22 09:10

John Kenn