andrebaltieri / FluentValidator

Fluent Validator is a fluent way to use Notification Pattern with your entities
122 stars 40 forks source link

Support notification messages with parameters #14

Closed kvnallen closed 7 years ago

kvnallen commented 7 years ago

Added support to use notification parameters in messages e.g:


new ValidationContract()
.Requires()
.Contains("my text", "banana", "message", "{0} does not contains {1}"); 

// my text does not contains banana
fgemig commented 7 years ago

@kvnallen,

I liked your idea, but Is it possible to just inform the index and let the same method "AddNotification" to be in charge of swapping it by the parameter value?

That way we could avoid have to send new parameters. For example:

public ValidationContract HasMinLen(string val, int min, string property, string message)
{
    if (val.Length < min)
        AddNotification(property, string.Format(message, property, min));

    return this;
}

That way, it will be similar to what we usually do in the Data Annotations:

[MinLength(10, ErrorMessage = "{0} field must be at least {1} characters in length")]
var right = new ValidationContract()
    .Requires()
    .HasMinLen(dummy.stringProp, 10, nameof(dummy.stringProp), "{0} field must be at least {1} characters in length");
andrebaltieri commented 7 years ago

I think is better to keep any kind of "string manipulation" out. I agree with @fgemig