-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Closed
Description
I'm confused if nested validation it's supported, how it works and if it's possible do the situation described below
I have the following models
Customerbased onPersistedModelcontactproperty typeContactInfoaddressproperty typeAddress- ...
ContactInfobased onModelemailAddressproperty type stringphoneNumberproperty type stringfirstNameproperty type stringlastNameproperty type string
Addressbased onModelstreetproperty type stringcityproperty type stringstateproperty type stringzipCodeproperty type string
I also want to validate
contact.emailAddressis unique, but not requiredcontact.phoneNumberis unique, but not required
I want know if it's possible validate only when it's present in the payload and using the default validators e.g. validatesUniquenessOf
Currently I've added a validator
//contact-info.js
module.exports = function(ContactInfo) {
ContactInfo.validatesUniquenessOf('phoneNumber', {message: 'You can\'t use this number', allowNull: true});
};
for testing proposes I have in my database a document in the Customer collection
{
"_id": ObjectId("56003b7901caa9db416eca5f"),
"contact": {
"firstName" "Bob",
"lastName" "SquarePants",
"phoneNumber": "555-5678"
}
}
and sent following request
POST /api/Customers
{
"contact": {
"firstName" "Patrick",
"lastName" "Star",
"phoneNumber": "555-5678"
}
}
the expected result is a 422 response but I get a 200, btw I think it should be a 201