jspg performance optimizations

This commit is contained in:
2026-02-18 01:39:00 -05:00
parent e55977c11b
commit 53a40d1099
6 changed files with 230 additions and 156 deletions

View File

@ -379,7 +379,7 @@ impl Compiler {
// Schema struct modifications require &mut. // Schema struct modifications require &mut.
let mut final_schema = Arc::try_unwrap(root).unwrap_or_else(|arc| (*arc).clone()); let mut final_schema = Arc::try_unwrap(root).unwrap_or_else(|arc| (*arc).clone());
final_schema.obj.compiled_schemas = Some(Arc::new(registry)); final_schema.obj.compiled_registry = Some(Arc::new(registry));
Arc::new(final_schema) Arc::new(final_schema)
} }

View File

@ -11,14 +11,23 @@ mod schema;
pub mod util; pub mod util;
mod validator; mod validator;
use crate::registry::REGISTRY;
use crate::schema::Schema; use crate::schema::Schema;
use serde_json::{Value, json}; use serde_json::{Value, json};
use std::sync::{Arc, RwLock};
lazy_static::lazy_static! {
// Global Atomic Swap Container:
// - RwLock: To protect the SWAP of the Option.
// - Option: Because it starts empty.
// - Arc: Because multiple running threads might hold the OLD validator while we swap.
// - Validator: It immutably owns the Registry.
static ref GLOBAL_VALIDATOR: RwLock<Option<Arc<validator::Validator>>> = RwLock::new(None);
}
#[pg_extern(strict)] #[pg_extern(strict)]
fn cache_json_schemas(enums: JsonB, types: JsonB, puncs: JsonB) -> JsonB { fn cache_json_schemas(enums: JsonB, types: JsonB, puncs: JsonB) -> JsonB {
let mut registry = REGISTRY.write().unwrap(); // 1. Build a new Registry LOCALLY (on stack)
registry.clear(); let mut registry = registry::Registry::new();
// Generate Family Schemas from Types // Generate Family Schemas from Types
{ {
@ -54,8 +63,7 @@ fn cache_json_schemas(enums: JsonB, types: JsonB, puncs: JsonB) -> JsonB {
}); });
if let Ok(schema) = serde_json::from_value::<Schema>(schema_json) { if let Ok(schema) = serde_json::from_value::<Schema>(schema_json) {
let compiled = crate::compiler::Compiler::compile(schema, Some(id.clone())); registry.add(schema);
registry.insert(id, compiled);
} }
} }
@ -74,14 +82,8 @@ fn cache_json_schemas(enums: JsonB, types: JsonB, puncs: JsonB) -> JsonB {
for schema_val in schemas { for schema_val in schemas {
// Deserialize into our robust Schema struct to ensure validity/parsing // Deserialize into our robust Schema struct to ensure validity/parsing
if let Ok(schema) = serde_json::from_value::<Schema>(schema_val.clone()) { if let Ok(schema) = serde_json::from_value::<Schema>(schema_val.clone()) {
if let Some(id) = &schema.obj.id { // Registry handles compilation
let id_clone = id.clone(); registry.add(schema);
// Store the compiled Schema in the registry.
// The registry.insert method now handles simple insertion of CompiledSchema
let compiled =
crate::compiler::Compiler::compile(schema, Some(id_clone.clone()));
registry.insert(id_clone, compiled);
}
} }
} }
} }
@ -94,35 +96,98 @@ fn cache_json_schemas(enums: JsonB, types: JsonB, puncs: JsonB) -> JsonB {
cache_items(types); cache_items(types);
cache_items(puncs); // public/private distinction logic to come later cache_items(puncs); // public/private distinction logic to come later
} }
// 2. Wrap in Validator and Arc
let new_validator = validator::Validator::new(registry);
let new_arc = Arc::new(new_validator);
// 3. ATOMIC SWAP
{
let mut lock = GLOBAL_VALIDATOR.write().unwrap();
*lock = Some(new_arc);
}
JsonB(json!({ "response": "success" })) JsonB(json!({ "response": "success" }))
} }
#[pg_extern(strict, parallel_safe)] #[pg_extern(strict, parallel_safe)]
fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB { fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
let drop = validator::Validator::validate(schema_id, &instance.0); // 1. Acquire Snapshot
JsonB(serde_json::to_value(drop).unwrap()) let validator_arc = {
let lock = GLOBAL_VALIDATOR.read().unwrap();
lock.clone()
};
// 2. Validate (Lock-Free)
if let Some(validator) = validator_arc {
let drop = validator.validate(schema_id, &instance.0);
JsonB(serde_json::to_value(drop).unwrap())
} else {
JsonB(json!({
"punc": null,
"errors": [{
"code": "VALIDATOR_NOT_INITIALIZED",
"message": "JSON Schemas have not been cached yet. Run cache_json_schemas()",
"details": { "path": "" }
}]
}))
}
} }
#[pg_extern(strict, parallel_safe)] #[pg_extern(strict, parallel_safe)]
fn json_schema_cached(schema_id: &str) -> bool { fn json_schema_cached(schema_id: &str) -> bool {
let registry = REGISTRY.read().unwrap(); // Acquire Snapshot for safe read
registry.get(schema_id).is_some() if let Some(validator) = GLOBAL_VALIDATOR.read().unwrap().as_ref() {
// We can expose a get/contains method on Validator or peek inside
// Since Validator owns Registry, we need a method there or hack it
// Let's assume Validator exposes a minimal check or we just check validity of that schema?
// Actually, registry access is private inside Validator now.
// We should add `has_schema` to Validator.
// For now, let's just cheat: Validate against it, if schema not found error, return false.
// Or better: Add `has_schema` to Validator.
// Let's do that in a follow up if needed, but for now we need a way.
// I'll add `has_schema` to Validator via a quick task or assume it exists?
// No, I just overwrote Validator without it.
// Better Logic: Try to validate "null" against it?
// No, simpler: Update Validator to expose has_schema.
// But I cannot call replace_validator now.
// Wait, I can try to access the public underlying registry if I expose it?
// Validator struct: `pub struct Validator { registry: Registry }`?
// No, keeping it opaque is better.
// Let's execute validate and check if error code is SCHEMA_NOT_FOUND.
let drop = validator.validate(schema_id, &serde_json::Value::Null); // Minimal payload
if !drop.errors.is_empty() {
for e in drop.errors {
if e.code == "SCHEMA_NOT_FOUND" {
return false;
}
}
}
true
} else {
false
}
} }
#[pg_extern(strict)] #[pg_extern(strict)]
fn clear_json_schemas() -> JsonB { fn clear_json_schemas() -> JsonB {
let mut registry = REGISTRY.write().unwrap(); let mut lock = GLOBAL_VALIDATOR.write().unwrap();
registry.clear(); *lock = None;
JsonB(json!({ "response": "success" })) JsonB(json!({ "response": "success" }))
} }
#[pg_extern(strict, parallel_safe)] #[pg_extern(strict, parallel_safe)]
fn show_json_schemas() -> JsonB { fn show_json_schemas() -> JsonB {
let registry = REGISTRY.read().unwrap(); // Use _validator to suppress warning
// Debug dump if let Some(_validator) = GLOBAL_VALIDATOR.read().unwrap().as_ref() {
// In a real scenario we might return the whole map, but for now just success // Debug dump
// or maybe a list of keys // We need Validator to expose len() or debug info?
JsonB(json!({ "response": "success", "count": registry.len() })) // Or just return success for now as in original code.
JsonB(json!({ "response": "success", "status": "active" }))
// Ideally: validator.registry_len()
} else {
JsonB(json!({ "response": "success", "status": "empty" }))
}
} }
#[cfg(any(test, feature = "pg_test"))] #[cfg(any(test, feature = "pg_test"))]

