-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
41 lines (35 loc) · 1.09 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#![deny(clippy::all)]
#[macro_use]
extern crate napi_derive;
use jsonschema::{CompilationOptions, Draft, JSONSchema};
use serde_json::from_str;
use napi::{
CallContext, Error, JsBoolean, JsFunction, JsObject, JsString, JsUndefined, Result, Status,
};
#[cfg(all(
any(windows, unix),
target_arch = "x86_64",
not(target_env = "musl"),
not(debug_assertions)
))]
#[global_allocator]
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
#[module_exports]
fn init(mut exports: JsObject) -> Result<()> {
exports.create_named_method("compile", compile)?;
Ok(())
}
#[js_function(1)]
fn compile(ctx: CallContext) -> Result<JsFunction> {
let arg0 = ctx.get::<JsString>(0)?.into_utf8()?;
let schema = from_str(arg0.as_str()?)?;
let compiled = JSONSchema::options()
.with_draft(Draft::Draft7)
.compile(&schema)
.map_err(|e| Error::new(Status::InvalidArg, format!("{}", e)))?;
ctx.env.create_function_from_closure("isValid", move |ctx| {
let arg0 = ctx.get::<JsString>(0)?.into_utf8()?;
let input = from_str(arg0.as_str()?)?;
ctx.env.get_boolean(compiled.is_valid(&input))
})
}