Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
Created June 14, 2018 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielplawgo/b32d50d074f3713cb2689cb6c0a44136 to your computer and use it in GitHub Desktop.
Save danielplawgo/b32d50d074f3713cb2689cb6c0a44136 to your computer and use it in GitHub Desktop.
Fluent Validation własny walidator
public static class IRuleBuilderExtensions
{
public static IRuleBuilderOptions<T, TProperty> Nip<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder)
{
return ruleBuilder.SetValidator(new NipValidator());
}
}
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;
}
}
RuleFor(u => u.Nip)
.Nip();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment