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

add jsonschema_is_valid function and its tests #34

Merged
merged 3 commits into from
Jul 7, 2023

Conversation

imor
Copy link
Contributor

@imor imor commented Jul 5, 2023

What kind of change does this PR introduce?

Feature

What is the current behavior?

Fixes #28

What is the new behavior?

This PR adds a function jsonschema_is_valid(schema: Json) which returns true/false if the schema is valid/invalid. If the schema is invalid it also raises a notice which tells the user which part of the schema is invalid. For example when a valid schema is provided it return true:

pg_jsonschema=# select jsonschema_is_valid('{"type": "object", "properties": { "tags": { "type": "array", "items": { "type": "string", "maxLength": 16 } } } }');
 jsonschema_is_valid
---------------------
 t
(1 row)

And when an invalid schema is provided (notice typo sting instead of string in input schema):

pg_jsonschema=# select jsonschema_is_valid('{"type": "object", "properties": { "tags": { "type": "array", "items": { "type": "sting", "maxLength": 16 } } } }');
NOTICE:  Invalid JSON schema at path: /properties/tags/items
 jsonschema_is_valid
---------------------
 f
(1 row)

Additional context

@olirice olirice self-requested a review July 5, 2023 19:47
@olirice
Copy link
Collaborator

olirice commented Jul 5, 2023

thanks, this looks great. I'll check it out tomorrow and follow up then

Copy link
Collaborator

@olirice olirice left a comment

Choose a reason for hiding this comment

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

could you also add this function to README.md
in the same style as the existing functions?

src/lib.rs Outdated
Err(e) => {
notice!(
"Invalid JSON schema at path: {}",
e.instance_path.to_string()
Copy link
Collaborator

Choose a reason for hiding this comment

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

instance_path is not always available

pg_jsonschema=# select jsonschema_is_valid('"sdfg"');   
NOTICE:  Invalid JSON schema at path:               

could you skip the notice when no path is available?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@imor
Copy link
Contributor Author

imor commented Jul 6, 2023

could you also add this function to README.md in the same style as the existing functions?

Added to the README.

@imor imor requested a review from olirice July 6, 2023 17:34
@olirice
Copy link
Collaborator

olirice commented Jul 7, 2023

thanks!

@olirice olirice merged commit 47dc3e9 into supabase:master Jul 7, 2023
1 check passed
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.

Validating the schemae themselves
2 participants