Is it possible to validate if only one of a group of properties are defined?
See original GitHub issueSuppose I have something like this.
class UpdateUserDTO {
@IsString()
readonly status: string;
@IsBoolean()
readonly deleted: boolean;
@IsString()
readonly name: string;
}
I want to validate this so that the class can only have either status or deleted defined. It doesn’t matter if name is defined or not, but if status is defined, then deleted cannot be defined and vice versa.
Any way to make that work?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:10
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Validate Property Values - MATLAB & Simulink - MathWorks
When inheriting validation for a property from multiple classes, only a single Abstract property in one superclass can define the validation.
Read more >How do I require one field or another or (one of two others) but ...
Remember that it means that "just one of these schemas can validate". The following schema achieves the property switching you are attempting to...
Read more >Is there a way to validate that one of two properties (both ...
The object is valid if it matches one of two objects;. 1) The object requires an "id" and a "bar" array with at...
Read more >Property form - General tab - Completing the Table fields
If an input value for a property doesn't match the table edits, the property name and the invalid value remain on the clipboard,...
Read more >Schema validation reference for object types
data.schema object instance has properties which are not allowed by the schema: ["maximum"]. The schema defines an order_count property that expects ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Extending @halcarleton 's work above as suggested to combine the two decorators, the following works for me:
It’s been a while since this issue was created, but here’s the solution I came up with for this use case. I had the same requirement, and it seems like a pretty common requirement.
What you’re looking to do would currently require the combination of a custom validator and
ValidateIf. You end up with two validations, one validates if there a property present that cannot exist on the same instance as the validated property, and the other determines if a property should be validated.Your class
Note: there are definitely some improvements that can be made to this, but as a quick example it should get the job done.
If you wanted to you could wrap these two decorators in a decorator to make it a one line validation definition.