-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to use @Or decorator #35
Comments
Thanks for raising this issue. There's quite a few "object" constraint decorators that should be applicable at the class level. Unfortunately, tsdv-joi doesn't currently support this. As a workaround, you could try wrapping your object to validate in another object, and decorate that property. Something like below might work, but I haven't tested it, so I don't know if you can use export class Address {
@Optional()
public currentAddress?: string;
@Optional()
public permanentAddress?: string;
}
class AddressWrapper {
@ObjectConstraints.Or('currentAddress', 'permanentAddress')
@Nested(Address)
public address: Address;
} For anyone who wants to tackle this, here's some of the constraints that should be class decorators, but really all "object" constraints should be applicable to classes:
|
Thanks for the response but this doesn't work either. It fails while verifying peers. I checked the code and verifyPeers tries to find properties in the target object. And based on your code above, target is |
I can confirm your findings as a bug. Any object decorators that verify peers are incorrectly looking for those peers in the parent class, not the decorated property, and the decorators can only be placed on "object" properties. I had a quick go at relaxing the decorators so they can be placed on properties of any type, but ran into further issues:
This will require some more work to solve, sorry. 🤕 |
I have a model similar to below:
The requirement here is that at-least
currentAddress
orpermanentAddress
should be provided. And @or decorator fits the bill perfectly here, however i'm unable to get it working.Looking at some of the joi examples, it seems
.or
is used on the schema of the object (i.e.Address
class in my example above) rather than on the keys (i.e.currentAddress
,permanentAddress
etc).Can someone provide an example of how @or decorator be used? I could not find anything in the tests too.
Thanks in advance.
The text was updated successfully, but these errors were encountered: