public ValidationError(IRule rule, EquatableExpression expression, object[] validationArguments, object value, object originatingValue, EquatableExpression originatingExpression) { if (validationArguments == null) { throw new System.ArgumentNullException("validationArguments"); } if (value == null) { throw new System.ArgumentNullException("value"); } if (expression == null) { throw new System.ArgumentNullException("cachedExpression"); } if (rule == null) { throw new System.ArgumentNullException("rule"); } _rule = rule; _expression = expression; _validationArguments = validationArguments; _value = value; _originatingValue = originatingValue; _originatingExpression = originatingExpression; }
public RuleInvoker(IRule <R> rule, IValueResolver <T, R> valueToValidate, EquatableExpression originatingExpression, ICulpritResolver culpritResolver) { _rule = rule; _originatingExpression = originatingExpression; _valueToValidate = valueToValidate; _culpritResolver = culpritResolver; }
protected virtual MessageEntry GetEntry(Type type, IRule rule, EquatableExpression expression) { var entries = _messageEntries.ToArray(); //entries = entries.Where(e => e.Type.IsAssignableFrom(validationError.Value.GetType())).ToArray(); entries = entries.Where(e => e.Type == type).ToArray(); if (entries.Length == 0) { return(null); } var entriesByExpression = entries.Where(e => expression == null || expression.AppliesTo(e.Expression)).ToArray(); if (entriesByExpression.Length == 0) { entriesByExpression = entries.Where(e => e.Expression == null).ToArray(); if (entriesByExpression.Length == 0) { return(null); } } var entriesByRule = entriesByExpression.Where(e => object.ReferenceEquals(e.Rule, rule)).ToArray(); if (entriesByRule.Length == 0) { entriesByRule = entriesByExpression.Where(e => object.ReferenceEquals(e.Rule, null)).ToArray(); if (entriesByRule.Length == 0) { return(null); } } return(entriesByRule.Last()); }
private bool HasError(object value, EquatableExpression expression, out ValidationError[] validationErrors) { validationErrors = null; if (value == null) { throw new System.ArgumentNullException("value"); } Dictionary <EquatableExpression, List <ValidationError> > errorsOnValue; if (!_errors.TryGetValue(value, out errorsOnValue)) { return(false); } //If we don't care on which expression the error occured, return true. if (expression == null) { //Get all validation errors for that value. validationErrors = errorsOnValue.SelectMany(k => k.Value).ToArray(); return(true); } List <ValidationError> errorsOnExpression; if (!errorsOnValue.TryGetValue(expression, out errorsOnExpression)) { return(false); } validationErrors = errorsOnExpression.ToArray(); return(true); }
public MessageEntry(Type type, EquatableExpression expression, IRule rule, Func <string> message) { //Type cannot be null, others can however. if (type == null) { throw new System.ArgumentNullException("type"); } _type = type; _expression = expression; _rule = rule; _message = message; }
private void Initialize(LambdaExpression expression) { _expression = expression; LambdaExpression culpritValue; LambdaExpression culpritExpression; if (Utilities.TryGetCulprit(expression, out culpritValue, out culpritExpression)) { _culpritValueDelegate = culpritValue.Compile(); _culpritExpression = culpritExpression; } else { _culpritValueDelegate = null; _culpritExpression = expression; } }
public string GetErrorMessage(Type type, EquatableExpression expression, IRule rule, object[] arguments) { var entry = GetEntry(type, rule, expression); if (entry == null) { foreach (Type baseType in GetTypeHierarchy(type)) { entry = GetEntry(baseType, rule, expression); if (entry != null) { break; } } } if (entry == null) { return(null); } return(FormatMessage(entry.Message(), arguments)); }
public EnumerableCompositionInvoker(IEngine rulesEngine, Expression <Func <T, R> > enumerableCompositionExpression) { _rulesEngine = rulesEngine; _compiledExpression = enumerableCompositionExpression.Compile(); _enumerableCompositionExpression = new EquatableExpression(enumerableCompositionExpression); }
public CompositionInvoker(IEngine rulesEngine, Expression <Func <T, R> > compositionExpression) { _rulesEngine = rulesEngine; _compiledExpression = compositionExpression.Compile(); _expression = new EquatableExpression(compositionExpression); }
public string GetErrorMessage(object value, EquatableExpression expression, IErrorResolver resolver) { throw new NotImplementedException(); }
public bool HasError(object value, EquatableExpression expression) { throw new NotImplementedException(); }
public bool HasError(object value, EquatableExpression expression) { ValidationError[] errors; return(HasError(value, expression, out errors)); }
public string GetErrorMessage(object value, EquatableExpression expression, IErrorResolver resolver) { //NOTE: Expression can be null. if (value == null) { throw new System.ArgumentNullException("value"); } if (resolver == null) { throw new System.ArgumentNullException("resolver"); } ValidationError[] errors; ValidationError error; object[] validationArguments = new object[0]; if (this.HasError(value, expression, out errors)) { error = errors[0]; validationArguments = error.ValidationArguments; var message = resolver.GetErrorMessage(error.OriginatingValue.GetType(), error.OriginatingExpression, error.Rule, error.ValidationArguments); return(message); } else if (expression != null && expression.Expression.ReturnType.IsClass && expression.Expression.ReturnType != typeof(string)) { //There may be the case that for compositions, a message is set for CallValidate(). When //this happens, there won't be any error for a => a.C (where 'C' is another object) //still, you would like to have a 'C is not valid' message... //Step 1: find out if there are any Messages defined for that property var message = resolver.GetErrorMessage(value.GetType(), expression, null, new object[0]); //if null fallback on message for the type of that property. if (message == null) { message = resolver.GetErrorMessage(expression.Expression.ReturnType, null, null, new object[0]); } if (message == null) { return(null); } //Step 2: There is a Message defined. Find out if there is an error for the property. object propertyValue = null; try { //TODO: This should be resolved using a Interface maybe. propertyValue = expression.Expression.Compile().DynamicInvoke(value); } catch (NullReferenceException) { //NOTE: Swallow all NullReferenceException. } catch (Exception ex) { //Wrap and re-throw. throw new InvalidOperationException(string.Format("Could not get property value for expression {0}. See InnerException for details.", expression.Expression), ex); } if (propertyValue == null) { return(null); } //Step 3: The property may be an IEnumerable. Need to return the message if any elements has an error... //TODO: This is bit dodgy, you should only enumerate if a CallValidateForEach was used... //Return null if none of the propertyValue(s) have errors. if (ToEnumerable(propertyValue).All(v => !HasError(v, null))) { return(null); } return(message); } return(null); }
public CulpritResult(object value, EquatableExpression expression) { _value = value; _expression = expression; }