Example #1
0
        public void Catches_validation_error_for_Test1_file()
        {
            var services      = new ServiceCollection();
            var configuration = ConfigurationTestBuilder
                                .BuildFromEmbeddedResource(JsonIndex.AttributeValidated.Test1);
            var sectionName = SettingsSectionNameAttribute
                              .GetSettingsSectionName <AttributeValidatedSettings>();

            // the next call will throw an exception
            Action act = () =>
            {
                services.AddEagerlyValidatedSettings <AttributeValidatedSettings>(configuration, out _);
            };

            var exception = Assert.Throws <OptionsValidationException>(act);

            Assert.StartsWith("Validation failed for members:", exception.Message);

            /* Full validation message looks like
             *
             * Validation failed for members: 'IntegerA' with the error: 'The field IntegerA must be between 1
             * and 100.'.; Validation failed for members: 'BooleanB' with the error: 'The BooleanB field is
             * required.'.
             */
        }
Example #2
0
        public void Test_reference_equals_1(string filename)
        {
            var services = new ServiceCollection();

            var configuration        = ConfigurationTestBuilder.BuildFromEmbeddedResource(filename);
            var sectionName          = SettingsSectionNameAttribute.GetSettingsSectionName <AttributeValidatedSettings>();
            var configurationSection = configuration.GetSection(sectionName);

            // This is the standard way, it results in lazy validation
            services.AddOptions <AttributeValidatedSettings>()
            .Bind(configurationSection)
            .RecursivelyValidateDataAnnotations();

            var serviceProvider = services.BuildServiceProvider();

            var result1 = serviceProvider.GetRequiredService <IOptions <AttributeValidatedSettings> >().Value;
            var result2 = serviceProvider.GetRequiredService <IOptions <AttributeValidatedSettings> >().Value;

            Assert.True(object.ReferenceEquals(result1, result2));
        }
Example #3
0
        public void Test_reference_equals_2(string filename)
        {
            var services = new ServiceCollection();

            var configuration  = ConfigurationTestBuilder.BuildFromEmbeddedResource(filename);
            var sectionName    = SettingsSectionNameAttribute.GetSettingsSectionName <AttributeValidatedSettings>();
            var simpleSettings = configuration.GetSection(sectionName).Get <AttributeValidatedSettings>();

            // This approach gives eager validation
            // Downside is that I think IOptionsMonitor<T> will not inform of changes

            var options = Options.Create <AttributeValidatedSettings>(simpleSettings);

            services.AddSingleton <IOptions <AttributeValidatedSettings> >(options);
            services.AddSingleton <IValidateOptions <AttributeValidatedSettings> >(
                new RecursiveDataAnnotationValidateOptions <AttributeValidatedSettings>(
                    null
                    ));

            var serviceProvider = services.BuildServiceProvider();

            var validator             = serviceProvider.GetRequiredService <IValidateOptions <AttributeValidatedSettings> >();
            var validateOptionsResult = validator.Validate(null, options.Value);

            if (validateOptionsResult.Failed)
            {
                throw new Exception();
            }

            var validatedSettings = options.Value;

            var result1 = validatedSettings;
            var result2 = serviceProvider.GetRequiredService <IOptions <AttributeValidatedSettings> >().Value;

            Assert.True(object.ReferenceEquals(result1, result2));
        }