-
Notifications
You must be signed in to change notification settings - Fork 101
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
Crash in WASM app because of calls to filesystem functions #58
Comments
I tested the same and it works. below is the program I have tested: package main
import (
"encoding/json"
"fmt"
"github.com/santhosh-tekuri/jsonschema/v5"
)
func main() {
schema := `{"type": "object"}`
instance := `{"foo": "bar"}`
sch, err := jsonschema.CompileString("mem://schema.json", schema)
if err != nil {
panic(err)
}
var v interface{}
if err := json.Unmarshal([]byte(instance), &v); err != nil {
panic(err)
}
err = sch.Validate(v)
fmt.Println("error:", err)
} note that if you use if no scheme is specified, then this library assumes that it is file. since wasm does not implement filesystem calls, we have to avoid this by using some dummy scheme say |
I don't know how you got this to work as WASM, but it fails on chrome before main starts running. The problem is the relative references in the schema drafts. While initializing the drafts, it calls isAbs, which then goes on to call filesystem funcs to resolve directories. |
relative urls are resolved against the document url. in case of draft, the document url is of type http url. so it never lands up using filesystem calls. try the sample i mentioned above at your end and check if it still fails |
BTW, I am using safari on mac |
I tested on chrome on mac. it works fine. |
As I said, in my case it is not even hitting |
Are you using V5 version |
It is the v5 version. Here's the stack trace, obtained by commenting out my fix:
|
I guess the path |
Turns out annotations.json is one of my files. And using mem://annotations.json fixes the problem. So we can close this ticket and the PR if you want, or the PR can still stay so that it works for relative JSON schemas in WASM. Sorry for the trouble, |
Without PR it works for relative json also |
How so? I have to change the filename to |
yes. you have to use absolute url like but any relative paths used inside |
in case anybody else is wondering how to use this lib with wasm I'll leave those snippets here: TinyGo compiles without errors on my machine, but the generated wasm file does not load in the browser because of an error package main
import (
"encoding/json"
"syscall/js"
"github.com/santhosh-tekuri/jsonschema/v5"
)
func main() {
js.Global().Set("JSONSchemaValidate", js.FuncOf(validate))
<-make(chan bool)
}
func validate(this js.Value, args []js.Value) interface{} {
compiler := jsonschema.NewCompiler()
compiler.Draft = jsonschema.Draft2020
// get an object
schema := args[0].String()
instance := args[1].String()
compiledSchema, err := jsonschema.CompileString("mem://schema.json", schema)
if err != nil {
panic(err)
}
var parsedJSON interface{}
if err := json.Unmarshal([]byte(instance), &parsedJSON); err != nil {
panic(err)
}
err = compiledSchema.Validate(parsedJSON)
if err != nil {
if validationError, ok := err.(*jsonschema.ValidationError); ok {
b, _ := json.Marshal(validationError.DetailedOutput())
return string(b)
} else {
panic(err)
}
}
return nil
} <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Go wasm</title>
</head>
<body>
<script src="wasm_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(
fetch("./main.wasm"),
go.importObject
).then((res) => {
go.run(res.instance);
console.time("validate");
const result = JSONSchemaValidate(
`{"properties": { "val": {"const": 10}, "val2": {"const": 5}}, "required": ["val", "val2"]}`,
`{"val": 9, "val2": 1}`
);
console.timeEnd("validate");
console.log({ result: JSON.parse(result) });
});
</script>
</body>
</html> |
@manuschillerdev I did not try this with tinygo. This is part of a rather large project, and my binary size is around 15M. |
JSON schema crashes at WASM app startup during the initialization of the drafts. The reason is the call to
stat
function, which is not implemented for the WASM platform. The call is intoAbs
function where it callsfilepath.Abs
.The text was updated successfully, but these errors were encountered: