chore: JSPG Engine tuple decoupling and core routing optimizations

This commit is contained in:
2026-04-13 22:41:32 -04:00
parent 665a821bf9
commit 0017c598e1
57 changed files with 5510 additions and 5166 deletions

View File

@ -30,77 +30,34 @@ impl<'a> ValidationContext<'a> {
if self.schema.family.is_some() {
if let Some(options) = self.schema.compiled_options.get() {
if let Some(disc) = self.schema.compiled_discriminator.get() {
return self.execute_polymorph(disc, options, result);
}
return self.execute_polymorph(options, result);
} else {
result.errors.push(ValidationError {
code: "UNCOMPILED_FAMILY".to_string(),
message: "Encountered family block that could not be mapped to deterministic options during db schema compilation.".to_string(),
path: self.path.to_string(),
});
return Ok(false);
}
}
Ok(true)
}
pub(crate) fn validate_one_of(
&self,
result: &mut ValidationResult,
) -> Result<bool, ValidationError> {
if let Some(one_of) = &self.schema.one_of {
if self.schema.one_of.is_some() {
if let Some(options) = self.schema.compiled_options.get() {
if let Some(disc) = self.schema.compiled_discriminator.get() {
return self.execute_polymorph(disc, options, result);
}
}
// Native Draft2020-12 oneOf Evaluation Fallback
let mut valid_count = 0;
let mut final_successful_result = None;
let mut failed_candidates = Vec::new();
for child_schema in one_of {
let derived = self.derive_for_schema(child_schema, false);
if let Ok(sub_res) = derived.validate_scoped() {
if sub_res.is_valid() {
valid_count += 1;
final_successful_result = Some(sub_res.clone());
} else {
failed_candidates.push(sub_res);
}
}
}
if valid_count == 1 {
if let Some(successful_res) = final_successful_result {
result.merge(successful_res);
}
return Ok(true);
} else if valid_count == 0 {
result.errors.push(ValidationError {
code: "NO_ONEOF_MATCH".to_string(),
message: "Payload matches none of the required candidate sub-schemas natively".to_string(),
path: self.path.to_string(),
});
if let Some(first) = failed_candidates.first() {
let mut shared_errors = first.errors.clone();
for sub_res in failed_candidates.iter().skip(1) {
shared_errors.retain(|e1| {
sub_res.errors.iter().any(|e2| e1.code == e2.code && e1.path == e2.path)
});
}
for e in shared_errors {
if !result.errors.iter().any(|existing| existing.code == e.code && existing.path == e.path) {
result.errors.push(e);
}
}
}
return Ok(false);
return self.execute_polymorph(options, result);
} else {
result.errors.push(ValidationError {
code: "AMBIGUOUS_POLYMORPHIC_MATCH".to_string(),
message: "Matches multiple polymorphic candidates inextricably natively".to_string(),
result.errors.push(ValidationError {
code: "UNCOMPILED_ONEOF".to_string(),
message: "Encountered oneOf block that could not be mapped to deterministic compiled options natively.".to_string(),
path: self.path.to_string(),
});
return Ok(false);
return Ok(false);
}
}
Ok(true)
@ -108,46 +65,115 @@ impl<'a> ValidationContext<'a> {
pub(crate) fn execute_polymorph(
&self,
disc: &str,
options: &std::collections::BTreeMap<String, String>,
options: &std::collections::BTreeMap<String, (Option<usize>, Option<String>)>,
result: &mut ValidationResult,
) -> Result<bool, ValidationError> {
// 1. O(1) Fast-Path Router & Extractor
let instance_val = self.instance.as_object().and_then(|o| o.get(disc)).and_then(|t| t.as_str());
let instance_val = if let Some(disc) = self.schema.compiled_discriminator.get() {
let val = self
.instance
.as_object()
.and_then(|o| o.get(disc))
.and_then(|t| t.as_str());
if val.is_some() {
result.evaluated_keys.insert(disc.to_string());
}
val.map(|s| s.to_string())
} else {
match self.instance {
serde_json::Value::Null => Some("null".to_string()),
serde_json::Value::Bool(_) => Some("boolean".to_string()),
serde_json::Value::Number(n) => {
if n.is_i64() || n.is_u64() {
Some("integer".to_string())
} else {
Some("number".to_string())
}
}
serde_json::Value::String(_) => Some("string".to_string()),
serde_json::Value::Array(_) => Some("array".to_string()),
serde_json::Value::Object(_) => Some("object".to_string()),
}
};
if let Some(val) = instance_val {
result.evaluated_keys.insert(disc.to_string());
if let Some(target_id) = options.get(val) {
if let Some((idx_opt, target_id_opt)) = options.get(&val) {
if let Some(target_id) = target_id_opt {
if let Some(target_schema) = self.db.schemas.get(target_id) {
let derived = self.derive_for_schema(target_schema.as_ref(), false);
let sub_res = derived.validate()?;
let is_valid = sub_res.is_valid();
result.merge(sub_res);
return Ok(is_valid);
let derived = self.derive_for_schema(target_schema.as_ref(), false);
let sub_res = derived.validate()?;
let is_valid = sub_res.is_valid();
result.merge(sub_res);
return Ok(is_valid);
} else {
result.errors.push(ValidationError {
code: "MISSING_COMPILED_SCHEMA".to_string(),
message: format!("Polymorphic router target '{}' does not exist in the database schemas map", target_id),
path: self.path.to_string(),
});
return Ok(false);
result.errors.push(ValidationError {
code: "MISSING_COMPILED_SCHEMA".to_string(),
message: format!(
"Polymorphic router target '{}' does not exist in the database schemas map",
target_id
),
path: self.path.to_string(),
});
return Ok(false);
}
} else {
result.errors.push(ValidationError {
code: if self.schema.family.is_some() { "NO_FAMILY_MATCH".to_string() } else { "NO_ONEOF_MATCH".to_string() },
message: format!("Payload provided discriminator {}='{}' which matches none of the required candidate sub-schemas", disc, val),
path: self.path.to_string(),
});
} else if let Some(idx) = idx_opt {
if let Some(target_schema) = self
.schema
.one_of
.as_ref()
.and_then(|options| options.get(*idx))
{
let derived = self.derive_for_schema(target_schema.as_ref(), false);
let sub_res = derived.validate()?;
let is_valid = sub_res.is_valid();
result.merge(sub_res);
return Ok(is_valid);
} else {
result.errors.push(ValidationError {
code: "MISSING_COMPILED_SCHEMA".to_string(),
message: format!(
"Polymorphic index target '{}' does not exist in the local oneOf array",
idx
),
path: self.path.to_string(),
});
return Ok(false);
}
} else {
return Ok(false);
}
} else {
}
} else {
let disc_msg = if let Some(d) = self.schema.compiled_discriminator.get() {
format!("discriminator {}='{}'", d, val)
} else {
format!("structural JSON base primitive '{}'", val)
};
result.errors.push(ValidationError {
code: "MISSING_TYPE".to_string(),
message: format!("Missing '{}' discriminator. Unable to resolve polymorphic boundaries", disc),
code: if self.schema.family.is_some() {
"NO_FAMILY_MATCH".to_string()
} else {
"NO_ONEOF_MATCH".to_string()
},
message: format!(
"Payload matched no candidate boundaries based on its {}",
disc_msg
),
path: self.path.to_string(),
});
return Ok(false);
}
} else {
if let Some(d) = self.schema.compiled_discriminator.get() {
result.errors.push(ValidationError {
code: "MISSING_TYPE".to_string(),
message: format!(
"Missing explicit '{}' discriminator. Unable to resolve polymorphic boundaries",
d
),
path: self.path.to_string(),
});
}
return Ok(false);
}
}
@ -179,14 +205,16 @@ impl<'a> ValidationContext<'a> {
}
}
Some(crate::database::object::SchemaTypeOrArray::Multiple(arr)) => {
if arr.contains(&payload_primitive.to_string()) || (payload_primitive == "integer" && arr.contains(&"number".to_string())) {
// It natively matched a primitive in the array options, skip forcing custom proxy fallback
if arr.contains(&payload_primitive.to_string())
|| (payload_primitive == "integer" && arr.contains(&"number".to_string()))
{
// It natively matched a primitive in the array options, skip forcing custom proxy fallback
} else {
for t in arr {
if !crate::database::object::is_primitive_type(t) {
custom_types.push(t.clone());
}
}
for t in arr {
if !crate::database::object::is_primitive_type(t) {
custom_types.push(t.clone());
}
}
}
}
None => {}