public static IRuleBuilderOptions <TRequest, Guid> EntityExists <TRequest, TEntity>( this IRuleBuilderInitialCollection <TRequest, Guid> ruleBuilder, IArpaContext arpaContext, string propertyName) where TEntity : BaseEntity { return(ruleBuilder .MustAsync(async(id, cancellation) => (await arpaContext.EntityExistsAsync <TEntity>(id, cancellation))) .OnFailure((_) => throw new NotFoundException(typeof(TEntity).Name, propertyName))); }
public static IRuleBuilderOptions <TRequest, byte> InstrumentPart <TRequest>( this IRuleBuilderInitialCollection <TRequest, byte> ruleBuilderInitial, IArpaContext arpaContext) where TRequest : IHasInstrumentRequest { return(ruleBuilderInitial .MustAsync(async(request, preferredPart, cancellation) => (await arpaContext .FindAsync <Section>(new object[] { request.InstrumentId }, cancellation)) .InstrumentPartCount >= preferredPart) .WithMessage("The selected part is not valid for this instrument")); }
public static IRuleBuilderOptions <TRequest, Guid> MusicianProfilePosition <TRequest>( this IRuleBuilderInitialCollection <TRequest, Guid> ruleBuilderInitial, IArpaContext arpaContext, string propertyName) where TRequest : IHasInstrumentRequest { return(ruleBuilderInitial .Cascade(CascadeMode.Stop) .EntityExists <TRequest, SelectValueSection>(arpaContext, propertyName) .MustAsync(async(request, selectValueSectionId, cancellation) => (await arpaContext .FindAsync <Section>(new object[] { request.InstrumentId }, cancellation)) .SelectValueSections.Any(item => item.Id.Equals(selectValueSectionId))) .WithMessage("The selected position is not valid for this instrument")); }
public CollectionValidatorRuleBuilder(IRuleBuilder <T, IEnumerable <TCollectionElement> > ruleBuilder, IRuleBuilderInitialCollection <IEnumerable <TCollectionElement>, TCollectionElement> innerRuleBuilder, IValidator <T> parent) { _ruleBuilder = ruleBuilder; _innerRuleBuilder = innerRuleBuilder; ParentValidator = parent; }
/// <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; })); }
/// <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; })); }
/// <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); })); }
public static IRuleBuilderOptions <T, KeyValuePair <string, string> > MustBeValidTag <T>(this IRuleBuilderInitialCollection <T, KeyValuePair <string, string> > ruleBuilder) => ruleBuilder .Must(BeValidTag) .WithMessage("'{PropertyName}' must contain valid tag name/values");
/// <summary> /// Gets the configurable rule instance from a rule builder. /// </summary> /// <param name="ruleBuilder">The rule builder.</param> /// <returns>A configurable IValidationRule instance.</returns> public static ICollectionRule <T, TCollectionElement> Configurable <T, TCollectionElement>(IRuleBuilderInitialCollection <T, TCollectionElement> ruleBuilder) { return((ICollectionRule <T, TCollectionElement>)((RuleBuilder <T, TCollectionElement>)ruleBuilder).Rule); }
/// <summary> /// Configures the rule object. /// </summary> /// <param name="ruleBuilder"></param> /// <param name="configurator">Action to configure the object.</param> /// <returns></returns> public static IRuleBuilderInitialCollection <T, TElement> Configure <T, TElement>(this IRuleBuilderInitialCollection <T, TElement> ruleBuilder, Action <ICollectionRule <T, TElement> > configurator) { configurator(Configurable(ruleBuilder)); return(ruleBuilder); }
/// <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)); Configurable(rule).IndexBuilder = callback; return(rule); }
/// <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) { Configurable(ruleBuilder).CascadeMode = cascadeMode; return(ruleBuilder); }
/// <summary> /// Gets the configurable rule instance from a rule builder. /// </summary> /// <param name="ruleBuilder">The rule builder.</param> /// <returns>A configurable IValidationRule instance.</returns> public static ICollectionRule <T, TCollectionElement> Configurable <T, TCollectionElement>(IRuleBuilderInitialCollection <T, TCollectionElement> ruleBuilder) { return((ICollectionRule <T, TCollectionElement>)((IRuleBuilderInternal <T, TCollectionElement>)ruleBuilder).GetConfigurableRule()); }
public static IRuleBuilderOptions <T, TProperty> SetValidator <T, TProperty>(this IRuleBuilderInitialCollection <T, TProperty> ruleBuilder, IValidatorProvider validatorProvider) => ruleBuilder.SetValidator(validatorProvider.ToValidator <TProperty>());
OverrideIndexerWithDictionaryKey <TKey, TElement>( this IRuleBuilderInitialCollection <IDictionary <TKey, TElement>, KeyValuePair <TKey, TElement> > @this) { return(@this.OverrideIndexer((x, collection, element, index) => $"[{element.Key}]")); }