예제 #1
0
        /// <summary>
        /// Validates the current scope nullable value against the specification. The error output is saved in the current scope.
        /// This is a scope command - its error output can be altered with any of the parameter commands (WithCondition, WithPath, WithMessage, WithExtraMessage, WithCode, WithExtraCode).
        /// </summary>
        /// <param name="this">Fluent API builder - input.</param>
        /// <param name="specification">Specification for the value type wrapped in the nullable type.</param>
        /// <typeparam name="T">The value type wrapped in the nullable type.</typeparam>
        /// <returns>Fluent API builder - output.</returns>
        public static IRuleOut <T?> AsNullable <T>(this IRuleIn <T?> @this, Specification <T> specification)
            where T : struct
        {
            ThrowHelper.NullArgument(@this, nameof(@this));

            return(((SpecificationApi <T?>)@this).AddCommand(new AsNullableCommand <T>(specification)));
        }
예제 #2
0
        public static IRequiredOut <T> Required <T>(this IRequiredIn <T> @this)
            where T : class
        {
            ThrowHelper.NullArgument(@this, nameof(@this));

            return(((SpecificationApi <T>)@this).AddCommand(RequiredCommand.Instance));
        }
        public static IRuleOut <T> AsCollection <T, TItem>(this IRuleIn <T> @this, Specification <TItem> specification)
            where T : IEnumerable <TItem>
        {
            ThrowHelper.NullArgument(@this, nameof(@this));

            return(((SpecificationApi <T>)@this).AddCommand(new AsCollectionCommand <T, TItem>(specification)));
        }
예제 #4
0
        public static ValidatorSettings WithTranslation(this ValidatorSettings @this, ITranslationHolder translationHolder)
        {
            ThrowHelper.NullArgument(@this, nameof(@this));
            ThrowHelper.NullArgument(translationHolder, nameof(translationHolder));

            return(@this.WithTranslation(translationHolder.Translations));
        }
예제 #5
0
        /// <inheritdoc cref="Optional{T}(Validot.Specification.IOptionalIn{T})"/>
        public static IOptionalOut <T?> Optional <T>(this IOptionalIn <T?> @this)
            where T : struct
        {
            ThrowHelper.NullArgument(@this, nameof(@this));

            return(((SpecificationApi <T?>)@this).AddCommand(OptionalCommand.Instance));
        }
예제 #6
0
        public static IForbiddenOut <T> Forbidden <T>(this IForbiddenIn <T> @this)
            where T : class
        {
            ThrowHelper.NullArgument(@this, nameof(@this));

            return(((SpecificationApi <T>)@this).AddCommand(ForbiddenCommand.Instance));
        }
예제 #7
0
        public static ValidatorSettings WithTranslation(this ValidatorSettings @this, IReadOnlyDictionary <string, IReadOnlyDictionary <string, string> > translations)
        {
            ThrowHelper.NullArgument(@this, nameof(@this));
            ThrowHelper.NullArgument(translations, nameof(translations));

            foreach (var translation in translations)
            {
                @this.WithTranslation(translation.Key, translation.Value);
            }

            return(@this);
        }
예제 #8
0
        public static ValidatorSettings WithTranslation(this ValidatorSettings @this, string name, IReadOnlyDictionary <string, string> translation)
        {
            ThrowHelper.NullArgument(@this, nameof(@this));
            ThrowHelper.NullArgument(name, nameof(name));
            ThrowHelper.NullArgument(translation, nameof(translation));

            foreach (var entry in translation)
            {
                @this.WithTranslation(name, entry.Key, entry.Value);
            }

            return(@this);
        }
예제 #9
0
        public static string ResolvePath(string basePath, string relativePath)
        {
            ThrowHelper.NullArgument(basePath, nameof(basePath));
            ThrowHelper.NullArgument(relativePath, nameof(relativePath));

            if (relativePath.Length == 0)
            {
                return(basePath);
            }

            if (basePath.Length == 0)
            {
                if (relativePath[0] != UpperLevelPointer)
                {
                    return(relativePath);
                }
            }

            if (relativePath[0] == UpperLevelPointer)
            {
                var up = 0;

                while (++up < relativePath.Length)
                {
                    if (relativePath[up] != UpperLevelPointer)
                    {
                        break;
                    }
                }

                var newSegmentCore = relativePath.TrimStart(UpperLevelPointer);

                for (var i = basePath.Length - 2; i >= 0; --i)
                {
                    if (basePath[i] == Divider && --up == 0)
                    {
                        if (newSegmentCore.Length == 0)
                        {
                            return(basePath.Substring(0, i));
                        }

                        return(basePath.Substring(0, i + 1) + newSegmentCore);
                    }
                }

                return(newSegmentCore);
            }

            return($"{basePath}.{relativePath}");
        }
예제 #10
0
        /// <summary>
        /// Adds Polish translation dictionary for the error messages used in the built-in rules.
        /// </summary>
        /// <param name="this">Settings fluent API builder - input.</param>
        /// <returns>Settings fluent API builder - output.</returns>
        public static ValidatorSettings WithPolishTranslation(this ValidatorSettings @this)
        {
            ThrowHelper.NullArgument(@this, nameof(@this));

            return(@this.WithTranslation(nameof(Translation.Polish), Translation.Polish));
        }
예제 #11
0
        /// <summary>
        /// Overwrites the path of the current scope's error output.
        /// This is a parameter command - it can be followed by a new scope command or other parameter commands: WithMessage, WithExtraMessage, WithCode, WithExtraCode.
        /// </summary>
        /// <param name="this">Fluent API builder - input.</param>
        /// <param name="path">New path of the current scope's error output. To nest it more deeply, simply use dots (e.g., "More.Nested.Level"). To move it to the upper level, use `&lt;` character (e.g., "&lt;", "&lt;&lt;TwoLevelsUp.And.ThreeDown").</param>
        /// <typeparam name="T">Type of the current scope value.</typeparam>
        /// <returns>Fluent API builder - output.</returns>
        public static IWithPathOut <T> WithPath <T>(this IWithPathIn <T> @this, string path)
        {
            ThrowHelper.NullArgument(@this, nameof(@this));

            return(((SpecificationApi <T>)@this).AddCommand(new WithPathCommand(path)));
        }
예제 #12
0
        public static IRuleOut <string> Matches(this IRuleIn <string> @this, string pattern)
        {
            ThrowHelper.NullArgument(pattern, nameof(pattern));

            return(@this.RuleTemplate(v => Regex.IsMatch(v, pattern, RegexOptions.CultureInvariant), MessageKey.Texts.Matches, Arg.Text(nameof(pattern), pattern)));
        }
예제 #13
0
        public static IRuleOut <string> NotContains(this IRuleIn <string> @this, string value, StringComparison stringComparison = StringComparison.Ordinal)
        {
            ThrowHelper.NullArgument(value, nameof(value));

            return(@this.RuleTemplate(v => v.IndexOf(value, stringComparison) < 0, MessageKey.Texts.NotContains, Arg.Text(nameof(value), value), Arg.Enum(nameof(stringComparison), stringComparison)));
        }
예제 #14
0
        /// <summary>
        /// Sets execution condition of the closest preceding scope command. If the condition is not met, the validation logic is not even executed.
        /// This is a parameter command - it can be followed by a new scope command or other parameter commands: WithPath, WithMessage, WithExtraMessage, WithCode, WithExtraCode.
        /// </summary>
        /// <param name="this">Fluent API builder - input.</param>
        /// <param name="executionCondition">Execution condition of the closest preceding scope command. A predicate that receives the current scope's value and determines whether the validation logic should be executed (returns true if yes, otherwise - false).</param>
        /// <typeparam name="T">Type of the current scope value.</typeparam>
        /// <returns>Fluent API builder - output.</returns>
        public static IWithConditionOut <T> WithCondition <T>(this IWithConditionIn <T> @this, Predicate <T> executionCondition)
        {
            ThrowHelper.NullArgument(@this, nameof(@this));

            return(((SpecificationApi <T>)@this).AddCommand(new WithConditionCommand <T>(executionCondition)));
        }
예제 #15
0
        public static IRuleOut <string> Matches(this IRuleIn <string> @this, Regex pattern)
        {
            ThrowHelper.NullArgument(pattern, nameof(pattern));

            return(@this.RuleTemplate(pattern.IsMatch, MessageKey.Texts.Matches, Arg.Text(nameof(pattern), pattern.ToString())));
        }
예제 #16
0
        public static IRuleOut <T> RuleTemplate <T>(this IRuleIn <T> @this, Predicate <T> predicate, string key, params IArg[] args)
        {
            ThrowHelper.NullArgument(@this, nameof(@this));

            return(((SpecificationApi <T>)@this).AddCommand(new RuleCommand <T>(predicate, key, args)));
        }
예제 #17
0
        public static IRuleOut <string> NotEqualTo(this IRuleIn <string> @this, string value, StringComparison stringComparison = StringComparison.Ordinal)
        {
            ThrowHelper.NullArgument(value, nameof(value));

            return(@this.RuleTemplate(v => !string.Equals(v, value, stringComparison), MessageKey.Texts.NotEqualTo, Arg.Text(nameof(value), value), Arg.Enum(nameof(stringComparison), stringComparison)));
        }
예제 #18
0
        /// <summary>
        /// Contains no validation logic and no meaning. It's purpose is to visually separate rules in the fluent API method chain.
        /// </summary>
        /// <param name="this">Fluent API builder - input.</param>
        /// <typeparam name="T">Type of the specified model.</typeparam>
        /// <returns>Fluent API builder - output.</returns>
        public static IAndOut <T> And <T>(this IAndIn <T> @this)
        {
            ThrowHelper.NullArgument(@this, nameof(@this));

            return((SpecificationApi <T>)@this);
        }
예제 #19
0
        /// <summary>
        /// Validates the current scope value against the specification. The error output is saved in the current scope.
        /// This is a scope command - its error output can be altered with any of the parameter commands (WithCondition, WithPath, WithMessage, WithExtraMessage, WithCode, WithExtraCode).
        /// </summary>
        /// <param name="this">Fluent API builder - input.</param>
        /// <param name="specification">Specification for the current scope value.</param>
        /// <typeparam name="T">Type of the current scope value.</typeparam>
        /// <returns>Fluent API builder - output.</returns>
        public static IRuleOut <T> AsModel <T>(this IRuleIn <T> @this, Specification <T> specification)
        {
            ThrowHelper.NullArgument(@this, nameof(@this));

            return(((SpecificationApi <T>)@this).AddCommand(new AsModelCommand <T>(specification)));
        }
예제 #20
0
        public static IRuleOut <T> Member <T, TMember>(this IRuleIn <T> @this, Expression <Func <T, TMember> > memberSelector, Specification <TMember> specification)
        {
            ThrowHelper.NullArgument(@this, nameof(@this));

            return(((SpecificationApi <T>)@this).AddCommand(new MemberCommand <T, TMember>(memberSelector, specification)));
        }
예제 #21
0
        public static IWithExtraCodeOut <T> WithExtraCode <T>(this IWithExtraCodeIn <T> @this, string code)
        {
            ThrowHelper.NullArgument(@this, nameof(@this));

            return(((SpecificationApi <T>)@this).AddCommand(new WithExtraCodeCommand(code)));
        }
예제 #22
0
        public static IRuleOut <T> Rule <T>(this IRuleIn <T> @this, Predicate <T> predicate)
        {
            ThrowHelper.NullArgument(@this, nameof(@this));

            return(((SpecificationApi <T>)@this).AddCommand(new RuleCommand <T>(predicate)));
        }
예제 #23
0
        public static IWithMessageOut <T> WithMessage <T>(this IWithMessageIn <T> @this, string message)
        {
            ThrowHelper.NullArgument(@this, nameof(@this));

            return(((SpecificationApi <T>)@this).AddCommand(new WithMessageCommand(message)));
        }
예제 #24
0
        public static IRuleOut <string> StartsWith(this IRuleIn <string> @this, string value, StringComparison stringComparison = StringComparison.Ordinal)
        {
            ThrowHelper.NullArgument(value, nameof(value));

            return(@this.RuleTemplate(v => v.StartsWith(value, stringComparison), MessageKey.Texts.StartsWith, Arg.Text(nameof(value), value), Arg.Enum(nameof(stringComparison), stringComparison)));
        }