Skip to content
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

Custom validation message for array elements #62

Closed
tbknl opened this issue Jan 30, 2014 · 2 comments
Closed

Custom validation message for array elements #62

tbknl opened this issue Jan 30, 2014 · 2 comments

Comments

@tbknl
Copy link

tbknl commented Jan 30, 2014

I'm trying to write a custom validation message for keys in an array of sub-schema elements. The message keys are matched literally, while my key includes the array index of the element: groups.0.name. Here is the (simplified) relevant schema code:

var group = new SimpleSchema({
  name: {
    type: String
  }
});

var schema = new SimpleSchema({
  groups: {
    type: [group]
  }
});

I have two potential solutions in mind:

Solution 1. It would be great if I could either add a message to the messages object of the group object from the above example, which will be automatically picked up when validating against the parent schema schema. This solution could also be taken into account, when working on issue #46. Like this:

group.messages({
  'required name': 'my_error_message'
});

Solution 2. Add a message to the messages object of the schema object with a wildcard for matching the array index. Example:

schema.messages({
  'required groups.$.name': 'my_error_message'
});
@aldeed
Copy link
Collaborator

aldeed commented Jan 30, 2014

I think solution 2 would be best and easiest. Solution 1 would introduce a layer of complexity that goes beyond its benefits I think.

There is already a variety of checking for messages, from most specific to least specific, so I can add an additional check for the generic key if the specific key isn't there.

@tbknl
Copy link
Author

tbknl commented Jan 31, 2014

Awesome! Let me know if I can do anything to help.

@aldeed aldeed closed this as completed in 2ff10e6 Feb 4, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants