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 JSON Schema validation to Lua #23

Merged
merged 2 commits into from
Aug 23, 2022
Merged

Conversation

javierguerragiraldez
Copy link
Contributor

New 'go.jsonschema' module, exports two functions:

compile( <schematext> [, <draftname>] )

Where <schematext> is a string containing a JSONSchema and <draftname>
is one of "draft4", "draft6", "draft7", "draft2019" or "draft2020".
If omitted, "draft4" is assumed.

If successful, a compiled schema object will be returned, on error it
returns nil and an error message.

validate( <schema>, <input> )

Where <schema> is a compiled schema object and <input> is some JSON
text.

If succesful, returns a single true value. If the validation failes,
returns false and a descriptive error. On any other error, returns nil
and an error message.

@javierguerragiraldez javierguerragiraldez requested a review from a team as a code owner August 3, 2022 05:42
@hbagdi
Copy link
Member

hbagdi commented Aug 3, 2022

What is the reason for not being able to use the Lua library for this? Is it using a dynamic shared object?

@javierguerragiraldez
Copy link
Contributor Author

What is the reason for not being able to use the Lua library for this? Is it using a dynamic shared object?

from a quick review of ljsonschema, i saw:

  • it only supports Draft4 (i don't know if newer schemas can be added or if they would need updates in Lua code)
  • it generates Lua code, which works great in LuaJIT, but could get heavy on pure interpreters.
  • it's written for OpenResty, relying on a few libraries that are easy to replace, but have different corner cases
  • it does expect cjson behaviour specifically, mostly around ambiguities in Lua about arrays vs objects.


func unmarshal(s string) (interface{}, error) {
decoder := json.NewDecoder(strings.NewReader(s))
decoder.UseNumber()
Copy link
Member

Choose a reason for hiding this comment

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

Why is this being done? Does Lua's cjson decode numbers as string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

copy/pasta error.

if err := decoder.Decode(&doc); err != nil {
return nil, err
}
if t, _ := decoder.Token(); t != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Should this not always return an error == io.EOF?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, we're expecting a nil response. i'll change to check that the error is actually io.EOF


l.Push(lua.LFalse)
l.Push(lua.LString(err.Error()))
}
Copy link
Member

Choose a reason for hiding this comment

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

Is the code missing a return here?

New 'go.jsonschema' module, exports two functions:

compile( <schematext> [, <draftname>] )

Where <schematext> is a string containing a JSONSchema and <draftname>
is one of "draft4", "draft6", "draft7", "draft2019" or "draft2020".
If omitted, "draft4" is assumed.

If successful, a compiled schema object will be returned, on error it
returns nil and an error message.

validate( <schema>, <input> )

Where <schema> is a compiled schema object and <input> is some JSON
text.

If succesful, returns a single true value.  If the validation failes,
returns false and a descriptive error.  On any other error, returns nil
and an error message.
@hbagdi hbagdi merged commit 2dc1334 into main Aug 23, 2022
@hbagdi hbagdi deleted the feat/jsonschema-validation branch August 23, 2022 17:10
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.

2 participants