Skip to content

Validators defined in models .js file does not work on submodels #1485

Closed

Description

@Nutelac

Hello, I'm using isomorphic loopback in combination with React.js and trying to use validators on client side. Everything works well with server models, but not work with local ones.

User.json

{
  "name": "User",
  "base": "PersistedModel",
  "persistUndefinedAsNull": true,
  "trackChanges": true,
  "properties": {
    "id": {
      "id": true,
      "type": "string",
      "required": true,
      "defaultFn": "guid"
    },
    "name": {
      "type": "string",
      "required": true
    }
  }
}

User.js

export default (User) => {
  User.validatesLengthOf('name', { min: 5 }) // name should be longer than or equal to 5 chars
}

LocalUser.json

{
  "name": "LocalUser",
  "base": "User",
  "enableRemoteReplication": true
}

browser code

import { models } from 'loopback-client'

// ----- Remote model ------

let user = models.User({
  name: 'Bob' // 3 chars, 5 is minimum
})

user.isValid() // false

// ----- Local model ------

let localUser = models.LocalUser({
  name: 'Bob'
})

localUser.isValid() // true, should be false

I don't think it's expected behaviour.

Thank you.

edit:
It's interesting that required: true validator from User.json file works well on client (maybe because base: "User" in LocalUser.json ignore User.js file?)

let localUser = models.LocalUser({
  name: ''
})

localUser.isValid() // false

When I change it to required: false, isValid() returns true.

edit 2:
I found dirty solution. Creating LocalUser.json with same content as User.json solve this problem (after that length validator works).

Activity

  1. changed the title [-]Validators does not works on client[/-] [+]Validators defined in models .js file does not work on submodels[/+] on Jun 25, 2015
  2. superkhau commented on Jul 6, 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. Kr0san89 commented on Aug 13, 2015

    @Kr0san89

    Hello

    I created a Fork (https://github.com/Kr0san89/loopback-sandbox) like in the description, i was the guy who did the 2nd issue #1500 sorry for that.
    It is easy to reproduce just start it. If you send a post on /As
    {
    "test": "a"
    }
    you get "message": "The A instance is not valid. Details: test test is too short (value: "a").",
    Which is as expected, because of the validation rule
    But if you Post on /Bs which has base Class A the same Message
    {
    "test": "A"
    }
    It is working without Error ;) I guess the validation should also work for subclasses?
    Thanks

  4. superkhau commented on Aug 13, 2015

    @superkhau
    Contributor

    Thanks for providing the repo. Will look into it.

  5. self-assigned this
    on Aug 13, 2015
  6. Kr0san89 commented on Sep 2, 2015

    @Kr0san89

    Cool, did the repo help to reproduce it for you?

  7. superkhau commented on Sep 2, 2015

    @superkhau
    Contributor

    @Kr0san89 Yes, I managed to reproduce it. Escalating to bug as I've seen a few duplicate issues now.

  8. ghost removed on Sep 3, 2015
  9. ghost added on Sep 3, 2015
  10. 22 remaining items

  11. davidcheung commented on May 10, 2016

    @davidcheung
    Contributor

    Had some discussion and test implementation and came to the conclusion that we probably should not be inheriting the validations by model creation, but to encourage users to use the Model.setup() function as a constructor for the model (this is not really well documented, or maybe not documented at all) and i think is what led to users having subModel inherit issues. Basically model on submodels are already being rebuilt, but if we also inherit we will run into issues where validations gets inherited then rebuilt and end up with duplication and there aren't easy ways to remove validation after the fact.

    The creation of sub-models actually calls the parents setup function, which gives us the ability to add logic to the child-model's constructor, which also means the ability to build those validations on the childModel.

    /cc @bajtos @raymondfeng

  12. richardpringle commented on May 11, 2016

    @richardpringle
    Contributor

    @davidcheung, did you create a doc issue for @crandmck?

  13. davidcheung commented on May 11, 2016

    @davidcheung
    Contributor

    @richardpringle good idea
    #2332

    Closing issue now

  14. acabreragnz commented on Aug 3, 2016

    @acabreragnz

    @bajtos your workaround is not working for me

    My model herarchy is the next one:

    PersistedModel -> BaseModel -> BusinessOwnerActionModel (workaround here) -> BusinessModel (when saving it doesn't throw an error)

  15. richardpringle commented on Aug 3, 2016

    @richardpringle
    Contributor

    @acabrera91 can you fork the loopback-sandbox and reproduce the issue in your forked repo, then provide a link?

    It might be a good idea to open a new issue too since this one is closed.

  16. AlexYegupov commented on Aug 8, 2016

    @AlexYegupov

    Additionally: issue #1588 is closed as duplicate of this issue. But the problem described there is still seems unsolved: validation rules are still applied in not required properties.

  17. thegovind commented on Aug 15, 2016

    @thegovind

    Somewhat related to this - loopback-component-passport/issues/173 I basically need an option to override or suppress email validation from the base model. Is there a way to do this?

  18. flam312 commented on Aug 15, 2016

    @flam312

    @git2g I have the same problem. I added the following line in my app.start function:

    delete app.models.User.validations.email;

    It not the best solution but it does suppress the email validation.

    Source:
    #879

  19. thegovind commented on Aug 15, 2016

    @thegovind

    Thanks @flam312 - wish they resolved the issue by providing a custom validatorFn for the base Email model. But I guess this will have to do for now

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