Comments (15)
@manuschillerdev I did not try this with tinygo. This is part of a rather large project, and my binary size is around 15M.
from jsonschema.
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 "schema.json"
instead of "mem://shema.json"
then i get the same error that you got.
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 mem
from jsonschema.
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.
from jsonschema.
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
from jsonschema.
BTW, I am using safari on mac
from jsonschema.
I tested on chrome on mac. it works fine.
from jsonschema.
As I said, in my case it is not even hittingmain
. It crashed before that, in init()
. I added some printlns, and the last thing it prints is one of the meta/
components of the schema drafts. It is a relative reference in the draft text.
from jsonschema.
Are you using V5 version
from jsonschema.
It is the v5 version. Here's the stack trace, obtained by commenting out my fix:
path is: annotations.json // This fmt.Println is just before this in isAbs(): if s, err = filepath.Abs(s); err != nil {
wasm_exec.js:51 panic: stat .: not implemented on js
wasm_exec.js:51
wasm_exec.js:51 goroutine 1 [running]:
wasm_exec.js:51 github.com/santhosh-tekuri/jsonschema/v5.MustCompileString({0x15dbc0, 0x10}, {0x15102e, 0x2})
wasm_exec.js:51 /home/bserdar/github.com/cloudprivacylabs/lsa-playground/vendor/github.com/santhosh-tekuri/jsonschema/v5/compiler.go:68 +0x1c
wasm_exec.js:51 github.com/cloudprivacylabs/lsa/pkg/json.init()
wasm_exec.js:51 /home/bserdar/github.com/cloudprivacylabs/lsa-playground/vendor/github.com/cloudprivacylabs/lsa/pkg/json/import.go:90 +0x2
from jsonschema.
MustCompileString
is never called in the library. it must be getting called by your code. you can check this by looking at usages of MustCompilieString
.
I guess the path annotations.json
is coming from your code/schema
from jsonschema.
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,
from jsonschema.
Without PR it works for relative json also
from jsonschema.
How so? I have to change the filename to mem://annotations.json
to make it work. annotations.json
still crashes.
from jsonschema.
yes. you have to use absolute url like mem://annotations.json
. because relative path is assumed to be file relative to current working directory.
but any relative paths used inside annotations.json
are resolve against mem://annotations.json
. Only the root resource is interpreted as file if relative path is given.
from jsonschema.
in case anybody else is wondering how to use this lib with wasm I'll leave those snippets here:
@bserdar did you happen to get this to work with tinygo insteand of go? The binary size of the generated .wasm file is around 4MB, which is a lot for the browser.
TinyGo compiles without errors on my machine, but the generated wasm file does not load in the browser because of an error reflect.Type.NumMethod() is unimplemented
(see tinygo-org/tinygo#2660 for reference)
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>
from jsonschema.
Related Issues (20)
- Provide CLI binaries with release HOT 3
- Link in README for YAML playground example is broken HOT 1
- Issue with custom Regex Library HOT 10
- `Compiler.AddResource` for adding pre-compiled schemas HOT 6
- Feature: Adding Context to Validate Method HOT 2
- implementing json.Marshaler interface HOT 9
- Create JSONSchema from Compile()'s result HOT 2
- Wrong unevaluatedProperties validation errors HOT 6
- compiler.go bug in 171
- Invalid JSON schema does not error out HOT 2
- [bug] Not reading from stdin HOT 2
- Missing document file path in validation output when validating multiple documents HOT 5
- Error messages are revealing absolute file paths HOT 1
- YouValidateMe JSON Schema library server project built on this library
- ECMA-262 as the default regex in this library? HOT 1
- jsonschema.Compiler has no field or method CompileRegex HOT 7
- Question: parsing Schema to string HOT 1
- error is missing instanceLocation HOT 1
- Cannot install older version than `@latest` HOT 3
- The latest release doesn't build HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonschema.