Skip to content
This repository has been archived by the owner on Nov 7, 2018. It is now read-only.

Add support for options validation #266

Merged
merged 1 commit into from
Jul 30, 2018
Merged

Add support for options validation #266

merged 1 commit into from
Jul 30, 2018

Conversation

HaoK
Copy link
Member

@HaoK 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 July 2, 2018 17:58
@davidfowl
Copy link
Member

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

@ajcvickers
Copy link
Member

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

@HaoK
Copy link
Member Author

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
Copy link
Member Author

HaoK commented Jul 12, 2018

Addresses dotnet/aspnetcore#2388 which I've moved into 2.2

@HaoK
Copy link
Member Author

HaoK commented Jul 27, 2018

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

@davidfowl
Copy link
Member

cc @rynowak @anurse

@rynowak
Copy link
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.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants