Skip to content

Instantly share code, notes, and snippets.

@einarwh
Last active December 12, 2015 12:18
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 einarwh/4770759 to your computer and use it in GitHub Desktop.
Save einarwh/4770759 to your computer and use it in GitHub Desktop.
Server-side validation code for Mkay.
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var subject = validationContext.ObjectInstance;
var memName = validationContext.MemberName;
if (memName == null)
{
throw new Exception(
"Property name is not set for property with display name " + validationContext.DisplayName
+ ", you should register the MkayValidator with the MkayAttribute in global.asax.");
}
var validator = CreateValidator(subject, memName, Tree);
return validator()
? ValidationResult.Success
: new ValidationResult(ErrorMessage ?? _defaultErrorMessage);
}
private static Func<bool> CreateValidator(object subject, string property, ConsCell ast)
{
var builder = new ExpressionTreeBuilder(subject, property);
var viz = new ExpVisitor<Expression>(builder);
ast.Accept(viz);
var exp = viz.GetResult();
var validator = builder.DeriveFunc(exp).Compile();
return validator;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment