Пример #1
0
        /// <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 IValidationOptions SetCollectionForceKey(this IValidationOptions options, string collectionForceKey)
        {
            if (collectionForceKey == null)
            {
                throw new ArgumentNullException(nameof(collectionForceKey));
            }

            return(OptionsUnwrapper.UnwrapValidationOptions(options, validationOptions => { validationOptions.CollectionForceKey = collectionForceKey; }));
        }
Пример #2
0
        /// <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 IValidationOptions SetMaxDepth(this IValidationOptions options, int maxDepth)
        {
            if (maxDepth < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(maxDepth), maxDepth, $"{nameof(maxDepth)} cannot be less than 0");
            }

            return(OptionsUnwrapper.UnwrapValidationOptions(options, validationOptions => { validationOptions.MaxDepth = maxDepth; }));
        }
Пример #3
0
        /// <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);
        }
Пример #4
0
        /// <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);
                }
            }));
        }
Пример #5
0
        /// <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);
                }
            }));
        }
Пример #6
0
        /// <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 IValidationOptions SetDefaultError(this IValidationOptions options, string errorMessage)
        {
            var requiredError = new Error(errorMessage);

            return(OptionsUnwrapper.UnwrapValidationOptions(options, validationOptions => { validationOptions.DefaultError = requiredError; }));
        }
Пример #7
0
 /// <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 IValidationOptions SetValidationStrategy(this IValidationOptions options, ValidationStrategy validationStrategy)
 {
     return(OptionsUnwrapper.UnwrapValidationOptions(options, validationOptions => { validationOptions.ValidationStrategy = validationStrategy; }));
 }
Пример #8
0
 /// <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 IValidationOptions SetTranslationDisabled(this IValidationOptions options)
 {
     return(OptionsUnwrapper.UnwrapValidationOptions(options, validationOptions => { validationOptions.TranslationName = null; }));
 }
Пример #9
0
 /// <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 IValidationOptions SetTranslationName(this IValidationOptions options, string translationName)
 {
     return(OptionsUnwrapper.UnwrapValidationOptions(options, validationOptions => { validationOptions.TranslationName = translationName; }));
 }
Пример #10
0
 /// <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 IValidationOptions SetNullRootStrategy(this IValidationOptions options, NullRootStrategy nullRootStrategy)
 {
     return(OptionsUnwrapper.UnwrapValidationOptions(options, validationOptions => { validationOptions.NullRootStrategy = nullRootStrategy; }));
 }