Code Monkey home page Code Monkey logo

Comments (15)

bserdar avatar bserdar commented on July 30, 2024 1

@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.

santhosh-tekuri avatar santhosh-tekuri commented on July 30, 2024

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.

bserdar avatar bserdar commented on July 30, 2024

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.

santhosh-tekuri avatar santhosh-tekuri commented on July 30, 2024

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.

santhosh-tekuri avatar santhosh-tekuri commented on July 30, 2024

BTW, I am using safari on mac

from jsonschema.

santhosh-tekuri avatar santhosh-tekuri commented on July 30, 2024

I tested on chrome on mac. it works fine.

from jsonschema.

bserdar avatar bserdar commented on July 30, 2024

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.

santhosh-tekuri avatar santhosh-tekuri commented on July 30, 2024

Are you using V5 version

from jsonschema.

bserdar avatar bserdar commented on July 30, 2024

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.

santhosh-tekuri avatar santhosh-tekuri commented on July 30, 2024

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.

bserdar avatar bserdar commented on July 30, 2024

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.

santhosh-tekuri avatar santhosh-tekuri commented on July 30, 2024

Without PR it works for relative json also

from jsonschema.

bserdar avatar bserdar commented on July 30, 2024

How so? I have to change the filename to mem://annotations.json to make it work. annotations.json still crashes.

from jsonschema.

santhosh-tekuri avatar santhosh-tekuri commented on July 30, 2024

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.

manuschillerdev avatar manuschillerdev commented on July 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.