Validation rules are applied in not required properties
When I define a property not required like "languages"
{
"name": "guide",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"languages": {
"type": [
"string"
]
},
And I define custom validation rules for this property like these:
Guide.validatesLengthOf('languages', {min: 1, message: 'You need to add 1 language at least'});
Guide.validate('languages', validatesLanguages, {message: 'Languages are not in supported languages'});
Loopback always try to check these rules even when I try to post a new guide without "languages" property, I remember "languages" is not required then whenever I try to create a guide without "languages" I think Loopback should not validate the non-exist property and should only do it when the property exists.
Can you provide a link to a test project on Github? See https://github.com/strongloop/loopback/wiki/Issues#bug-report
I create the repository, the URL is: https://github.com/onnoll/loopback-sandbox
STP:
- Run the project:
node .
- Go to StrongLoop API explorer. Type http://localhost:3000/explorer in your browser and go ahead.
- Try to create a new guide without languages property. POST an empty object in /guides endpoint.
As you can appreciate the validation rules have effect on "languages" property and the server rejects the request although I don't require this property to create a guide. I think that the validation rules should have effect just whenever you add the property to the payload.
I think this is a dupe of #1485. Can you confirm and close if that is the case?
I'm having the same issue, my users can sign up using his email either phone number, I want add uniqueness and format validations to both properties but only if they are in the payload
Closing, dupe of #1485.