Skip to content

Commit

Permalink
speed up validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoskal committed Sep 9, 2024
1 parent 41f9c45 commit 75ff181
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
22 changes: 17 additions & 5 deletions parser/src/json.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 75ff181

Please sign in to comment.