Skip to content

multiple when/unless calls in the same chain not consistent with FluentValidation behaviour #45

@maxwell-01

Description

@maxwell-01

In FluentValidation it is possible to string together multiple when statements for a single property. In this library the behaviour does not work as expected. For example:

this.ruleFor("days")
      .notEmpty()
      .must([beNumeric, beAnInteger, beGreaterThanOrEqualTo(0)])
      .withMessage("days must be greater than or equal to 0")
      .must([beNumeric, beAnInteger, beLessThan(2000000000)])
      .withMessage("The maximum allowed number size is 1,999,999,999")
      .must((value, model) => asNumber(value) > 0 || asNumber(model.otherDays) > 0)
      .withMessage("days and otherDays cannot both be 0")
      .when((model) => model.dayCode== "weekday")
      .null()
      .withMessage("days must not be entered for weekendDays")
      .when((model) => model.dayCode!= "weekendDay");

Is not the same as:

this.ruleFor("days")
      .notEmpty()
      .must([beNumeric, beAnInteger, beGreaterThanOrEqualTo(0)])
      .withMessage("days must be greater than or equal to 0")
      .must([beNumeric, beAnInteger, beLessThan(2000000000)])
      .withMessage("The maximum allowed number size is 1,999,999,999")
      .must((value, model) => asNumber(value) > 0 || asNumber(model.otherDays) > 0)
      .withMessage("days and otherDays cannot both be 0")
      .when((model) => model.dayCode== "weekday");

this.ruleFor("days")
      .null()
      .withMessage("days must not be entered for weekendDays")
      .when((model) => model.dayCode!= "weekendDay");

The second set of code provides the expected behaviour.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions