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

How to validate a property is optional only if another property is defined? #395

Open
1 task done
shafi- opened this issue Oct 10, 2024 · 0 comments
Open
1 task done

Comments

@shafi-
Copy link

shafi- commented Oct 10, 2024

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

Disclaimer: This is probably not an existing problem.

Recently there was a requirement to make a property optional when another property is defined.

Is there any specific approach or combination of existing validators to achieve that?

Describe the solution you'd like

There could be a validator IsOptionalWhenDefined('depenedencyProp').

So if dependencyProp is defined, the validations should consider the prop as optional similar to IsOptional. Otherwise, it should be required and other validations should not be ignored.

Teachability, documentation, adoption, migration strategy

So the usage can be like:

class Test {
     propA: string;

     @IsOptionalWhenDefined('propA')
     propB?: string;
}

What is the motivation / use case for changing the behavior?

Recently there was a requirement to make a property optional when another property is defined.

We had to validate an api request body where the requirement is a property is optional if another property exists.

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

No branches or pull requests

1 participant