/// <summary>
 /// Applies a filter to a collection property.
 /// </summary>
 /// <param name="rule">The current rule</param>
 /// <param name="predicate">The condition</param>
 /// <returns></returns>
 public static IRuleBuilderInitialCollection <T, TCollectionElement> Where <T, TCollectionElement>(this IRuleBuilderInitialCollection <T, TCollectionElement> rule, Func <TCollectionElement, bool> predicate)
 {
     // This overload supports RuleFor().SetCollectionValidator() (which returns IRuleBuilderOptions<T, IEnumerable<TElement>>)
     predicate.Guard("Cannot pass null to Where.", nameof(predicate));
     return(rule.Configure(cfg => {
         cfg.Filter = predicate;
     }));
 }
Esempio n. 2
0
 /// <summary>
 /// Allows the generated indexer to be overridden for collection rules.
 /// </summary>
 /// <param name="rule">The current rule</param>
 /// <param name="callback">The callback. Receives the model, the collection, the current element and the current index as parameters. Should return a string representation of the indexer. The default is "[" + index + "]"</param>
 /// <returns></returns>
 public static IRuleBuilderInitialCollection <T, TCollectionElement> OverrideIndexer <T, TCollectionElement>(this IRuleBuilderInitialCollection <T, TCollectionElement> rule, Func <T, IEnumerable <TCollectionElement>, TCollectionElement, int, string> callback)
 {
     // This overload supports RuleFor().SetCollectionValidator() (which returns IRuleBuilderOptions<T, IEnumerable<TElement>>)
     callback.Guard("Cannot pass null to OverrideIndexer.", nameof(callback));
     return(rule.Configure(cfg => {
         cfg.IndexBuilder = (x, collection, element, index) => callback((T)x, (IEnumerable <TCollectionElement>)collection, (TCollectionElement)element, index);
     }));
 }
 /// <summary>
 /// Transforms the property value before validation occurs. The transformed value must be of the same type as the input value.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <typeparam name="TProperty"></typeparam>
 /// <param name="ruleBuilder"></param>
 /// <param name="transformationFunc"></param>
 /// <returns></returns>
 public static IRuleBuilderInitialCollection <T, TProperty> Transform <T, TProperty>(this IRuleBuilderInitialCollection <T, TProperty> ruleBuilder, Func <TProperty, TProperty> transformationFunc)
 {
     return(ruleBuilder.Configure(cfg => {
         cfg.Transformer = transformationFunc.CoerceToNonGeneric();
     }));
 }
 /// <summary>
 /// Specifies the cascade mode for failures.
 /// If set to 'Stop' then execution of the rule will stop once the first validator in the chain fails.
 /// If set to 'Continue' then all validators in the chain will execute regardless of failures.
 /// </summary>
 public static IRuleBuilderInitialCollection <T, TProperty> Cascade <T, TProperty>(this IRuleBuilderInitialCollection <T, TProperty> ruleBuilder, CascadeMode cascadeMode)
 {
     return(ruleBuilder.Configure(cfg => {
         cfg.CascadeMode = cascadeMode;
     }));
 }