Skip to content
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

topdown: Make *.is_valid function behavior more consistent #4844

Conversation

philipaconrad
Copy link
Contributor

This PR changes the behavior of the *.is_valid builtins to consistently return only true/false values, with no runtime/builtin errors. No docs changes should be required, since this is the documented behavior of these functions.

The affected list of builtins:

Encoding:

  • base64.is_valid
  • json.is_valid
  • yaml.is_valid

GraphQL:

  • graphql.is_valid

Regex:

  • regex.is_valid (already implements behavior)

Semver:

  • semver.is_valid

Questionable additions to the list:

  • io.jwt.verifyX functions?

Fixes #4760.

Copy link
Member

@ashutosh-narkar ashutosh-narkar left a comment

Choose a reason for hiding this comment

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

This looks good. Any tests need to be added/updated. Also does this affect strict-builtin-errors behavior?

@philipaconrad
Copy link
Contributor Author

philipaconrad commented Jul 6, 2022

Any tests need to be added/updated. Also does this affect strict-builtin-errors behavior?

I'll update the affected tests soon.

This does not change strict-builtin-errors behavior overall, but does mean that the builtins in the affected list above will no longer produce builtin errors / undefined when they hit an error condition, such as a bad argument. Instead, they will just return false.

anderseknert
anderseknert previously approved these changes Jul 6, 2022
Copy link
Member

@anderseknert anderseknert left a comment

Choose a reason for hiding this comment

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

Thanks for picking this up!

perhaps it would be good to have a test with strict built in errors set to true, and assert the same behavior as when set to false?

@philipaconrad
Copy link
Contributor Author

perhaps it would be good to have a test with strict built in errors set to true, and assert the same behavior as when set to false?

--strict-builtin-errors causes actual errors though for the test cases that are changed here. 🤔 My understanding of the original issue was to replace the builtin error cases for the affected is_valid functions with return false cases.

Are there some types of errors we want to keep, even if they affect the true/false outputs?

@anderseknert
Copy link
Member

@philipaconrad OK! How does that work, given the functions return boolean and not errors now? If I provide e.g. an int to semver.is_valid, is that false without strict mode and an error with it on?

srenatus
srenatus previously approved these changes Jul 6, 2022
@srenatus
Copy link
Contributor

srenatus commented Jul 6, 2022

If I provide e.g. an int to semver.is_valid, is that false without strict mode and an error with it on?

It'll be false, regardless of strict-builtin-errors.

@anderseknert
Copy link
Member

It'll be false, regardless of strict-builtin-errors.

Thanks @srenatus. This is what I wanted a test to assert.

@philipaconrad philipaconrad force-pushed the issue-4760-consistent-is-valid-behavior branch from d1c8797 to 591535b Compare July 6, 2022 14:56
@philipaconrad philipaconrad force-pushed the issue-4760-consistent-is-valid-behavior branch 2 times, most recently from e4f13fa to d034006 Compare July 6, 2022 15:36
@philipaconrad
Copy link
Contributor Author

Changes squashed, should be ready-for-merge.

The old test cases that checked for builtin errors on the affected *.is_valid builtins now check for the value false, so I think that should cover most of what @anderseknert requested in the way of test cases.

@philipaconrad philipaconrad force-pushed the issue-4760-consistent-is-valid-behavior branch from d034006 to 9ee5bd7 Compare July 6, 2022 15:40
@anderseknert
Copy link
Member

@philipaconrad not terribly important, but what I wanted was a test that kept the strict_error attribute set to true, and asserted that the value returned is still false.

@philipaconrad philipaconrad force-pushed the issue-4760-consistent-is-valid-behavior branch from 9ee5bd7 to be94f17 Compare July 6, 2022 16:58
This commit updates the `*.is_valid` functions to no longer produce
errors when providing wrong-typed arguments. Instead, they will now
return true/false for all inputs. Tests and WASM versions of these
builtins have been updated to match the new behavior.

Fixes open-policy-agent#4760.

Signed-off-by: Philip Conrad <philipaconrad@gmail.com>
@philipaconrad philipaconrad force-pushed the issue-4760-consistent-is-valid-behavior branch from be94f17 to 7539067 Compare July 6, 2022 17:01
@philipaconrad
Copy link
Contributor Author

@philipaconrad not terribly important, but what I wanted was a test that kept the strict_error attribute set to true, and asserted that the value returned is still false.

@anderseknert Done. I took the old json.is_valid tests, and added the strict_error attribute on each of them, which I think should do what you were asking for.

@philipaconrad philipaconrad merged commit 41dc9f7 into open-policy-agent:main Jul 6, 2022
@philipaconrad philipaconrad deleted the issue-4760-consistent-is-valid-behavior branch July 6, 2022 17:20
@philipaconrad philipaconrad restored the issue-4760-consistent-is-valid-behavior branch July 6, 2022 17:24
@philipaconrad philipaconrad deleted the issue-4760-consistent-is-valid-behavior branch September 14, 2022 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent runtime type checks in *.is_valid functions
4 participants