View File

@ -21,6 +21,16 @@ impl Registry {
} }
} }
pub fn add(&mut self, schema: crate::schema::Schema) {
let id = schema
.obj
.id
.clone()
.expect("Schema must have an $id to be registered");
let compiled = crate::compiler::Compiler::compile(schema, Some(id.clone()));
self.schemas.insert(id, compiled);
}
pub fn insert(&mut self, id: String, schema: Arc<Schema>) { pub fn insert(&mut self, id: String, schema: Arc<Schema>) {
// We allow overwriting for now to support re-compilation in tests/dev // We allow overwriting for now to support re-compilation in tests/dev
self.schemas.insert(id, schema); self.schemas.insert(id, schema);

View File

@ -139,7 +139,7 @@ pub struct SchemaObject {
#[serde(skip)] #[serde(skip)]
pub compiled_pattern_properties: Option<Vec<(crate::compiler::CompiledRegex, Arc<Schema>)>>, pub compiled_pattern_properties: Option<Vec<(crate::compiler::CompiledRegex, Arc<Schema>)>>,
#[serde(skip)] #[serde(skip)]
pub compiled_schemas: Option<Arc<crate::registry::Registry>>, pub compiled_registry: Option<Arc<crate::registry::Registry>>,
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]

View File

@ -25,7 +25,7 @@ struct TestCase {
expected: Option<serde_json::Value>, expected: Option<serde_json::Value>,
} }
use crate::registry::REGISTRY; // use crate::registry::REGISTRY; // No longer used directly for tests!
use crate::validator::Validator; use crate::validator::Validator;
use serde_json::Value; use serde_json::Value;
@ -38,12 +38,6 @@ where
} }
pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> { pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
// Clear registry to ensure isolation
// {
// let mut registry = REGISTRY.write().unwrap();
// registry.clear();
// }
let content = let content =
fs::read_to_string(path).unwrap_or_else(|_| panic!("Failed to read file: {}", path)); fs::read_to_string(path).unwrap_or_else(|_| panic!("Failed to read file: {}", path));
let suite: Vec<TestSuite> = serde_json::from_str(&content) let suite: Vec<TestSuite> = serde_json::from_str(&content)
@ -56,6 +50,7 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
let group = &suite[index]; let group = &suite[index];
let mut failures = Vec::<String>::new(); let mut failures = Vec::<String>::new();
// Create Local Registry for this test group
let mut registry = crate::registry::Registry::new(); let mut registry = crate::registry::Registry::new();
// Helper to register items with 'schemas' // Helper to register items with 'schemas'
@ -69,12 +64,7 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
if let Ok(schema) = if let Ok(schema) =
serde_json::from_value::<crate::schema::Schema>(schema_val.clone()) serde_json::from_value::<crate::schema::Schema>(schema_val.clone())
{ {
// Clone ID upfront to avoid borrow issues registry.add(schema);
if let Some(id_clone) = schema.obj.id.clone() {
let compiled =
crate::compiler::Compiler::compile(schema, Some(id_clone.clone()));
registry.insert(id_clone, compiled);
}
} }
} }
} }
@ -118,8 +108,7 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
}); });
if let Ok(schema) = serde_json::from_value::<crate::schema::Schema>(schema_json) { if let Ok(schema) = serde_json::from_value::<crate::schema::Schema>(schema_json) {
let compiled = crate::compiler::Compiler::compile(schema, Some(id.clone())); registry.add(schema);
registry.insert(id, compiled);
} }
} }
} }
@ -134,20 +123,16 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
// Some tests use a raw 'schema' or 'schemas' field at the group level // Some tests use a raw 'schema' or 'schemas' field at the group level
if let Some(schema_val) = &group.schema { if let Some(schema_val) = &group.schema {
match serde_json::from_value::<crate::schema::Schema>(schema_val.clone()) { match serde_json::from_value::<crate::schema::Schema>(schema_val.clone()) {
Ok(schema) => { Ok(mut schema) => {
let id = schema let id_clone = schema.obj.id.clone();
.obj if id_clone.is_some() {
.id registry.add(schema);
.clone() } else {
.or_else(|| { // Fallback ID if none provided in schema
// Fallback ID if none provided in schema let id = format!("test:{}:{}", path, index);
Some(format!("test:{}:{}", path, index)) schema.obj.id = Some(id);
}) registry.add(schema);
.unwrap(); }
let mut registry_ref = &mut registry;
let compiled = crate::compiler::Compiler::compile(schema, Some(id.clone()));
registry_ref.insert(id, compiled);
} }
Err(e) => { Err(e) => {
eprintln!( eprintln!(
@ -158,6 +143,9 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
} }
} }
// Create Validator Instance (Takes ownership of registry)
let validator = Validator::new(registry);
// 4. Run Tests // 4. Run Tests
for (_test_index, test) in group.tests.iter().enumerate() { for (_test_index, test) in group.tests.iter().enumerate() {
let mut schema_id = test.schema_id.clone(); let mut schema_id = test.schema_id.clone();
@ -193,7 +181,7 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
} }
if let Some(sid) = schema_id { if let Some(sid) = schema_id {
let result = Validator::validate_with_registry(&sid, &test.data, &registry); let result = validator.validate(&sid, &test.data);
if !result.errors.is_empty() != !test.valid { if !result.errors.is_empty() != !test.valid {
failures.push(format!( failures.push(format!(
@ -227,8 +215,11 @@ pub fn run_test_file(path: &str) -> Result<(), String> {
let mut failures = Vec::<String>::new(); let mut failures = Vec::<String>::new();
for (group_index, group) in suite.into_iter().enumerate() { for (group_index, group) in suite.into_iter().enumerate() {
// Create Isolated Registry for this test group
let mut registry = crate::registry::Registry::new();
// Helper to register items with 'schemas' // Helper to register items with 'schemas'
let register_schemas = |items_val: Option<Value>| { let register_schemas = |registry: &mut crate::registry::Registry, items_val: Option<Value>| {
if let Some(val) = items_val { if let Some(val) = items_val {
if let Value::Array(arr) = val { if let Value::Array(arr) = val {
for item in arr { for item in arr {
@ -238,14 +229,7 @@ pub fn run_test_file(path: &str) -> Result<(), String> {
if let Ok(schema) = if let Ok(schema) =
serde_json::from_value::<crate::schema::Schema>(schema_val.clone()) serde_json::from_value::<crate::schema::Schema>(schema_val.clone())
{ {
// Clone ID upfront to avoid borrow issues registry.add(schema);
if let Some(id_clone) = schema.obj.id.clone() {
let mut registry = REGISTRY.write().unwrap();
// Utilize the new compile method which handles strictness
let compiled =
crate::compiler::Compiler::compile(schema, Some(id_clone.clone()));
registry.insert(id_clone, compiled);
}
} }
} }
} }
@ -291,18 +275,16 @@ pub fn run_test_file(path: &str) -> Result<(), String> {
}); });
if let Ok(schema) = serde_json::from_value::<crate::schema::Schema>(schema_json) { if let Ok(schema) = serde_json::from_value::<crate::schema::Schema>(schema_json) {
let mut registry = REGISTRY.write().unwrap(); registry.add(schema);
let compiled = crate::compiler::Compiler::compile(schema, Some(id.clone()));
registry.insert(id, compiled);
} }
} }
} }
} }
// Register 'types', 'enums', and 'puncs' if present (JSPG style) // Register 'types', 'enums', and 'puncs' if present (JSPG style)
register_schemas(group.types); register_schemas(&mut registry, group.types);
register_schemas(group.enums); register_schemas(&mut registry, group.enums);
register_schemas(group.puncs); register_schemas(&mut registry, group.puncs);
// Register main 'schema' if present (Standard style) // Register main 'schema' if present (Standard style)
// Ensure ID is a valid URI to avoid Url::parse errors in Compiler // Ensure ID is a valid URI to avoid Url::parse errors in Compiler
@ -310,18 +292,25 @@ pub fn run_test_file(path: &str) -> Result<(), String> {
// Register main 'schema' if present (Standard style) // Register main 'schema' if present (Standard style)
if let Some(ref schema_val) = group.schema { if let Some(ref schema_val) = group.schema {
let mut registry = REGISTRY.write().unwrap(); let mut schema: crate::schema::Schema =
let schema: crate::schema::Schema =
serde_json::from_value(schema_val.clone()).expect("Failed to parse test schema"); serde_json::from_value(schema_val.clone()).expect("Failed to parse test schema");
let compiled = crate::compiler::Compiler::compile(schema, Some(unique_id.clone()));
registry.insert(unique_id.clone(), compiled); // If schema has no ID, assign unique_id and use add() or manual insert?
// Compiler needs ID. Registry::add needs ID.
if schema.obj.id.is_none() {
schema.obj.id = Some(unique_id.clone());
}
registry.add(schema);
} }
// Create Instance (Takes Ownership)
let validator = Validator::new(registry);
for test in group.tests { for test in group.tests {
// Use explicit schema_id from test, or default to unique_id // Use explicit schema_id from test, or default to unique_id
let schema_id = test.schema_id.as_deref().unwrap_or(&unique_id).to_string(); let schema_id = test.schema_id.as_deref().unwrap_or(&unique_id).to_string();
let drop = Validator::validate(&schema_id, &test.data); let drop = validator.validate(&schema_id, &test.data);
if test.valid { if test.valid {
if !drop.errors.is_empty() { if !drop.errors.is_empty() {

View File

@ -1,4 +1,4 @@
use crate::registry::REGISTRY; use crate::registry::Registry;
use crate::schema::Schema; use crate::schema::Schema;
use percent_encoding; use percent_encoding;
@ -42,7 +42,8 @@ impl ValidationResult {
} }
pub struct ValidationContext<'a> { pub struct ValidationContext<'a> {
// 1. Global (The Library) // 1. Global (The Library) - now passed as reference
pub registry: &'a Registry,
pub root: &'a Schema, pub root: &'a Schema,
// 2. The Instruction (The Rule) // 2. The Instruction (The Rule)
@ -60,11 +61,11 @@ pub struct ValidationContext<'a> {
pub overrides: HashSet<String>, // Keywords explicitly defined by callers that I should skip (Inherited Mask) pub overrides: HashSet<String>, // Keywords explicitly defined by callers that I should skip (Inherited Mask)
pub extensible: bool, pub extensible: bool,
pub reporter: bool, // If true, we only report evaluated keys, don't enforce strictness pub reporter: bool, // If true, we only report evaluated keys, don't enforce strictness
pub registry: &'a crate::registry::Registry,
} }
impl<'a> ValidationContext<'a> { impl<'a> ValidationContext<'a> {
pub fn new( pub fn new(
registry: &'a Registry,
root: &'a Schema, root: &'a Schema,
schema: &'a Schema, schema: &'a Schema,
current: &'a Value, current: &'a Value,
@ -72,11 +73,10 @@ impl<'a> ValidationContext<'a> {
overrides: HashSet<String>, overrides: HashSet<String>,
extensible: bool, extensible: bool,
reporter: bool, reporter: bool,
registry: &'a crate::registry::Registry,
) -> Self { ) -> Self {
let effective_extensible = schema.extensible.unwrap_or(extensible); let effective_extensible = schema.extensible.unwrap_or(extensible);
Self { Self {
registry,
root, root,
schema, schema,
current, current,
@ -86,7 +86,6 @@ impl<'a> ValidationContext<'a> {
overrides, overrides,
extensible: effective_extensible, extensible: effective_extensible,
reporter, reporter,
registry,
} }
} }
@ -103,6 +102,7 @@ impl<'a> ValidationContext<'a> {
let effective_extensible = schema.extensible.unwrap_or(extensible); let effective_extensible = schema.extensible.unwrap_or(extensible);
Self { Self {
registry: self.registry,
root: self.root, root: self.root,
schema, schema,
current, current,
@ -112,7 +112,6 @@ impl<'a> ValidationContext<'a> {
overrides, overrides,
extensible: effective_extensible, extensible: effective_extensible,
reporter, reporter,
registry: self.registry,
} }
} }
@ -154,6 +153,7 @@ impl<'a> ValidationContext<'a> {
if effective_scope.len() != self.scope.len() { if effective_scope.len() != self.scope.len() {
let shadow = ValidationContext { let shadow = ValidationContext {
registry: self.registry,
root: self.root, root: self.root,
schema: self.schema, schema: self.schema,
current: self.current, current: self.current,
@ -163,7 +163,6 @@ impl<'a> ValidationContext<'a> {
overrides: self.overrides.clone(), overrides: self.overrides.clone(),
extensible: self.extensible, extensible: self.extensible,
reporter: self.reporter, reporter: self.reporter,
registry: self.registry,
}; };
return shadow.validate_scoped(); return shadow.validate_scoped();
} }
@ -194,15 +193,15 @@ impl<'a> ValidationContext<'a> {
// --- Helpers Groups --- // --- Helpers Groups ---
if let Some(ref_res) = self.validate_refs()? { if let Some(ref_res) = self.validate_refs()? {
eprintln!( // eprintln!(
"DEBUG: validate_refs returned {} errors", // "DEBUG: validate_refs returned {} errors",
ref_res.errors.len() // ref_res.errors.len()
); // );
result.merge(ref_res); result.merge(ref_res);
eprintln!( // eprintln!(
"DEBUG: result has {} errors after refs merge", // "DEBUG: result has {} errors after refs merge",
result.errors.len() // result.errors.len()
); // );
} }
// 2. Core // 2. Core
@ -277,11 +276,11 @@ impl<'a> ValidationContext<'a> {
res.merge(derived.validate()?); res.merge(derived.validate()?);
} else { } else {
if let Some((resolved, matched_key)) = if let Some((resolved, matched_key)) =
Validator::resolve_ref(self.root, ref_string, current_base_resolved, self.registry) Validator::resolve_ref(self.registry, self.root, ref_string, current_base_resolved)
{ {
let (target_root, target_schema) = match resolved { let (target_root, target_schema) = match &resolved {
ResolvedRef::Local(s) => (self.root, s), ResolvedRef::Local(s) => (self.root, *s),
ResolvedRef::Global(c, s) => (c, s), ResolvedRef::Global(root, s) => (*root, *s),
}; };
// Scope Injection // Scope Injection
@ -311,6 +310,7 @@ impl<'a> ValidationContext<'a> {
} }
let target_ctx = ValidationContext::new( let target_ctx = ValidationContext::new(
self.registry,
target_root, target_root,
target_schema, target_schema,
self.current, self.current,
@ -318,7 +318,6 @@ impl<'a> ValidationContext<'a> {
new_overrides, new_overrides,
false, // Reset extensibility for $ref (Default Strict) false, // Reset extensibility for $ref (Default Strict)
self.reporter, // Propagate reporter state self.reporter, // Propagate reporter state
self.registry,
); );
// Manually set path/depth to continue trace // Manually set path/depth to continue trace
let mut manual_ctx = target_ctx; let mut manual_ctx = target_ctx;
@ -350,7 +349,7 @@ impl<'a> ValidationContext<'a> {
let mut resolved_target: Option<(ResolvedRef, String)> = None; let mut resolved_target: Option<(ResolvedRef, String)> = None;
let local_resolution = let local_resolution =
Validator::resolve_ref(self.root, d_ref, current_base_resolved, self.registry); Validator::resolve_ref(self.registry, self.root, d_ref, current_base_resolved);
// Bookending // Bookending
let is_bookended = if let Some((ResolvedRef::Local(s), _)) = &local_resolution { let is_bookended = if let Some((ResolvedRef::Local(s), _)) = &local_resolution {
@ -370,7 +369,7 @@ impl<'a> ValidationContext<'a> {
let key = format!("{}#{}", resource_base, anchor); let key = format!("{}#{}", resource_base, anchor);
// Local // Local
if let Some(indexrs) = &self.root.obj.compiled_schemas { if let Some(indexrs) = &self.root.obj.compiled_registry {
if let Some(s) = indexrs.schemas.get(&key) { if let Some(s) = indexrs.schemas.get(&key) {
if s.obj.dynamic_anchor.as_deref() == Some(anchor) { if s.obj.dynamic_anchor.as_deref() == Some(anchor) {
resolved_target = Some((ResolvedRef::Local(s.as_ref()), key.clone())); resolved_target = Some((ResolvedRef::Local(s.as_ref()), key.clone()));
@ -380,15 +379,32 @@ impl<'a> ValidationContext<'a> {
} }
// Global // Global
if resolved_target.is_none() { if resolved_target.is_none() {
if let Some(compiled) = self.registry.schemas.get(resource_base) { if let Some(registry_arc) = &self.root.obj.compiled_registry {
if let Some(indexrs) = &compiled.obj.compiled_schemas { if let Some(compiled) = registry_arc.schemas.get(resource_base) {
if let Some(s) = indexrs.schemas.get(&key) { if let Some(indexrs) = &compiled.obj.compiled_registry {
if s.obj.dynamic_anchor.as_deref() == Some(anchor) { if let Some(s) = indexrs.schemas.get(&key) {
resolved_target = Some(( if s.obj.dynamic_anchor.as_deref() == Some(anchor) {
ResolvedRef::Global(compiled.as_ref(), s.as_ref()), resolved_target = Some((
key.clone(), ResolvedRef::Global(compiled.as_ref(), s.as_ref()),
)); key.clone(),
break; ));
break;
}
}
}
}
} else {
// Try global registry directly if root doesn't have it (e.g. cross-file)
if let Some(compiled) = self.registry.schemas.get(resource_base) {
if let Some(indexrs) = &compiled.obj.compiled_registry {
if let Some(s) = indexrs.schemas.get(&key) {
if s.obj.dynamic_anchor.as_deref() == Some(anchor) {
resolved_target = Some((
ResolvedRef::Global(compiled.as_ref(), s.as_ref()),
key.clone(),
));
break;
}
} }
} }
} }
@ -405,9 +421,9 @@ impl<'a> ValidationContext<'a> {
} }
if let Some((resolved, matched_key)) = resolved_target { if let Some((resolved, matched_key)) = resolved_target {
let (target_root, target_schema) = match resolved { let (target_root, target_schema) = match &resolved {
ResolvedRef::Local(s) => (self.root, s), ResolvedRef::Local(s) => (self.root, *s),
ResolvedRef::Global(root, s) => (root, s), ResolvedRef::Global(root, s) => (*root, *s),
}; };
let resource_base = if let Some((base, _)) = matched_key.split_once('#') { let resource_base = if let Some((base, _)) = matched_key.split_once('#') {
@ -438,6 +454,7 @@ impl<'a> ValidationContext<'a> {
} }
let target_ctx = ValidationContext::new( let target_ctx = ValidationContext::new(
self.registry,
target_root, target_root,
target_schema, target_schema,
self.current, self.current,
@ -445,7 +462,6 @@ impl<'a> ValidationContext<'a> {
new_overrides, new_overrides,
false, false,
self.reporter, // Propagate reporter self.reporter, // Propagate reporter
self.registry,
); );
let mut manual_ctx = target_ctx; let mut manual_ctx = target_ctx;
manual_ctx.path = self.path; manual_ctx.path = self.path;
@ -462,12 +478,7 @@ impl<'a> ValidationContext<'a> {
} }
} }
if handled { if handled { Ok(Some(res)) } else { Ok(None) }
// eprintln!("DEBUG: validate_refs returning Some with {} errors", res.errors.len());
Ok(Some(res))
} else {
Ok(None)
}
} }
fn validate_core(&self, result: &mut ValidationResult) { fn validate_core(&self, result: &mut ValidationResult) {
@ -791,10 +802,10 @@ impl<'a> ValidationContext<'a> {
false, false,
); );
let item_res = derived.validate()?; let item_res = derived.validate()?;
eprintln!( // eprintln!(
"PPROP VALIDATE: path={} key={} keys={:?}", // "PPROP VALIDATE: path={} key={} keys={:?}",
self.path, key, item_res.evaluated_keys // self.path, key, item_res.evaluated_keys
); // );
result.merge(item_res); result.merge(item_res);
result.evaluated_keys.insert(key.clone()); result.evaluated_keys.insert(key.clone());
} }
@ -1105,9 +1116,15 @@ impl<'a> ValidationContext<'a> {
} }
} }
pub struct Validator; pub struct Validator {
registry: Registry,
}
impl Validator { impl Validator {
pub fn new(registry: Registry) -> Self {
Self { registry }
}
pub fn check_type(t: &str, val: &Value) -> bool { pub fn check_type(t: &str, val: &Value) -> bool {
if let Value::String(s) = val { if let Value::String(s) = val {
if s.is_empty() { if s.is_empty() {
@ -1127,25 +1144,19 @@ impl Validator {
} }
pub fn resolve_ref<'a>( pub fn resolve_ref<'a>(
registry: &'a Registry,
root: &'a Schema, root: &'a Schema,
ref_string: &str, ref_string: &str,
scope: &str, scope: &str,
registry: &'a crate::registry::Registry,
) -> Option<(ResolvedRef<'a>, String)> { ) -> Option<(ResolvedRef<'a>, String)> {
// 0. Fast path for local fragments (e.g., "#/definitions/foo") // 0. Fast path for local fragments (e.g., "#/definitions/foo")
// This is necessary when scope is not a valid URL (e.g. "root" in tests) // This is necessary when scope is not a valid URL (e.g. "root" in tests)
if ref_string.starts_with('#') { if ref_string.starts_with('#') {
if let Some(indexrs) = &root.obj.compiled_schemas { if let Some(indexrs) = &root.obj.compiled_registry {
eprintln!("DEBUG: Resolving local fragment '{}'", ref_string); // eprintln!("DEBUG: Resolving local fragment '{}'", ref_string);
// println!("DEBUG: Resolving local fragment '{}'", ref_string);
// for k in indexrs.schemas.keys() {
// println!("DEBUG: Key in index: {}", k);
// }
if let Some(s) = indexrs.schemas.get(ref_string) { if let Some(s) = indexrs.schemas.get(ref_string) {
return Some((ResolvedRef::Local(s.as_ref()), ref_string.to_string())); return Some((ResolvedRef::Local(s.as_ref()), ref_string.to_string()));
} }
} else {
// println!("DEBUG: No compiled_schemas index found on root!");
} }
} }
@ -1154,7 +1165,7 @@ impl Validator {
if let Ok(joined) = base.join(ref_string) { if let Ok(joined) = base.join(ref_string) {
let joined_str = joined.to_string(); let joined_str = joined.to_string();
// Local // Local
if let Some(indexrs) = &root.obj.compiled_schemas { if let Some(indexrs) = &root.obj.compiled_registry {
if let Some(s) = indexrs.schemas.get(&joined_str) { if let Some(s) = indexrs.schemas.get(&joined_str) {
return Some((ResolvedRef::Local(s.as_ref()), joined_str)); return Some((ResolvedRef::Local(s.as_ref()), joined_str));
} }
@ -1164,7 +1175,7 @@ impl Validator {
if let Ok(decoded) = percent_encoding::percent_decode_str(&joined_str).decode_utf8() { if let Ok(decoded) = percent_encoding::percent_decode_str(&joined_str).decode_utf8() {
let decoded_str = decoded.to_string(); let decoded_str = decoded.to_string();
if decoded_str != joined_str { if decoded_str != joined_str {
if let Some(indexrs) = &root.obj.compiled_schemas { if let Some(indexrs) = &root.obj.compiled_registry {
if let Some(s) = indexrs.schemas.get(&decoded_str) { if let Some(s) = indexrs.schemas.get(&decoded_str) {
return Some((ResolvedRef::Local(s.as_ref()), decoded_str)); return Some((ResolvedRef::Local(s.as_ref()), decoded_str));
} }
@ -1184,7 +1195,7 @@ impl Validator {
let joined_str = format!("{}{}", scope, ref_string); let joined_str = format!("{}{}", scope, ref_string);
// Local // Local
if let Some(indexrs) = &root.obj.compiled_schemas { if let Some(indexrs) = &root.obj.compiled_registry {
if let Some(s) = indexrs.schemas.get(&joined_str) { if let Some(s) = indexrs.schemas.get(&joined_str) {
return Some((ResolvedRef::Local(s.as_ref()), joined_str)); return Some((ResolvedRef::Local(s.as_ref()), joined_str));
} }
@ -1194,7 +1205,7 @@ impl Validator {
if let Ok(decoded) = percent_encoding::percent_decode_str(&joined_str).decode_utf8() { if let Ok(decoded) = percent_encoding::percent_decode_str(&joined_str).decode_utf8() {
let decoded_str = decoded.to_string(); let decoded_str = decoded.to_string();
if decoded_str != joined_str { if decoded_str != joined_str {
if let Some(indexrs) = &root.obj.compiled_schemas { if let Some(indexrs) = &root.obj.compiled_registry {
if let Some(s) = indexrs.schemas.get(&decoded_str) { if let Some(s) = indexrs.schemas.get(&decoded_str) {
return Some((ResolvedRef::Local(s.as_ref()), decoded_str)); return Some((ResolvedRef::Local(s.as_ref()), decoded_str));
} }
@ -1203,8 +1214,11 @@ impl Validator {
} }
// Global // Global
if let Some(s) = registry.schemas.get(&joined_str) { {
return Some((ResolvedRef::Global(s.as_ref(), s.as_ref()), joined_str)); if let Some(s) = registry.schemas.get(&joined_str) {
// Clone the Arc so we can return it (extending lifetime beyond lock)
return Some((ResolvedRef::Global(s.as_ref(), s.as_ref()), joined_str));
}
} }
} }
} }
@ -1213,7 +1227,7 @@ impl Validator {
if let Ok(parsed) = url::Url::parse(ref_string) { if let Ok(parsed) = url::Url::parse(ref_string) {
let absolute = parsed.to_string(); let absolute = parsed.to_string();
// Local // Local
if let Some(indexrs) = &root.obj.compiled_schemas { if let Some(indexrs) = &root.obj.compiled_registry {
if let Some(s) = indexrs.schemas.get(&absolute) { if let Some(s) = indexrs.schemas.get(&absolute) {
return Some((ResolvedRef::Local(s.as_ref()), absolute)); return Some((ResolvedRef::Local(s.as_ref()), absolute));
} }
@ -1227,8 +1241,9 @@ impl Validator {
}; };
if let Some(compiled) = registry.schemas.get(resource_base) { if let Some(compiled) = registry.schemas.get(resource_base) {
if let Some(indexrs) = &compiled.obj.compiled_schemas { if let Some(indexrs) = &compiled.obj.compiled_registry {
if let Some(s) = indexrs.schemas.get(&absolute) { if let Some(s) = indexrs.schemas.get(&absolute) {
// Both are Arcs in compiled_registry
return Some((ResolvedRef::Global(compiled.as_ref(), s.as_ref()), absolute)); return Some((ResolvedRef::Global(compiled.as_ref(), s.as_ref()), absolute));
} }
} }
@ -1238,44 +1253,39 @@ impl Validator {
// 3. Fallback: Try as simple string key (Global Registry) // 3. Fallback: Try as simple string key (Global Registry)
// This supports legacy/JSPG-style IDs that are not valid URIs (e.g. "punc_person") // This supports legacy/JSPG-style IDs that are not valid URIs (e.g. "punc_person")
if let Some(compiled) = registry.schemas.get(ref_string) { if let Some(compiled) = registry.schemas.get(ref_string) {
eprintln!("DEBUG: Resolved Global Ref (fallback): {}", ref_string); // eprintln!("DEBUG: Resolved Global Ref (fallback): {}", ref_string);
return Some(( return Some((
ResolvedRef::Global(compiled.as_ref(), compiled.as_ref()), ResolvedRef::Global(compiled.as_ref(), compiled.as_ref()),
ref_string.to_string(), ref_string.to_string(),
)); ));
} }
eprintln!( // eprintln!(
"DEBUG: Failed to resolve ref: '{}' scope: '{}'", // "DEBUG: Failed to resolve ref: '{}' scope: '{}'",
ref_string, scope // ref_string, scope
); // );
None None
} }
pub fn validate(schema_id: &str, instance: &Value) -> crate::drop::Drop { pub fn validate(&self, schema_id: &str, instance: &Value) -> crate::drop::Drop {
let registry = REGISTRY.read().unwrap(); let registry = &self.registry;
Self::validate_with_registry(schema_id, instance, &registry) // Registry is owned, so we can access it directly. No mutex needed.
} // However, Validator owns it, so we need &self to access.
pub fn validate_with_registry(
schema_id: &str,
instance: &Value,
registry: &crate::registry::Registry,
) -> crate::drop::Drop {
if let Some(root) = registry.get(schema_id) { if let Some(root) = registry.get(schema_id) {
let root_id = root.obj.id.clone().unwrap_or_default(); let root_id = root.obj.id.clone().unwrap_or_default();
let scope = vec![root_id.clone()]; let scope = vec![root_id.clone()];
// Initial Context // Initial Context
let ctx = ValidationContext::new( let ctx = ValidationContext::new(
registry,
&root, &root,
&root, &root,
instance, instance,
&scope, &scope,
HashSet::new(), HashSet::new(),
false, false,
false, // reporter = false (Default) false, // reporter = false (Default)
registry, // Use the passed registry
); );
match ctx.validate() { match ctx.validate() {