/// <summary> /// Checks value is greater /// </summary> public static IValidationCondition <TValue> IsGreater <TValue>( this IValidationCondition <TValue> condition, TValue comparable) where TValue : IComparable <TValue> { return(condition.Is(value => value.CompareTo(comparable) > 0)); }
/// <summary> /// Checks if value is not match regex /// </summary> public static IValidationCondition <string> IsNotMatch( this IValidationCondition <string> condition, string regex, RegexOptions options = RegexOptions.None) { return(condition.IsNot(value => Regex.IsMatch(value, regex, options))); }
/// <summary> /// Adds <see cref="ValidationDetail" /> to <see cref="IValidationContext" /> with specified /// <see cref="ValidationSeverity" /> /// </summary> /// <exception cref="ValidationConditionException"> /// Throws when ValidationDetail.ValidationSeverity greater ValidationContext.ValidationSeverity. /// Example: Add fatal detail, when validation context severity is error /// </exception> public static ValidationDetail?AddValidationDetail( this IValidationCondition condition, string validationMessage, ValidationSeverity validationSeverity = ValidationSeverity.Error) { var validationContext = condition.ValidationContext; var validationDetail = new ValidationDetail( validationContext, condition.ValidationKey, validationMessage, validationSeverity, condition.IsValid ?? false); // Not null or false // Null by default, so validationContext.When(...).AddValidationDetail(...) works without additional conditions if (validationDetail.IsValid) { return(validationDetail); } validationContext.ValidationDetails.Add(validationDetail); if (validationDetail.ValidationSeverity > validationContext.ValidationSeverity) { throw new ValidationConditionException(validationDetail); } return(validationDetail); }
/// <summary> /// Checks value is less or equal /// </summary> public static IValidationCondition <TValue> IsLessOrEqual <TValue>( this IValidationCondition <TValue> condition, TValue comparable) where TValue : IComparable <TValue> { return(condition.Is(value => value.CompareTo(comparable) <= 0)); }
public ValidationConditionEntity MapFromInterface(IValidationCondition validationCondition) { this.ApplicationID = validationCondition.ApplicationID; this.Expression = validationCondition.Expression; return(this); }
public override IValidationCondition CreateValidationCondition(IValidationCondition validationCondition) { ValidationConditionEntity validationConditionEntity = _ctx.ValidationConditions.Add((ValidationConditionEntity)validationCondition); _ctx.SaveChanges(); return(validationConditionEntity); }
/// <summary> /// Checks value is not in range (min: inclusive, max: exclusive) /// </summary> public static IValidationCondition <TValue> IsNotInRange <TValue>( this IValidationCondition <TValue> condition, TValue min, TValue max) where TValue : IComparable <TValue> { return(condition.IsNot(value => value.CompareTo(min) >= 0 && value.CompareTo(max) < 0)); }
public IResponse UpdateValidationCondition(ValidationCondition validationCondition) { GuardianOptions options = GuardianOptionsFactory.GetOptions(); GuardianDataProvider dataProvider = options.GuardianDataProviderFactory(); IValidationCondition createdValidation = dataProvider.UpdateValidationCondition(validationCondition); return(new JsonResponse(dataProvider.GetValidationCondition(createdValidation.ValidationConditionID))); }
public override IValidationCondition CreateValidationCondition(IValidationCondition validationCondition) { ValidationConditionEntity newValidationConditionEntity = new ValidationConditionEntity().MapFromInterface(validationCondition); newValidationConditionEntity = _ctx.ValidationConditions.Add(newValidationConditionEntity); _ctx.SaveChanges(); return(newValidationConditionEntity); }
/// <summary> /// Adds a Property based Condition to the rule. The Property is added to the Condition if not provided by the condition. /// </summary> /// <param name="condition">The condition that will be validated</param> /// <returns>A Property bsed ValidationRule</returns> public virtual IValidationRule <T, TPrp> AddCondition(IValidationCondition <T, TPrp> condition) { if (condition.Property == null) { condition.Property = _property; } ValidationConditions.Add(condition); return(this); }
public override IValidationCondition UpdateValidationCondition(IValidationCondition validationCondition) { ValidationConditionEntity validationConditionEntity = getValidationCondition(validationCondition.ValidationConditionID); validationConditionEntity.ApplicationID = validationCondition.ApplicationID; validationConditionEntity.DateModifiedOffset = DateTimeOffset.UtcNow;; validationConditionEntity.Expression = validationCondition.Expression; _ctx.SaveChanges(); return(validationConditionEntity); }
public static ValidationConditionEntity FromInterface(IValidationCondition validationCondition) { return(new ValidationConditionEntity { ActiveFlag = true, ApplicationID = validationCondition.ApplicationID, Expression = validationCondition.Expression, DateCreatedOffset = DateTimeOffset.UtcNow, DateModifiedOffset = DateTimeOffset.UtcNow }); }
private bool EvaluateValidationCondition <T>(T target, IValidationCondition validationCondition) { if (!_validationConditionResultDictionary.ContainsKey(validationCondition.ValidationConditionID)) { ParameterExpression parameterExpression = Expression.Parameter(typeof(T), typeof(T).Name); LambdaExpression expression = DynamicExpression.ParseLambda(new[] { parameterExpression }, typeof(bool), validationCondition.Expression); bool outcome = (bool)expression.Compile().DynamicInvoke(target); _validationConditionResultDictionary.Add(validationCondition.ValidationConditionID, outcome); } return(_validationConditionResultDictionary[validationCondition.ValidationConditionID]); }
private bool EvaluateNode <T>(ExpressionTreeNode node, T target, IEnumerable <IValidationCondition> validationConditions) { if (!node.Token.IsOperatorToken()) { IIdentifier identifier = (IIdentifier)node.Token; IValidationCondition validationCondition = validationConditions.FirstOrDefault(r => r.ValidationConditionID == identifier.ID); return(EvaluateValidationCondition(target, validationCondition)); } else { IOperator op = (IOperator)node.Token; return(op.Evaluate(() => EvaluateNode(node.Left, target, validationConditions), () => EvaluateNode(node.Right, target, validationConditions))); } }
/// <summary> /// Checks value is type of <see cref="TType"/> /// </summary> public static IValidationCondition <object> IsType <TType>( this IValidationCondition <object> condition, Action <IValidationCondition <TType> >?typed = null) { if (condition.Value is TType) { var typedCondition = new ValidationCondition <TType>( condition.ValidationContext, condition.ValidationKey, new Lazy <TType>(() => (TType)condition.Value)); typed?.Invoke(typedCondition); condition.IsValid = typedCondition.IsValid ?? false; } else { condition.IsValid = true; } return(condition); }
/// <summary> /// Adds <see cref="ValidationDetail" /> to <see cref="IValidationContext" /> with fatal severity /// </summary> /// <exception cref="ValidationConditionException"> /// Throws when ValidationDetail.ValidationSeverity greater ValidationContext.ValidationSeverity. /// Example: Add fatal detail, when validation context severity is error /// </exception> public static ValidationDetail?AddValidationFatal( this IValidationCondition condition, string validationMessage) { return(condition.AddValidationDetail(validationMessage, ValidationSeverity.Fatal)); }
/// <summary> /// Checks value is false /// </summary> public static IValidationCondition <bool> IsFalse( this IValidationCondition <bool> condition) { return(condition.IsNot(value => value)); }
/// <summary> /// Adds a Condition to the rule. If the condition needs a Property to be validated, the property has to be added to the condition. This will not be done automaticaly. /// </summary> /// <param name="condition">The condition that will be validated</param> /// <returns>A ValidationRule</returns> public virtual IValidationRule <T> AddCondition(IValidationCondition <T> condition) { ValidationConditions.Add(condition); return(this); }
/// <summary> /// Checks value is type of /// </summary> public static IValidationCondition <TValue> IsType <TValue>( this IValidationCondition <TValue> condition, Type type) { return(condition.Is(value => value?.GetType() == type)); }
/// <summary> /// Checks value is not equal using Equals method /// </summary> public static IValidationCondition <TValue> IsNotEqual <TValue>( this IValidationCondition <TValue> condition, TValue expect) { return(condition.IsNot(value => value?.Equals(expect) ?? expect is null)); }
/// <summary> /// Checks value is not null /// </summary> public static IValidationCondition <TValue> IsNotNull <TValue>(this IValidationCondition <TValue> condition) { return(condition.IsNot(value => value is null)); }
/// <summary> /// Checks collection has count not /// </summary> public static IValidationCondition <ICollection <TElement> > HasCountNot <TElement>( this IValidationCondition <ICollection <TElement> > condition, int count) { return(condition.IsNot(value => value?.Count == count)); }
/// <summary> /// Checks values has a value /// </summary> public static IValidationCondition <TValue> IsNotIn <TValue>( this IValidationCondition <TValue> condition, IEnumerable <TValue> values) { return(condition.IsNot(value => values.Contains(condition.Value))); }
/// <summary> /// Checks collection is not contains /// </summary> public static IValidationCondition <ICollection <TElement> > IsNotContains <TElement>( this IValidationCondition <ICollection <TElement> > condition, TElement element) { return(condition.IsNot(value => value?.Contains(element) ?? false)); }
/// <summary> /// Checks collection is empty /// </summary> public static IValidationCondition <ICollection <TElement> > IsEmpty <TElement>( this IValidationCondition <ICollection <TElement> > condition) { return(condition.Is(value => value is null || value.Count == 0)); }
/// <summary> /// Checks if value is not valid /// </summary> public static IValidationCondition <TValue> IsNot <TValue>( this IValidationCondition <TValue> condition, Func <TValue, bool> predicate) { return(condition.IsNot(() => predicate(condition.Value))); }
/// <summary> /// Checks if value is not default /// </summary> public static IValidationCondition <TValue> IsNotDefault <TValue>( this IValidationCondition <TValue> condition) { return(condition.IsNot(value => EqualityComparer <TValue> .Default.Equals(value, default))); }
public abstract IValidationCondition UpdateValidationCondition(IValidationCondition validationCondition);
/// <summary> /// Checks values has a value /// </summary> public static IValidationCondition <TValue> IsNotIn <TValue>( this IValidationCondition <TValue> condition, params TValue[] values) { return(condition.IsNot(value => values.Contains(condition.Value))); }
/// <summary> /// Checks value is true /// </summary> public static IValidationCondition <bool> IsTrue( this IValidationCondition <bool> condition) { return(condition.Is(value => value)); }