Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gadelkareem/8a6668e8fba7c6833ec7 to your computer and use it in GitHub Desktop.
Save gadelkareem/8a6668e8fba7c6833ec7 to your computer and use it in GitHub Desktop.
Custom validation messages in blueprint for sails.js
// For adding custom messages if using blueprint
// In reponses/badrequest.js
// after
// if (sails.config.environment === 'production') {
// data = undefined;
// }
// add
if (data.invalidAttributes !== undefined) {
var attributes = data.invalidAttributes;
_.each(attributes, function (attribute, key) {
_.each(attribute, function (properties) {
var rule = properties.rule;
var message = false;
try {
message = sails.__('validation.' + key + '.' + rule, key);
}
catch (e) {
}
try {
message = sails.__('validation.' + rule, key);
}
catch (e) {
}
if (message && message != 'validation.' + rule && message != 'validation.' + key + '.' + rule) {
properties.message = message;
}
});
});
}
// In config/i18n.js
//add
objectNotation: true
// example :
//In config/locales/en.json or your locale file
//add
"validation": {
"isJson": "invalid JSON format in %s"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment