Skip to content
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

Open
yagyesha opened this issue May 28, 2019 · 3 comments
Open

Unable to use @Or decorator #35

yagyesha opened this issue May 28, 2019 · 3 comments

Comments

@yagyesha
Copy link

I have a model similar to below:

export class Address {
    public currentAddress: string;
    public permanentAddress: string;
}

The requirement here is that at-least currentAddress or permanentAddress 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.

@laurence-myers
Copy link
Member

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 @Nested() with Or().

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:

  • And
  • Nand
  • Or
  • Pattern
  • Rename
  • Type
  • Unknown
  • With
  • Without
  • Xor

@yagyesha
Copy link
Author

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 AddressWrapper and obviously it doesnt have currentAddress and permanentAddress properties.

@laurence-myers
Copy link
Member

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:

  • The decorator must be placed on the last property referenced in the @Or peers, because the type metadata is missing for any following properties.
  • The decorator tries to update the property schema, but it needs to update the class's schema. Unfortunately, the way tsdv-joi is storing metadata is just as an object mapping property names to Joi schemas, with no way to apply any other object validations to the class.

This will require some more work to solve, sorry. 🤕

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

No branches or pull requests

2 participants