AspNetCoreOData
AspNetCoreOData copied to clipboard
Add ODataQueryOptions.TryValidate
There is currently no way to validate query options without throwing an exception. I'd like a TryValidate method, so I don't need to catch an exception to find out that the query options don't satisfy the settings. Ideally it would have an out parameter which includes the disallowed query option.
public ActionResult< IQueryable > Get(
ODataQueryOptions< TResult > queryOptions )
{
ODataValidationSettings validationSettings =
new( )
{
AllowedQueryOptions = AllowedQueryOptions.Select | AllowedQueryOptions.SkipToken,
MaxTop = 1000,
};
try
{
queryOptions.Validate( validationSettings );
}
catch ( Exception )
{
return BadRequest( "Unsupported query option" );
}
}
I've also had to make something similar in the past so definitely agree.
And please don't make the underlying implementation exception-based as that would miss an opportunity for performance improvement too.
Not sure if I 100% agree with your proposed API share, however. A bool TryValidate() method doesn't allow for more elaborate failure reporting. I think something similar to how FluentValidation does it would be better, where it can return multiple individual failures etc.