Created
June 14, 2018 12:35
-
-
Save danielplawgo/b32d50d074f3713cb2689cb6c0a44136 to your computer and use it in GitHub Desktop.
Fluent Validation własny walidator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class IRuleBuilderExtensions | |
{ | |
public static IRuleBuilderOptions<T, TProperty> Nip<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder) | |
{ | |
return ruleBuilder.SetValidator(new NipValidator()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NipValidator : PropertyValidator | |
{ | |
public NipValidator() | |
: base("{PropertyName} ma niepoprawny format NIPu.") | |
{ | |
} | |
protected override bool IsValid(PropertyValidatorContext context) | |
{ | |
var nip = context.PropertyValue as string; | |
if (nip == null) | |
{ | |
return false; | |
} | |
return NipValidate(nip); | |
} | |
private bool NipValidate(string nip) | |
{ | |
if (nip.Length != 10) | |
{ | |
return false; | |
} | |
return true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RuleFor(u => u.Nip) | |
.Nip(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment