Search code examples
typescriptclass-validator

How can I check if two properties are not true or false at the same time


I am using class-validator for validation. I have a class like this:

class MyClass {
x: boolean;
y: boolean;
}

I need to check that if x is true then y is always false, or if x is false then y is always true. Both of them can't be true or false at the same time. How can i do this?


Solution

  • You could try validation in this kind of logic by making a strict equality test of one Boolean with the logical not of the other, this way:

    (MyClass.x === !MyClass.y)
    //this would mean boolean === !boolean
    // in example cases if both x and y are true then the result would be false since true === false (!true) = false.
    

    You can then add a condition for validation based on this.