- Clients shouldn't have to parse different response bodies to find out what went wrong.
400 Bad Request
will give back response bodies complying with the standard contract.
- Clients shouldn't have to parse text to figure out what sort of error happened.
- errors have a unique integer ID, split into ranges to denote error classes
...(in a filter, or interceptor), making your code easier to understand.
... by running _validator.ValidateAndThrow(thingToProcess);
- the ValidationException
is caught and transformed to the standard error response DTO
Each filter or interceptor has a virtual method to allow you to log a warning in your logging framework of choice.
- API consumers want to be able to handle errors in a standard way
- anything else is wasted time
- implementing
- debugging
- supporting
- operating
- anything else is wasted time
Please follow the guidelines in CONTRIBUTING.md.
- CI
- To build, currently, use Visual Studio. We plan to open-source our build scripts, but haven't yet.
- Define your API contract as normal.
- Define a class inheriting from
AbstractValidator<TDto>
. - Make that class visible to IoC.
-> validation will occur within model-binding, and if the DTO is invalid, a 400 Bad Request
will result, with a standard error response, with code 40000
.
- Define a class inheriting from
AbstractValidator<TThingYouWantToApplyRulesTo>
. - Make that class visible to IoC, take a dependency on it.
- Call
_validator.ValidateAndThrow(input);
in your controller action or handler or service class, or wherever you need to apply rules.
-> A ValidationException
will be thrown and caught & handled by the filter/interceptor, and a 400 Bad Request
will result, with a standard error response, with code 45000
.
See the example in src/JE.ApiValidation.Examples.WebApi
, and the tests for that in src/JE.ApiValidation.Examples.Tests.WebApi
.
Register all implementations FluentValidation's AbstractValidators. These will then run automatically against any matching classes passed into ControllerActions.
See the example in src/JE.ApiValidation.Examples.WebApi.FluentValidation.StructureMap
, and the tests for that in src/JE.ApiValidation.Examples.Tests.WebApi
.
See the example in src/JE.ApiValidation.Examples.OpenRasta
, and the tests for that in src/JE.ApiValidation.Examples.Tests.OpenRasta
.