loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Validation rules are applied in not required properties

Open agregoris opened this issue 11 years ago • 5 comments

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.

agregoris avatar Aug 13 '15 10:08 agregoris

Can you provide a link to a test project on Github? See https://github.com/strongloop/loopback/wiki/Issues#bug-report

superkhau avatar Aug 13 '15 21:08 superkhau

I create the repository, the URL is: https://github.com/onnoll/loopback-sandbox

STP:

  1. Run the project:
  node .
  1. Go to StrongLoop API explorer. Type http://localhost:3000/explorer in your browser and go ahead.
  2. 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.

agregoris avatar Aug 14 '15 10:08 agregoris

I think this is a dupe of #1485. Can you confirm and close if that is the case?

superkhau avatar Sep 02 '15 22:09 superkhau

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

rkmax avatar Sep 11 '15 17:09 rkmax

Closing, dupe of #1485.

superkhau avatar Sep 21 '15 20:09 superkhau