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

Specification validation message customization with Func<> #28

Closed
michalkowal opened this issue Aug 14, 2022 · 0 comments · Fixed by #29
Closed

Specification validation message customization with Func<> #28

michalkowal opened this issue Aug 14, 2022 · 0 comments · Fixed by #29
Milestone

Comments

@michalkowal
Copy link
Owner

Right now there is possibility to override validation message by use WithMessage function.

var spec = Specification.Length<string>(5).WithMessage("Incorrect value length");
spec.IsSatisfiedBy("lorem ipsum", out var result);    // "Incorrect value length"

There should be possibility to create validation message based on candidate value and specification parameters:

var spec = Specification.Length<string>(5)
    .WithMessage((c, p) => $"Incorrect value length - expected [{p["Length"]}] but passed [{c.Length}]");
spec.IsSatisfiedBy("lorem ipsum", out var result);    // "Incorrect value length - expected [5] but passed [11]"

Localization example:

public IValidationSpecification<string> CreateSpecification(IStringLocalizer localizer)
{
    return Specification.MaxLength<string>(100)
        .WithMessage((c, p) => localizer.GetString("Max length of value should be {1} but passed value has {0}",
            c.Length, p["MaxLength"]));
}
@michalkowal michalkowal added this to the 1.3.0 milestone Aug 14, 2022
michalkowal added a commit that referenced this issue Aug 15, 2022
michalkowal added a commit that referenced this issue Aug 15, 2022
michalkowal added a commit that referenced this issue Aug 15, 2022
…ion-message-customization-with-func

specification validation message customization with func (#28)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant