Ability to override validation fields #4880

Open
joetidee opened this Issue Jan 8, 2017 · 2 comments

Projects

None yet

3 participants

@joetidee
joetidee commented Jan 8, 2017 edited

This is a feature request. It would be good to be able to explicitly state which fields can be validated, perhaps by supplying an array of field names as a parameter to the validate function:

doc.validate(['name','age',...], function(error){ ... }

Example 1: lets say that a web form has two stages - address information is entered into the first stage and saved, then the user moves onto the second stage, which asks for their personal interests, and saves that. Both address and personal interest information is required: true in the User schema, but when Mongoose validates stage 1 against this schema it cannot simply validate the personal information only as is will throw a validation error.

Example 2: I have a schema whereby the created_date field is set to required: true, however, this field will only be set server-side just before the document is saved. So, if I want to use the same schema to validate client-side, I this case when I try and validate client-side, it will throw a validation error.

I think that this would be a good first step to making schema usage isomorphic. I have used the jagi-astronomy package (used in Meteor applications), which I think look it's inspiration from Mongoose package. The Astronomy package allow for partial field validation as described above.

@varunjayaraman
Collaborator

This feature exists in document#validateSync. i.e. you can do:

const errors = theDoc.validateSync(['data.name', 'admin.readableId']);
@vkarpov15 vkarpov15 reopened this Jan 11, 2017
@vkarpov15 vkarpov15 added this to the 4.11 milestone Jan 11, 2017
@vkarpov15
Collaborator

Would be nice to add this for regular validate at some point

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment