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

FV When causes swagger to not be updated. #134

Open
niemyjski opened this issue Aug 28, 2023 · 3 comments
Open

FV When causes swagger to not be updated. #134

niemyjski opened this issue Aug 28, 2023 · 3 comments

Comments

@niemyjski
Copy link

I'm using the latest beta version on .NET 7 latest with the latest stable FV. I have the following record and validation rule and I noticed that my swagger doesn't include any of the not empty or length data (I was expecting these to be populated along side the nullable flag to control if they are optional):

public record MyModel(string? InviteToken = null);

public class MyModelValidator : AbstractValidator<MyModel>
{
    public MyModelValidator ()
    {
        RuleFor(u => u.InviteToken).NotEmpty().Length(40).When(m => m.InviteToken is not null);
    }
}

Looking at the generated swagger.json

            "MyModel": {
                "required": [
                ],
                "type": "object",
                "properties": {
                    "invite_token": {
                        "type": "string",
                        "nullable": true
                    }
                },
                "additionalProperties": false
            },
@petriashev
Copy link
Member

Hi!
Conditional rules skips by default.

What schema do you expect?

@niemyjski
Copy link
Author

niemyjski commented Aug 31, 2023

It's tricky because we have a condition in the When. But I'd expect this case to behave like Optional when not empty, otherwise the length has to be 40. Seems like FV needs a WhenNotEmpty

In data Annotations, I'd do

[StringLength(40, MinimumLength = 40)]
public string? InviteToken { get; init; }

And it would not mark it as required but would add length rules. Which would mean optional when null, otherwise must be 40 characters long.

@jgarciadelanoceda
Copy link

jgarciadelanoceda commented Sep 7, 2023

@niemyjski you could avoid the when because FluentValidation does not enter into a rule if the property is null. So the best thing you can do is to change the removing the NotEmpty rule, only there is a weird condition that is if the string has 40 whiteSpaces the Rule will not apply the same result, for this you could apply another rule making a workAround.I have kind of the same problem but when IsNullOrEmpty or IsNullOrWhiteSpace for strings, but I do not find a workAround to this matter

But if I remove the when operator the behavior of the validator changes it would be nice if there is a When with isNullOrEmpty/IsNullOrWhiteSpace the package continues reading the Rule and apply the documentation.

I have seen through the solution if there was a way to get the condition applied on the Where clause and there is no way right now because the library gets the info of the rule from IRuleComponent that is the element of FluentValidation that exposes the info and I see that there is only a exposure of the properties HasCondition or HasAsyncCondition

There is an issue on FluentValidation where they request Jeremy to add the identification of the when conditions, FluentValidation/FluentValidation#2114

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants