You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Validators defined in models .js file does not work on submodels #1485
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.
import{models}from'loopback-client'// ----- Remote model ------letuser=models.User({name: 'Bob'// 3 chars, 5 is minimum})user.isValid()// false// ----- Local model ------letlocalUser=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?)
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
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.
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.
Activity
[-]Validators does not works on client[/-][+]Validators defined in models .js file does not work on submodels[/+]superkhau commentedon Jul 6, 2015
Can you provide a link to a test project on Github? See https://github.com/strongloop/loopback/wiki/Issues#bug-report
Kr0san89 commentedon Aug 13, 2015
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
Ainstance is not valid. Details:testtest 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
superkhau commentedon Aug 13, 2015
Thanks for providing the repo. Will look into it.
Kr0san89 commentedon Sep 2, 2015
Cool, did the repo help to reproduce it for you?
superkhau commentedon Sep 2, 2015
@Kr0san89 Yes, I managed to reproduce it. Escalating to bug as I've seen a few duplicate issues now.
22 remaining items
davidcheung commentedon May 10, 2016
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
richardpringle commentedon May 11, 2016
@davidcheung, did you create a doc issue for @crandmck?
davidcheung commentedon May 11, 2016
@richardpringle good idea
#2332
Closing issue now
acabreragnz commentedon Aug 3, 2016
@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)
richardpringle commentedon Aug 3, 2016
@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.
AlexYegupov commentedon Aug 8, 2016
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.
thegovind commentedon Aug 15, 2016
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?
flam312 commentedon Aug 15, 2016
@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
thegovind commentedon Aug 15, 2016
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