This repository has been archived by the owner. It is now read-only.

Add support for options validation #266

Merged
merged 1 commit into from Jul 30, 2018

Conversation

Projects
None yet
4 participants
@HaoK
Member

HaoK commented Jun 29, 2018

Adds the ability to validate options via a new IValidateOptions interface that the IOptionsFactory consumes if registered:

General use case looks like this and is only exposed off of the OptionsBuilder:

            services.AddOptions<ComplexOptions>()
                .Configure(o =>
                {
                    o.Boolean = false;
                    o.Integer = 11;
                })
                .Validate(o => o.Boolean)
                .Validate(o => o.Integer > 12);
            var sp = services.BuildServiceProvider();
            var error = Assert.Throws<OptionsValidationException>(() => sp.GetRequiredService<IOptions<ComplexOptions>>().Value);
            var errors = error.Failures.ToArray();
            Assert.Equal(2, errors.Length);
            Assert.Equal("A validation error has occured.", errors[0]);
            Assert.Equal("A validation error has occured.", errors[1]);

Thoughts @davidfowl ?

I've already gone over the general idea with @ajcvickers and we think it looks promising.

@natemcmaster natemcmaster changed the base branch from dev to master Jul 2, 2018

@davidfowl

This comment has been minimized.

Member

davidfowl commented Jul 3, 2018

The feature looks generally useful. What's the need for 2.2? Which components are going to use it?

@ajcvickers

This comment has been minimized.

Member

ajcvickers commented Jul 3, 2018

@davidfowl Let's discuss in the leads sync today.

@HaoK

This comment has been minimized.

Member

HaoK commented Jul 11, 2018

There isn't an explicit need for this in 2.2 as opposed to 3.0, Security/Auth would probably want to switch to using this in some form to validate options.

The other thing that we should try to tackle as part of this is enabling some way to trigger validation at startup instead of on first use, maybe via IStartupFilter like this blog post: https://andrewlock.net/adding-validation-to-strongly-typed-configuration-objects-in-asp-net-core/

@HaoK

This comment has been minimized.

Member

HaoK commented Jul 12, 2018

Addresses aspnet/AspNetCore#2388 which I've moved into 2.2

@HaoK

This comment has been minimized.

Member

HaoK commented Jul 27, 2018

Gonna put this into 2.2, and look at triggering start up validation in a separate PR

@HaoK HaoK changed the base branch from master to release/2.2 Jul 27, 2018

@HaoK HaoK merged commit cea619f into release/2.2 Jul 30, 2018

4 checks passed

VSTS: Options-ci 20180727.1 succeeded
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
license/cla All CLA requirements met.
Details
@davidfowl

This comment has been minimized.

Member

davidfowl commented Aug 4, 2018

@rynowak

This comment has been minimized.

Member

rynowak commented Aug 4, 2018

@davidfowl @HaoK It's neat, is there a suggestion about any MVC features where we'd use it?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.