Skip to content

Conversation

WanjohiSammy
Copy link
Member

Issues

This pull request fixes #1419.

Description

This PR introduces a TryValidate methods to ODataQueryOptions addressing the current limitation where query options can only be validated by throwing exceptions.

The TryValidate method provides a way to validate query options without relying on exception handling. Instead of relying on exception handling, this method returns a bool indicating success or failure and provides detailed validation errors through an out parameter.

Example Usage:

var filterOption = new FilterQueryOption("Name eq 'John'", context);
var settings = new ODataValidationSettings();

if (_validator.TryValidate(filterOption, settings, out IEnumerable<string> validationErrors))
{
    Console.WriteLine("Validation succeeded!");
}
else
{
    Console.WriteLine("Validation failed:");
    foreach (var error in validationErrors)
    {
        Console.WriteLine(error);
    }
}

Checklist (Uncheck if it is not completed)

  • Test cases added
  • Build and test with one-click build and test script passed

Additional work necessary

If documentation update is needed, please add "Docs Needed" label to the issue and provide details about the required document change in the issue.

@WanjohiSammy
Copy link
Member Author

/AzurePipelines run OData-AspNetCoreOData-main-rolling-1ES

Copy link

No pipelines are associated with this pull request.

@WanjohiSammy
Copy link
Member Author

/AzurePipelines run OData-AspNetCoreOData-main-rolling-1ES

Copy link

No pipelines are associated with this pull request.

@WanjohiSammy
Copy link
Member Author

/AzurePipelines run OData-AspNetCoreOData-main-rolling-1ES

Copy link

No pipelines are associated with this pull request.

/// <param name="validationSettings">The validation settings.</param>
/// <param name="validationErrors">Contains a collection of validation errors encountered, or an empty collection if validation succeeds.</param>
/// <returns><see langword="true"/> if the validation succeeded; otherwise, <see langword="false"/>.</returns>
bool TryValidate(ComputeQueryOption computeQueryOption, ODataValidationSettings validationSettings, out IEnumerable<string> validationErrors);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this pattern, shall we call 'Validate' within 'TryValidate' and avoid duplicate the validation codes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid breaking changes, can we use the default interface method so we don't avoid the breaking changes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it the time to add 'async' interface method?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add ODataQueryOptions.TryValidate

2 participants