How to access configuration values from appsettings.json file inside ValidationAttribute class?

From this article, you will gain a knowledge on how to access appsettings.json properties or configuration values inside Custom Validation Attribute class.

To achieve this, First We should have a custom validation attribute class that implements ValidationAttribute class.

Add Override IsValid method inside our custom validator class.

Please find the sample code snippet for this scenario,

We are going to write a unit test for the line no’s 10 and 12 using MOQ framework.

The following code has IConfiguration and IServiceProvider interfaces because these two are dependencies to our CustomValidator class. All these are mocking using MOQ framework.

It’s your turn now to use the above code snippet and pass your own values and test it. I believe this article helps you to solve your problem and saves your valuable time.

— Happy Learning

--

--

Nuthan Murarysetty

I love sharing things what I know to others and passionate in photography.