private static IValidationContextOptions Unwrap(IValidationContextOptions wrapped, Func <ValidationContextOptions, ValidationContextOptions> processUnwrapped) { var unwrapped = (ValidationContextOptions)wrapped; var processedUnwrapped = processUnwrapped(unwrapped); return(processedUnwrapped); }
public ValidationContext(IValidationContextOptions options = null) { Id = Guid.NewGuid(); Options = OptionsService.GetVerifiedValidationContextOptions(options ?? new ValidationContextOptions()); SpecificationsRepository = new SpecificationsRepository(Options.Specifications); ValidatorsFactory = new ValidatorsFactory(SpecificationsRepository); TranslatorsRepository = new TranslatorsRepository(Options.Translations.ToArray()); }
public static IValidationContextOptions UnwrapSpecifications(IValidationContextOptions wrapped, Action <Dictionary <Type, object> > processUnwrapped) { return(Unwrap(wrapped, options => { processUnwrapped((Dictionary <Type, object>)options.Specifications); return options; })); }
public static IValidationContextOptions UnwrapTranslations(IValidationContextOptions wrapped, Action <List <Translation> > processUnwrapped) { return(Unwrap(wrapped, options => { processUnwrapped((List <Translation>)options.Translations); return options; })); }
private static void AddSpecificationFromHolder <T>(IValidationContextOptions options, ISpecificationHolder <T> specificationHolder) where T : class { if (specificationHolder?.Specification == null) { throw new InvalidSpecificationHolderException("Invalid (null?) specification from holders"); } options.AddSpecification(specificationHolder.Specification); }
public static IValidationContextOptions IncludeInEnglishTranslation(this IValidationContextOptions @this, IDictionary <string, string> include) { if (include == null) { throw new ArgumentNullException(nameof(include)); } @this.AddTranslation(nameof(Phrases.English), include); return(@this); }
public static IValidationContextOptions AddPolishTranslation(this IValidationContextOptions @this, bool asDefault = false, IDictionary <string, string> include = null) { @this.AddTranslation(nameof(Phrases.Polish), Phrases.Polish, asDefault); if (include != null) { @this.AddTranslation(nameof(Phrases.Polish), include); } return(@this); }
public static IValidationContextOptions GetMerged(IValidationContextOptions baseOptions, IValidationContextOptions newOptions) { if (baseOptions == null) { throw new ArgumentNullException(nameof(baseOptions)); } if (newOptions == null) { throw new ArgumentNullException(nameof(newOptions)); } var compiledBase = GetVerifiedValidationContextOptions(baseOptions); var merged = new ValidationContextOptions(); foreach (var translation in compiledBase.Translations) { merged.AddTranslation(translation.Name, translation.Dictionary); } foreach (var translation in newOptions.Translations) { merged.AddTranslation(translation.Name, translation.Dictionary); } OptionsUnwrapper.UnwrapSpecifications(merged, specifications => { foreach (var pair in compiledBase.Specifications) { specifications.Add(pair.Key, pair.Value); } foreach (var pair in newOptions.Specifications) { if (specifications.ContainsKey(pair.Key)) { specifications[pair.Key] = pair.Value; } else { specifications.Add(pair.Key, pair.Value); } } }); merged.Specifications = GetVerifiedSpecificationsDictionary(merged.Specifications); merged.ValidationOptions = GetVerifiedValidationOptions(newOptions.ValidationOptions); return(GetVerifiedValidationContextOptions(merged)); }
/// <summary> /// Adds translation. /// </summary> /// <param name="options"></param> /// <param name="name">Name of the translation. Usually the language name.</param> /// <param name="dictionary"> /// Dictionary with all translation entries. Keys are the original phrases. Values are the /// translations. /// </param> /// <param name="asDefault"> /// If true, sets the translations as the default one. The default is used to create /// <see cref="ITranslationProxy.DefaultTranslator" /> in the <see cref="IValidationResult{T}" />. /// </param> /// <returns></returns> public static IValidationContextOptions AddTranslation(this IValidationContextOptions options, string name, IDictionary <string, string> dictionary, bool asDefault = false) { var translation = new Translation(name, dictionary); var processedOptions = OptionsUnwrapper.UnwrapTranslations(options, translations => { translations.Add(translation); }); if (asDefault) { processedOptions.SetTranslationName(name); } return(processedOptions); }
/// <summary> /// Add translations from a <see cref="TranslationsPackage" />. /// </summary> /// <param name="options"></param> /// <param name="translationsPackage"> /// Translations package. Key is the name of the translation. Value is the dictionary in /// which key is the original phrase and the value is the translated one. /// </param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="translationsPackage" /> is null.</exception> public static IValidationContextOptions AddTranslations(this IValidationContextOptions options, TranslationsPackage translationsPackage) { if (translationsPackage == null) { throw new ArgumentNullException(nameof(translationsPackage)); } return(OptionsUnwrapper.UnwrapTranslations(options, translations => { var translationsFromPackage = translationsPackage.Select(pair => new Translation(pair.Key, pair.Value)).ToArray(); foreach (var translation in translationsFromPackage) { translations.Add(translation); } })); }
public static void InvokeAddSpecificationFromHolder(IValidationContextOptions options, object holderInstance, Type specifiedType) { var methods = typeof(SpecificationHoldersHelpers).GetMethods(BindingFlags.NonPublic | BindingFlags.Static); var method = methods.First(m => m.Name == nameof(AddSpecificationFromHolder)).MakeGenericMethod(specifiedType); try { method.Invoke(null, new[] { options, holderInstance }); } catch (Exception ex) { if (ex.InnerException is InvalidSpecificationHolderException) { throw ex.InnerException; } throw; } }
/// <summary> /// Adds specification of type <typeparamref name="T" />. /// </summary> /// <param name="options"></param> /// <param name="specification">Specification of type <typeparamref name="T" /></param> /// <typeparam name="T">Specified type.</typeparam> /// <returns></returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="specification" /> is null.</exception> public static IValidationContextOptions AddSpecification <T>(this IValidationContextOptions options, Specification <T> specification) where T : class { if (specification == null) { throw new ArgumentNullException(nameof(specification)); } return(OptionsUnwrapper.UnwrapSpecifications(options, specifications => { if (specifications.ContainsKey(typeof(T))) { specifications[typeof(T)] = specification; } else { specifications.Add(typeof(T), specification); } })); }
public static IValidationContextOptions GetVerifiedValidationContextOptions(IValidationContextOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } var compiledOptions = new ValidationContextOptions { Translations = GetVerifiedTranslations(options.Translations.ToArray()), Specifications = GetVerifiedSpecificationsDictionary(options.Specifications), ValidationOptions = GetVerifiedValidationOptions(options.ValidationOptions ?? throw new InvalidOperationException($"Null {nameof(ValidationContextOptions.ValidationOptions)}")) }; if (options.ValidationOptions.TranslationName != null) { VerifyTranslationName(compiledOptions.Translations.ToArray(), options.ValidationOptions.TranslationName); } return(compiledOptions); }
/// <summary> /// Add all specifications from the selected <see cref="ISpecificationHolder" />. /// </summary> /// <param name="options"></param> /// <param name="specificationsHolder"> /// Object that contains specifications. Should implement at least one /// <see cref="ISpecificationHolder{T}" />. /// </param> /// <returns></returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="specificationsHolder" /> is null.</exception> /// <exception cref="InvalidOperationException"> /// Thrown if <paramref name="specificationsHolder" /> doesn't contain /// specifications. /// </exception> public static IValidationContextOptions AddSpecificationsFromHolder(this IValidationContextOptions options, ISpecificationHolder specificationsHolder) { if (specificationsHolder == null) { throw new ArgumentNullException(nameof(specificationsHolder)); } var specifiedTypes = SpecificationHoldersHelpers.GetSpecifiedTypes(specificationsHolder.GetType()); if (!specifiedTypes.Any()) { throw new InvalidSpecificationHolderException($"Type passed as {nameof(specificationsHolder)} should implement at least one {typeof(ISpecificationHolder<>).Name} type"); } foreach (var specifiedType in specifiedTypes) { SpecificationHoldersHelpers.InvokeAddSpecificationFromHolder(options, specificationsHolder, specifiedType); } return(options); }
/// <summary> /// Default translation name. Used to create <see cref="ITranslationProxy.DefaultTranslator" /> in the /// <see cref="IValidationResult{T}" />. /// </summary> /// <param name="options"></param> /// <param name="translationName">Translation name.</param> public static IValidationContextOptions SetTranslationName(this IValidationContextOptions options, string translationName) { options.ValidationOptions.SetTranslationName(translationName); return(options); }
public void GetMerged_Should_ThrowException_When_NullOptions(IValidationContextOptions baseOptions, IValidationContextOptions newOptions) { Assert.Throws <ArgumentNullException>(() => { OptionsService.GetMerged(baseOptions, newOptions); }); }
/// <summary> /// Sets the key (a member name) for the collection's members errors if validating using /// <see cref="ValidationStrategy.Force" /> strategy. /// </summary> /// <param name="options"></param> /// <param name="collectionForceKey"> /// Key (a member name) for the collection's members errors if validating using /// <see cref="ValidationStrategy.Force" /> strategy. /// </param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="collectionForceKey" /> is null.</exception> public static IValidationContextOptions SetCollectionForceKey(this IValidationContextOptions options, string collectionForceKey) { options.ValidationOptions.SetCollectionForceKey(collectionForceKey); return(options); }
/// <summary> /// Sets the behavior for the null reference passed to be validated. /// </summary> /// <param name="options"></param> /// <param name="nullRootStrategy">Behavior for the null reference passed to be validated.</param> public static IValidationContextOptions SetNullRootStrategy(this IValidationContextOptions options, NullRootStrategy nullRootStrategy) { options.ValidationOptions.SetNullRootStrategy(nullRootStrategy); return(options); }
/// <summary> /// Disables the default translation. <see cref="ITranslationProxy.DefaultTranslator" /> in the /// <see cref="IValidationResult{T}" /> will be using original phrases. /// </summary> /// <param name="options"></param> public static IValidationContextOptions SetTranslationDisabled(this IValidationContextOptions options) { options.ValidationOptions.SetTranslationDisabled(); return(options); }
/// <summary> /// Sets the maximum allowed level of depth within the validated model. The default value is 10. /// </summary> /// <param name="options"></param> /// <param name="maxDepth">Max depth.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="maxDepth" /> is less than zero.</exception> public static IValidationContextOptions SetMaxDepth(this IValidationContextOptions options, int maxDepth) { options.ValidationOptions.SetMaxDepth(maxDepth); return(options); }
/// <summary> /// Sets the default error added to the member if it's invalid but no error message is assigned. /// </summary> /// <param name="options"></param> /// <param name="errorMessage">Error message.</param> /// <exception cref="ArgumentNullException">Thrown if <see cref="errorMessage" /> is null.</exception> public static IValidationContextOptions SetDefaultError(this IValidationContextOptions options, string errorMessage) { options.ValidationOptions.SetDefaultError(errorMessage); return(options); }
/// <summary> /// Sets the default strategy of the validation process. /// </summary> /// <param name="options"></param> /// <param name="validationStrategy">Strategy of the validation process.</param> public static IValidationContextOptions SetValidationStrategy(this IValidationContextOptions options, ValidationStrategy validationStrategy) { options.ValidationOptions.SetValidationStrategy(validationStrategy); return(options); }