diff --git a/parser/Cargo.toml b/parser/Cargo.toml index 29d1385e..a596b91f 100644 --- a/parser/Cargo.toml +++ b/parser/Cargo.toml @@ -13,6 +13,7 @@ rustc-hash = "2.0.0" instant = "0.1.13" jsonschema = { version = "0.18.1", default-features = false } url = "2.5.2" +lazy_static = "1.5.0" [features] default = [] diff --git a/parser/src/json.rs b/parser/src/json.rs index de11c1c9..421efeb6 100644 --- a/parser/src/json.rs +++ b/parser/src/json.rs @@ -1,4 +1,6 @@ use anyhow::{anyhow, bail, Result}; +use jsonschema::JSONSchema; +use lazy_static::lazy_static; use serde_json::{json, Value}; use std::{collections::HashMap, sync::Arc, vec}; @@ -212,6 +214,19 @@ impl jsonschema::SchemaResolver for DummyResolver { } } +lazy_static! { + static ref SCHEMA_VALIDATOR: JSONSchema = { + jsonschema::JSONSchema::options() + .with_draft(jsonschema::Draft::Draft7) + .with_resolver(DummyResolver {}) + .with_meta_schemas() + .compile(&json!({ + "$ref": "http://json-schema.org/draft-07/schema" + })) + .unwrap() + }; +} + impl Compiler { pub fn new(options: JsonCompileOptions) -> Self { Self { @@ -226,11 +241,8 @@ impl Compiler { pub fn run(&mut self, schema: &Value) -> Result<()> { if self.options.validate { - let _ = jsonschema::JSONSchema::options() - .with_draft(jsonschema::Draft::Draft7) - .with_resolver(DummyResolver {}) - .compile(schema) - .map_err(|e| anyhow!("Schema validation error: {}", e))?; + SCHEMA_VALIDATOR.validate(schema) + .map_err(|mut e| anyhow!("Invalid schema: {}", e.next().unwrap()))?; } self.builder.add_grammar(GrammarWithLexer {