Customize validation message example + documentation issues
See original GitHub issue@aldeed I am using this for my meteor project and I have migrate from meteor package to this node_module package together with collection-2-core package.
Currently I want to customize the validation messages for regEx because all error message for regEx is just “failed regular expression validation”, and I would like to give a more clear validation message such as “must contain at least 1 digit” etc.
However, I couldn’t find a correct way/clear example to do it. I checked the message-box package and still don’t know how to do it, and I found the documentation a bit confusing :
- the CHANGELOG in this README is not found
- at custom validation, it mentions using message-box but doen’s show clearly how to use it, I am still very new to Meteor and npm so may need more explanation on how to use it.
- at validate one key against another, it is using SimpleSchema.messages again instead of message-box which confuse me that which is the correct way to do.
- in message-box package, I am not sure where should I call the MessageBox.defaults() to make it effective (in import/ client folder?), also the
import MessageBox from 'message-box';
should be included in the Usage. - when I tried to follow the Manually Adding a Validation Error in imports/collections/Users.js with
SimpleSchema.messageBox.messages({
en: {
wrongPassword: "Wrong password"
}
});
It shows me messageBox is undefined even if I have imported both simpleschema and message-box npm packages.
I tried to customize with the code below:
MessageBox.defaults({
initialLanguage: 'en',
messages: {
en: {
required: '{{label}} is required',
minString: '{{label}} must be at least {{min}} characters',
maxString: '{{label}} cannot exceed {{max}} characters',
minNumber: '{{label}} must be at least {{min}}',
maxNumber: '{{label}} cannot exceed {{max}}',
minNumberExclusive: '{{label}} must be greater than {{min}}',
maxNumberExclusive: '{{label}} must be less than {{max}}',
minDate: '{{label}} must be on or after {{min}}',
maxDate: '{{label}} cannot be after {{max}}',
badDate: '{{label}} is not a valid date',
minCount: 'You must specify at least {{minCount}} values',
maxCount: 'You cannot specify more than {{maxCount}} values',
noDecimal: '{{label}} must be an integer',
notAllowed: '{{value}} is not an allowed value',
expectedType: '{{label}} must be of type {{dataType}}',
regEx: function ({
label,
type,
regExp,
}) {
console.log(label, type, regExp)
// See if there's one where exp matches this expression
let msgObj;
if (regExp) {
msgObj = _.find(regExpMessages, (o) => o.exp && o.exp.toString() === regExp);
}
var regExpMessageTail = 'failed regular expression validation';
if (regExp === /\d/) { // check for at least 1 digit
regExpMessageTail = 'must contain at least 1 digit';
}
var regExpMessage = msgObj ? msgObj.msg : regExpMessageTail;
return `${label} ${regExpMessage}`;
},
keyNotInSchema: '{{name}} is not allowed by the schema',
},
}
});
But it is not working. I understand that it is still in progress, but would be nice to add a clear example on customize validation messages for regEx. Thank you!
Issue Analytics
- State:
- Created 7 years ago
- Reactions:6
- Comments:7 (1 by maintainers)
Top GitHub Comments
I had a similar problem, I wanted to replace the built-in errors with translated versions:
Most code is taken from the messagebox defaults. Obviously you should put that messagebox in a file somewhere and import it always when you need it.
I wonder how this works: https://github.com/aldeed/node-simple-schema/blob/2b8f902e0d8d84eb923f9cf0cca2124f03f41cf8/lib/testHelpers/testSchema.js#L171
https://github.com/aldeed/node-simple-schema#customizing-validation-messages
This was not very well documented until yesterday, but I’m pretty sure there’s no actual issue here. If I’m wrong, I can reopen this.