예제 #1
0
        /// <inheritdoc />
        public IEnumerable <ValidationResult> Validate(object?value, bool required, string?format)
        {
            List <ValidationResult>?results = null;
            var r = Validators.SelectMany(v => v.Validate(value, ValueType, Configuration)).ToList();

            if (r.Any())
            {
                results = r;
            }

            // mandatory and regex validators cannot be part of valueEditor.Validators because they
            // depend on values that are not part of the configuration, .Mandatory and .ValidationRegEx,
            // so they have to be explicitly invoked here.

            if (required)
            {
                r = RequiredValidator.ValidateRequired(value, ValueType).ToList();
                if (r.Any())
                {
                    if (results == null)
                    {
                        results = r;
                    }
                    else
                    {
                        results.AddRange(r);
                    }
                }
            }

            var stringValue = value?.ToString();

            if (!string.IsNullOrWhiteSpace(format) && !string.IsNullOrWhiteSpace(stringValue))
            {
                r = FormatValidator.ValidateFormat(value, ValueType, format).ToList();
                if (r.Any())
                {
                    if (results == null)
                    {
                        results = r;
                    }
                    else
                    {
                        results.AddRange(r);
                    }
                }
            }

            return(results ?? Enumerable.Empty <ValidationResult>());
        }