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

built-in function to check is schema valid. is_schema_valid(jsonObj, jsonShema) returning true/false #5417

Closed
olegroom opened this issue Nov 28, 2022 · 6 comments

Comments

@olegroom
Copy link

adding bult-in function in rego to check is json object valid against json schema

What is the underlying problem you're trying to solve?

Describe the ideal solution

Describe a "Good Enough" solution

Additional Context

@olegroom
Copy link
Author

we need this functionality in OPA and we can implement this function. The only we need is detailed guide how to do that?

@olegroom
Copy link
Author

olegroom commented Nov 28, 2022

Is it OK if function declaration would be like this ?
res := json.is_valid(jsonObj, jsonSchema)

res := {
is_valid : true
error : nill
}

or

res := {
is_valid : false
error : No required field "example"
}

@srenatus
Copy link
Contributor

Heya! Thanks for your willingness to contribute! 👏

That proposed return value is something we need to reconsider, though: all other xyz.is_valid() methods in OPA return true/false only.

I don't believe there's a precedent here, we don't have methods that would return a list of errors if validation fails. Is that your use case?

@jkulvich
Copy link
Contributor

Heya! Thanks for your willingness to contribute! 👏

That proposed return value is something we need to reconsider, though: all other xyz.is_valid() methods in OPA return true/false only.

I don't believe there's a precedent here, we don't have methods that would return a list of errors if validation fails. Is that your use case?

Hi! Let me explain our case.
We would like to validate some JSON object with JSON schema, and have an error if occurred.
Both these JSONs (schema and doc) passes with input or data.

So, next points are important for us:

  1. Validation flag.
  2. Validation error.
  3. Performance (single function call for both values above).

And, I can image and implement one of next contracts:

  1. jsonschema.match(schema, doc): string | nil - Just like regexp.match. In this case we can use a nil string as a success validation flag.
  2. jsonschema.validate(schema, doc): { valid: bool, error: string | nil } - More comfortable way to get only what we need with single built-in call. But I couldn't find any implemented built-in with similar return signature, and I'm not sure that this is acceptable by OPA conventions.

Of course, we can follow classical way with 2 built-ins .is_valid(): bool, and .validate(): string. But performance.

Can I have any advice? Thanks!

@srenatus
Copy link
Contributor

I've just realised that there has been some prior discussion here, too: #1449

Just catching up myself 😅 👀

@anderseknert
Copy link
Member

Indeed. Since there is already an open issue for this, with several upvotes, and prior discussion, let's continue there.

@srenatus srenatus closed this as not planned Won't fix, can't repro, duplicate, stale Nov 28, 2022
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

4 participants