AlexJPotter / fluentvalidation-ts

A TypeScript-first library for building strongly-typed validation rules
Apache License 2.0
87 stars 6 forks source link

Validation rule for two properties #42

Closed rwb196884 closed 8 months ago

rwb196884 commented 1 year ago

I have an object

{
  a: number, 
  b: number
}

How do I write a validaton rule to require a + b <= 100?

bohoffi commented 1 year ago

@rwb196884 you could use a custom rule to achieve this.

this.ruleFor('a').must(
  (a, model) => a + model.b <= 100
);
AlexJPotter commented 8 months ago

Hi @rwb196884, the answer provided by @bohoffi is correct (thank you!)