Skip to content

Add ODataQueryOptions.TryValidate #1419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
KnapSac opened this issue Feb 19, 2025 · 1 comment
Open

Add ODataQueryOptions.TryValidate #1419

KnapSac opened this issue Feb 19, 2025 · 1 comment
Assignees

Comments

@KnapSac
Copy link

KnapSac commented Feb 19, 2025

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" );
	}
}
@julealgon
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants