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

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

Open
maxwell-01 opened this issue Oct 23, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@maxwell-01
Copy link

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.

@AlexJPotter AlexJPotter added the bug Something isn't working label Mar 8, 2024
@danrayson
Copy link

danrayson commented Nov 5, 2024

I have a similar issue, though it doesn't fill up the validation results properly in either case.

import { Validator } from 'fluentvalidation-ts';

export type ChangePasswordRequest = {
    Password: string;
    PasswordCheck: string;
}

export class ChangePasswordRequestValidator extends Validator<ChangePasswordRequest> {

    constructor() {
        super();

        this.ruleFor('Password')
            .notEmpty().withMessage("Password is required")
            .length(8, 100).withMessage("Password must be at least 8 characters long and less than 100 chars long")
            .matches(RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[^a-zA-Z\\d]).+$"))
            .withMessage("Must contain at least one lowercase letter, one uppercase letter, one number, and one special character");

        this.ruleFor('PasswordCheck')
            .notEmpty().withMessage("Confirm password is required")
            .must((x, m) => x == m.Password).withMessage("Passwords do not match");
    }
}

When I call it with 123 for both passwords, I expect that the result would be similar to;

["Password", ["Password must be at least 8 characters long and less than 100 chars long", "Must contain at least one lowercase letter, one uppercase letter, one number, and one special character"]]

But I don't, I only get

["Password", ["Password must be at least 8 characters long and less than 100 chars long"]]

I've tried splitting up the ruleFor's, but this time I see only the last check, so I get only:

["Password", ["Must contain at least one lowercase letter, one uppercase letter, one number, and one special character"]]

To clarify, in DotNet's FluentValidation's implementation you get all messages for all rules that failed in the response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants