/// <summary>
        ///     <para>
        ///     Initializes a new instance of the <see cref="FormatValidatorAttribute" /> class with a regex pattern,
        ///     matching options and a failure message template.
        ///     </para>
        /// </summary>
        /// <param name="pattern">The pattern to match.</param>
        /// <param name="patternResourceName">The resource name containing the pattern for the regular expression.</param>
        /// <param name="patternResourceType">The type containing the resource for the regular expression.</param>
        /// <param name="options">The <see cref="RegexOptions" /> to use when matching.</param>
        internal FormatValidatorAttribute(string pattern, string patternResourceName, Type patternResourceType, RegexOptions options)
        {
            ValidatorArgumentsValidatorHelper.ValidateRegexIgnoresEmptyStringValidator(pattern, patternResourceName, patternResourceType);

            _pattern             = pattern;
            _options             = options;
            _patternResourceName = patternResourceName;
            _patternResourceType = patternResourceType;
        }
Example #2
0
        /// <summary>
        ///     <para>
        ///     Initializes a new instance of the <see cref="FormatValidator" /> class with a regex pattern,
        ///     matching options and a failure message template.
        ///     </para>
        /// </summary>
        /// <param name="pattern">The pattern to match.</param>
        /// <param name="options">The <see cref="RegexOptions" /> to use when matching.</param>
        /// <param name="messageTemplate">The message template.</param>
        /// <param name="negated">True if the validator must negate the result of the validation.</param>
        /// <param name="patternResourceName">The resource name containing the pattern for the regular expression.</param>
        /// <param name="patternResourceType">The type containing the resource for the regular expression.</param>
        public FormatValidator(string pattern, string patternResourceName, Type patternResourceType, RegexOptions options, string messageTemplate, bool negated)
            : base(messageTemplate, null, negated)
        {
            ValidatorArgumentsValidatorHelper.ValidateRegexIgnoresEmptyStringValidator(pattern, patternResourceName, patternResourceType);

            Pattern             = pattern;
            _options            = options;
            PatternResourceName = patternResourceName;
            PatternResourceType = patternResourceType;
        }
        /// <summary>
        ///     <para>
        ///     Initializes a new instance of the <see cref="FormatValidatorAttribute" /> class with a regex pattern,
        ///     matching options and a failure message template.
        ///     </para>
        /// </summary>
        /// <param name="pattern">The pattern to match.</param>
        /// <param name="patternResourceName">The resource name containing the pattern for the regular expression.</param>
        /// <param name="patternResourceType">The type containing the resource for the regular expression.</param>
        /// <param name="options">The <see cref="RegexOptions"/> to use when matching.</param>        ///
        /// <param name="propertyName">The name of the property which validator depends on.</param>
        /// <param name="inverse">True if the validator must inverse the propertyName value.</param>
        internal FormatValidatorAttribute(string pattern, string patternResourceName, Type patternResourceType, RegexOptions options, string propertyName = null, bool inverse = false)
        {
            ValidatorArgumentsValidatorHelper.ValidateRegexIgnoresEmptyStringValidator(pattern, patternResourceName, patternResourceType);

            _pattern             = pattern;
            _options             = options;
            _propertyName        = propertyName;
            _inverse             = inverse;
            _patternResourceName = patternResourceName;
            _patternResourceType = patternResourceType;
        }
Example #4
0
        protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            bool isContinue = ValidatorArgumentsValidatorHelper.CheckIsNeedtoValidate(_dependPropertyName, _inverse, currentTarget);

            if (isContinue)
            {
                if (objectToValidate != null)
                {
                    base.DoValidate(objectToValidate, currentTarget, key, validationResults);
                }
            }
        }
Example #5
0
        /// <summary>
        ///     <para>
        ///     Initializes a new instance of the <see cref="FormatValidator" /> class with a regex pattern,
        ///     matching options and a failure message template.
        ///     </para>
        /// </summary>
        /// <param name="pattern">The pattern to match.</param>
        /// <param name="options">The <see cref="RegexOptions" /> to use when matching.</param>
        /// <param name="messageTemplate">The message template.</param>
        /// <param name="negated">True if the validator must negate the result of the validation.</param>
        /// <param name="propertyName">The name of the property which validator depends on.</param>
        /// <param name="inverse">True if the validator must inverse the propertyName value.</param>
        /// <param name="patternResourceName">The resource name containing the pattern for the regular expression.</param>
        /// <param name="patternResourceType">The type containing the resource for the regular expression.</param>
        public FormatValidator(string pattern, string patternResourceName, Type patternResourceType, RegexOptions options, string messageTemplate, bool negated, string propertyName = null, bool inverse = false)
            : base(messageTemplate, null, negated)
        {
            ValidatorArgumentsValidatorHelper.ValidateRegexIgnoresEmptyStringValidator(pattern, patternResourceName, patternResourceType);

            Pattern             = pattern;
            _options            = options;
            _dependPropertyName = propertyName;
            _inverse            = inverse;
            PatternResourceName = patternResourceName;
            PatternResourceType = patternResourceType;
        }
Example #6
0
        /// <summary>
        /// Проверяет обязательность заполнения <paramref name="objectToValidate"/>
        /// </summary>
        /// <param name="objectToValidate">объект для проверки</param>
        /// <param name="currentTarget">объект, на котором производится валидация</param>
        /// <param name="key">ключ, который идентифицирует источник <paramref name="objectToValidate"/></param>
        /// <param name="validationResults">результаты валидации</param>
        public override void DoValidate(object objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            bool isContinue = ValidatorArgumentsValidatorHelper.CheckIsNeedtoValidate(_dependPropertyName, _inverse, currentTarget);

            if (isContinue)
            {
                var isValid = !string.IsNullOrEmpty(objectToValidate?.ToString());
                if (isValid == Negated)
                {
                    LogValidationResult(validationResults, GetMessage(objectToValidate, key), currentTarget, key);
                }
            }
        }
Example #7
0
        /// <summary>
        /// Validates by comparing <paramref name="objectToValidate" /> by matching it to the pattern
        /// specified for the validator.
        /// </summary>
        /// <param name="objectToValidate">The object to validate.</param>
        /// <param name="currentTarget">The object on the behalf of which the validation is performed.</param>
        /// <param name="key">The key that identifies the source of <paramref name="objectToValidate" />.</param>
        /// <param name="validationResults">The validation results to which the outcome of the validation should be stored.</param>
        /// <remarks>
        /// <see langword="null" /> is considered a failed validation.
        /// </remarks>
        protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            bool isContinue = ValidatorArgumentsValidatorHelper.CheckIsNeedtoValidate(_dependPropertyName, _inverse, currentTarget);

            if (isContinue)
            {
                var logError = Negated;
                if (!string.IsNullOrWhiteSpace(objectToValidate))
                {
                    var pattern = GetPattern();
                    if (Options != null)
                    {
                        var regex = new Regex(pattern, (RegexOptions)Options);
                        logError = !regex.IsMatch(objectToValidate);
                    }
                }

                if (logError != Negated)
                {
                    LogValidationResult(validationResults, GetMessage(objectToValidate, key), currentTarget, key);
                }
            }
        }