-
-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow custom validation error messages for all types, not just regex fails #3
Comments
This is another thing I was planning to do once someone asked for it. Do you have any implementation ideas? I was thinking of providing an API function that allows you to specify the message for each error type, with support for a few basic placeholders. Like: MySimpleSchema.messages({
"required": "[label] is required",
"stringMax": "[label] cannot exceed [max] characters"
}); Might be nice to support field-specific overrides, too. Could just support a field name in the key, separated by a space, like: MySimpleSchema.messages({
"required": "[label] is required",
"required firstName": "I know it's a pain, but we need to insist that you enter your first name.",
"stringMax": "[label] cannot exceed [max] characters"
}); I can probably implement this later today or tomorrow, unless you have a better idea. |
Maybe its better to let the requirements themselves contain messages: MySchema( { On Mon, Aug 19, 2013 at 7:49 AM, Eric Dobbertin notifications@github.comwrote:
|
I thought of that, too, but there are a number of downsides:
|
A couple of thoughts on this: I think there should be both a messages function (and/or messages argument) to define/change default messages, as well as the ability to override with custom messages in the individual requirements. This gives the most flexibility while allowing code to be more terse if desired. Plus it makes more sense to me to put custom messages along with the requirements themselves. i18n should probably be passed onto a separate i18n package, right? Something that can store strings by keys in different languages and then retrieve the correct string in the current locale. Also, if required isn't associated with a schema key, then shouldn't the optional property be replaced with "required"? So this could work something like (using the simple-i18n package): //at some point discover and assign the current locale
locale = "us-en";
...
var i18n = Meteor.I18n().collection;
MySchema = new SimpleSchema({
"name": {
required: {true, i18n.findOne({lang: locale, base_str: "schemaNameRequired"})},
min: 3,
max: {10, i18n.findOne({lang: locale, base_str: "schemaNameMax"})}
}
}, {
required: i18n.findOne({lang: locale, base_str: "schemaRequired"}),
min: i18n.findOne({lang: locale, base_str: "schemaMin"}),
max: i18n.findOne({lang: locale, base_str: "schemaMax"})
}); So the key "name" would use the i18n messages for the "us-en" locale under the keys "schemaNameRequired", "schemaNameMax", and "schemaMin". Not that anyone would have to use simple-i18n, but I think this would let you delegate i18n out of this package. |
I find it kind of confusing to have the custom messages in one place and the generic messages in another place. I also think it makes the schema a little harder to read. If I'm alone in this, I would not be opposed to supporting the custom message definitions in the schema in addition to the message definitions object. The reason I chose to use the I do agree that externalizing I18N is best. I know a lot of validation plugins and frameworks make you specify I any case, this is all implemented in the package already now, so maybe folks can play around with it and see what they do and don't like about the current implementation. |
@aldeed fair point on the messages function |
No description provided.
The text was updated successfully, but these errors were encountered: