Beispiel #1
0
        /// <summary>
        /// Validates a specific named options instance (or all when <paramref name="name"/> is null).
        /// </summary>
        /// <param name="name">The name of the options instance being validated.</param>
        /// <param name="options">The options instance.</param>
        /// <returns>The <see cref="ValidateOptionsResult"/> result.</returns>
        public ValidateOptionsResult Validate(string name, TOptions options)
        {
            // Null name is used to configure all named options.
            if (Name == null || name == Name)
            {
                var validationResults = new List <ValidationResult>();
                if (Validator.TryValidateObject(options,
                                                new ValidationContext(options, serviceProvider: null, items: null),
                                                validationResults,
                                                validateAllProperties: true))
                {
                    return(ValidateOptionsResult.Success);
                }

                var errors = new List <string>();
                foreach (ValidationResult r in validationResults)
                {
                    errors.Add($"DataAnnotation validation failed for members: '{string.Join(",", r.MemberNames)}' with the error: '{r.ErrorMessage}'.");
                }
                return(ValidateOptionsResult.Fail(errors));
            }

            // Ignored if not validating this instance.
            return(ValidateOptionsResult.Skip);
        }
        public ValidateOptionsResult Validate(string?name, TOptions options)
        {
            // Null name is used to configure all named options.
            if (Name != null && Name != name)
            {
                // Ignored if not validating this instance.
                return(ValidateOptionsResult.Skip);
            }

            // Ensure options are provided to validate against
            ThrowHelper.ThrowIfNull(options);

            var validationResults = new List <ValidationResult>();

            if (Validator.TryValidateObject(options, new ValidationContext(options), validationResults, validateAllProperties: true))
            {
                return(ValidateOptionsResult.Success);
            }

            string typeName = options.GetType().Name;
            var    errors   = new List <string>();

            foreach (ValidationResult result in validationResults)
            {
                errors.Add($"DataAnnotation validation failed for '{typeName}' members: '{string.Join(",", result.MemberNames)}' with the error: '{result.ErrorMessage}'.");
            }

            return(ValidateOptionsResult.Fail(errors));
        }
        /// <summary>
        /// Validates a specific named options instance (or all when name is null).
        /// </summary>
        /// <param name="name">The name of the options instance being validated.</param>
        /// <param name="options">The options instance.</param>
        /// <returns>The <see cref="ValidateOptionsResult"/> result.</returns>
        public ValidateOptionsResult Validate(string name, TOptions options)
        {
            // Null name is used to configure all named options.
            if (Name == null || name == Name)
            {
                if ((Validation?.Invoke(options)).Value)
                {
                    return(ValidateOptionsResult.Success);
                }
                return(ValidateOptionsResult.Fail(FailureMessage));
            }

            // Ignored if not validating this instance.
            return(ValidateOptionsResult.Skip);
        }
Beispiel #4
0
        /// <summary>
        /// Returns a configured <typeparamref name="TOptions"/> instance with the given <paramref name="name"/>.
        /// </summary>
        public TOptions Create(string name)
        {
            TOptions options = CreateInstance(name);

            foreach (IConfigureOptions <TOptions> setup in _setups)
            {
                if (setup is IConfigureNamedOptions <TOptions> namedSetup)
                {
                    namedSetup.Configure(name, options);
                }
                else if (name == Options.DefaultName)
                {
                    setup.Configure(options);
                }
            }
            foreach (IPostConfigureOptions <TOptions> post in _postConfigures)
            {
                post.PostConfigure(name, options);
            }

            if (_validations.Length > 0)
            {
                var failures = new List <string>();
                foreach (IValidateOptions <TOptions> validate in _validations)
                {
                    ValidateOptionsResult result = validate.Validate(name, options);
                    if (result is not null && result.Failed)
                    {
                        failures.AddRange(result.Failures);
                    }
                }
                if (failures.Count > 0)
                {
                    throw new OptionsValidationException(name, typeof(TOptions), failures);
                }
            }

            return(options);
        }
Beispiel #5
0
        /// <summary>
        /// Validates a specific named options instance (or all when name is null).
        /// </summary>
        /// <param name="name">The name of the options instance being validated.</param>
        /// <param name="options">The options instance.</param>
        /// <returns>The <see cref="ValidateOptionsResult"/> result.</returns>
        public ValidateOptionsResult Validate(string name, TOptions options)
        {
            // Null name is used to configure all named options.
            if (Name == null || name == Name)
            {
                var validationResults = new List <ValidationResult>();
                if (Validator.TryValidateObject(options,
                                                new ValidationContext(options, serviceProvider: null, items: null),
                                                validationResults,
                                                validateAllProperties: true))
                {
                    return(ValidateOptionsResult.Success);
                }

                return(ValidateOptionsResult.Fail(String.Join(Environment.NewLine,
                                                              validationResults.Select(r => "DataAnnotation validation failed for members " +
                                                                                       String.Join(", ", r.MemberNames) +
                                                                                       " with the error '" + r.ErrorMessage + "'."))));
            }

            // Ignored if not validating this instance.
            return(ValidateOptionsResult.Skip);
        }