Stranger6667 / jsonschema-rs

JSON Schema validation library
https://docs.rs/jsonschema
MIT License
481 stars 90 forks source link

WASM bindings revisited #472

Open platoscave opened 2 months ago

platoscave commented 2 months ago

I'd like to use jsonschema in a wasm a environment. I tried this code (below). The app compiles just fine, however it crashes just as soon as it just as soon as it reaches JSONSchema::compile

let schema = json!({"maxLength": 5});
let compiled = JSONSchema::compile(&schema);

I tried different versions / features combinations based in previous issues regarding the same matter (#222, #395) . They each crash for different reasons.

jsonschema = { version = "=0.15.1", default-features = false, features = ["structopt"]} exception: no mapping for imported function sched_yield

jsonschema = { version = "0.17.0", default-features = false, features = ["resolve-file"]} exception: no mapping for imported function fd_read

As soon as I add resolve-http to the features I get: could not find blocking in reqwest, which is what we're trying to avoid.

Which features do I need to add in order to use JSONSchema in WASM environment?

Stranger6667 commented 2 months ago

Oh, thats problematic right now - I don't think that resolving works just yet, but it will as soon as I'll finish the rewrite. There will be async + blocking API for resolving refs via network. Not sure about the filesystem, but wasm32-wasi probably would work.

Also, I am going to run a full test suite on WASM as the implementation is ready.

Meanwhile - what is your rustc version and target?

platoscave commented 2 months ago

rustc version

rustc --version
rustc 1.76.0 (07dca489a 2024-02-04)

target: wasi-sdk-20.0

Looking forward to the rewrite!

Stranger6667 commented 2 months ago

Thanks! If you have any feedback on the API (current or the next version) & usability, feel free to leave a comment in this discussion

sbooeshaghi commented 1 month ago

Hi- I am not a power user and am quite new to developing in rust and using jsonschema so apologies in advance if this is a misinformed comment/question. I have written a rust package that checks an instance of a json schema using the jsonschema draft7 validator and it works great. I was following the Mozilla docs for compiling a rust package into wasm (https://developer.mozilla.org/en-US/docs/WebAssembly/Rust_to_Wasm) and it fails to compile due to what looks like a reqwest dependency in jsonschema.

wasm-pack build --target web

I get the same errors as https://github.com/Stranger6667/jsonschema-rs/issues/222#issuecomment-1185538808

Do you have a sense for why this is the case, how to overcome it, or if this is a fundamental challenge that the new API will address? Thank you!