Skip to content

Validation rules are applied in not required properties #1588

Closed

Description

@agregoris

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.

Activity

  1. changed the title [-]Validation rules are applied in not requires properties[/-] [+]Validation rules are applied in not required properties[/+] on Aug 13, 2015
  2. superkhau commented on Aug 13, 2015

    @superkhau
    Contributor

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

  3. agregoris commented on Aug 14, 2015

    @agregoris
    Author

    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.

  4. self-assigned this
    on Aug 14, 2015
  5. superkhau commented on Sep 2, 2015

    @superkhau
    Contributor

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

  6. rkmax commented on Sep 11, 2015

    @rkmax

    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

  7. superkhau commented on Sep 21, 2015

    @superkhau
    Contributor

    Closing, dupe of #1485.

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions