Last active
December 12, 2015 12:18
-
-
Save einarwh/4770759 to your computer and use it in GitHub Desktop.
Server-side validation code for Mkay.
This file contains 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
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