removed schema realms, fixed fixture generations, added dynamic type resolution for validation based on type and kind discriminators for filters
This commit is contained in:
@ -15,6 +15,7 @@ pub struct ValidationContext<'a> {
|
||||
pub extensible: bool,
|
||||
pub reporter: bool,
|
||||
pub overrides: HashSet<String>,
|
||||
pub parent: Option<&'a serde_json::Value>,
|
||||
}
|
||||
|
||||
impl<'a> ValidationContext<'a> {
|
||||
@ -38,6 +39,7 @@ impl<'a> ValidationContext<'a> {
|
||||
extensible: effective_extensible,
|
||||
reporter,
|
||||
overrides,
|
||||
parent: None,
|
||||
}
|
||||
}
|
||||
|
||||
@ -57,6 +59,7 @@ impl<'a> ValidationContext<'a> {
|
||||
overrides: HashSet<String>,
|
||||
extensible: bool,
|
||||
reporter: bool,
|
||||
parent_instance: Option<&'a serde_json::Value>,
|
||||
) -> Self {
|
||||
let effective_extensible = schema.extensible.unwrap_or(extensible);
|
||||
|
||||
@ -70,6 +73,7 @@ impl<'a> ValidationContext<'a> {
|
||||
extensible: effective_extensible,
|
||||
reporter,
|
||||
overrides,
|
||||
parent: parent_instance,
|
||||
}
|
||||
}
|
||||
|
||||
@ -81,6 +85,7 @@ impl<'a> ValidationContext<'a> {
|
||||
HashSet::new(),
|
||||
self.extensible,
|
||||
reporter,
|
||||
self.parent,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@ -10,7 +10,6 @@ pub use error::ValidationError;
|
||||
pub use result::ValidationResult;
|
||||
|
||||
use crate::database::Database;
|
||||
use crate::database::realm::SchemaRealm;
|
||||
use crate::validator::rules::util::is_integer;
|
||||
use serde_json::Value;
|
||||
use std::sync::Arc;
|
||||
@ -43,11 +42,7 @@ impl Validator {
|
||||
}
|
||||
|
||||
pub fn validate(&self, schema_id: &str, instance: &Value) -> crate::drop::Drop {
|
||||
let schema_opt = if schema_id.ends_with(".request") || schema_id.ends_with(".response") {
|
||||
self.db.get_scoped_schema(SchemaRealm::Punc, schema_id)
|
||||
} else {
|
||||
self.db.get_scoped_schema(SchemaRealm::Type, schema_id)
|
||||
};
|
||||
let schema_opt = self.db.schemas.get(schema_id);
|
||||
|
||||
if let Some(schema) = schema_opt {
|
||||
let ctx = ValidationContext::new(
|
||||
|
||||
@ -57,6 +57,7 @@ impl<'a> ValidationContext<'a> {
|
||||
HashSet::new(),
|
||||
self.extensible,
|
||||
false,
|
||||
Some(self.instance),
|
||||
);
|
||||
|
||||
let check = derived.validate()?;
|
||||
@ -108,6 +109,7 @@ impl<'a> ValidationContext<'a> {
|
||||
HashSet::new(),
|
||||
self.extensible,
|
||||
false,
|
||||
Some(self.instance),
|
||||
);
|
||||
let item_res = derived.validate()?;
|
||||
result.merge(item_res);
|
||||
@ -137,6 +139,7 @@ impl<'a> ValidationContext<'a> {
|
||||
HashSet::new(),
|
||||
self.extensible,
|
||||
false,
|
||||
Some(self.instance),
|
||||
);
|
||||
let item_res = derived.validate()?;
|
||||
result.merge(item_res);
|
||||
|
||||
@ -12,6 +12,7 @@ pub mod numeric;
|
||||
pub mod object;
|
||||
pub mod polymorphism;
|
||||
pub mod string;
|
||||
pub mod r#type;
|
||||
pub mod util;
|
||||
|
||||
impl<'a> ValidationContext<'a> {
|
||||
@ -28,7 +29,7 @@ impl<'a> ValidationContext<'a> {
|
||||
if !self.validate_family(&mut result)? {
|
||||
return Ok(result);
|
||||
}
|
||||
if !self.validate_type_inheritance(&mut result)? {
|
||||
if !self.validate_type(&mut result)? {
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
|
||||
@ -191,6 +191,7 @@ impl<'a> ValidationContext<'a> {
|
||||
HashSet::new(),
|
||||
next_extensible,
|
||||
false,
|
||||
Some(self.instance),
|
||||
);
|
||||
let item_res = derived.validate()?;
|
||||
|
||||
@ -220,6 +221,7 @@ impl<'a> ValidationContext<'a> {
|
||||
HashSet::new(),
|
||||
next_extensible,
|
||||
false,
|
||||
Some(self.instance),
|
||||
);
|
||||
let item_res = derived.validate()?;
|
||||
result.merge(item_res);
|
||||
@ -265,6 +267,7 @@ impl<'a> ValidationContext<'a> {
|
||||
HashSet::new(),
|
||||
next_extensible,
|
||||
false,
|
||||
Some(self.instance),
|
||||
);
|
||||
let item_res = derived.validate()?;
|
||||
result.merge(item_res);
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
use crate::validator::context::ValidationContext;
|
||||
use crate::validator::error::ValidationError;
|
||||
use crate::validator::result::ValidationResult;
|
||||
use crate::database::realm::SchemaRealm;
|
||||
|
||||
impl<'a> ValidationContext<'a> {
|
||||
pub(crate) fn validate_family(
|
||||
@ -100,8 +99,8 @@ impl<'a> ValidationContext<'a> {
|
||||
if let Some(val) = instance_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.get_scoped_schema(SchemaRealm::Type, target_id) {
|
||||
let derived = self.derive_for_schema(&target_schema, false);
|
||||
if let Some(target_schema) = self.db.schemas.get(target_id) {
|
||||
let derived = self.derive_for_schema(target_schema, false);
|
||||
let sub_res = derived.validate()?;
|
||||
let is_valid = sub_res.is_valid();
|
||||
result.merge(sub_res);
|
||||
@ -177,78 +176,4 @@ impl<'a> ValidationContext<'a> {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn validate_type_inheritance(
|
||||
&self,
|
||||
result: &mut ValidationResult,
|
||||
) -> Result<bool, ValidationError> {
|
||||
// Core inheritance logic replaces legacy routing
|
||||
let payload_primitive = match self.instance {
|
||||
serde_json::Value::Null => "null",
|
||||
serde_json::Value::Bool(_) => "boolean",
|
||||
serde_json::Value::Number(n) => {
|
||||
if n.is_i64() || n.is_u64() {
|
||||
"integer"
|
||||
} else {
|
||||
"number"
|
||||
}
|
||||
}
|
||||
serde_json::Value::String(_) => "string",
|
||||
serde_json::Value::Array(_) => "array",
|
||||
serde_json::Value::Object(_) => "object",
|
||||
};
|
||||
|
||||
let mut custom_types = Vec::new();
|
||||
match &self.schema.type_ {
|
||||
Some(crate::database::object::SchemaTypeOrArray::Single(t)) => {
|
||||
if !crate::database::object::is_primitive_type(t) {
|
||||
custom_types.push(t.clone());
|
||||
}
|
||||
}
|
||||
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
|
||||
} else {
|
||||
for t in arr {
|
||||
if !crate::database::object::is_primitive_type(t) {
|
||||
custom_types.push(t.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
for t in custom_types {
|
||||
if let Some(global_schema) = self.db.get_scoped_schema(SchemaRealm::Type, &t) {
|
||||
let mut new_overrides = self.overrides.clone();
|
||||
if let Some(props) = &self.schema.properties {
|
||||
new_overrides.extend(props.keys().map(|k| k.to_string()));
|
||||
}
|
||||
|
||||
let mut shadow = self.derive(
|
||||
&global_schema,
|
||||
self.instance,
|
||||
&self.path,
|
||||
new_overrides,
|
||||
self.extensible,
|
||||
true, // Reporter mode
|
||||
);
|
||||
shadow.root = &global_schema;
|
||||
result.merge(shadow.validate()?);
|
||||
} else {
|
||||
result.errors.push(ValidationError {
|
||||
code: "INHERITANCE_RESOLUTION_FAILED".to_string(),
|
||||
message: format!(
|
||||
"Inherited entity pointer '{}' was not found in schema registry",
|
||||
t
|
||||
),
|
||||
path: self.path.to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
138
src/validator/rules/type.rs
Normal file
138
src/validator/rules/type.rs
Normal file
@ -0,0 +1,138 @@
|
||||
use crate::validator::context::ValidationContext;
|
||||
use crate::validator::error::ValidationError;
|
||||
use crate::validator::result::ValidationResult;
|
||||
|
||||
impl<'a> ValidationContext<'a> {
|
||||
pub(crate) fn validate_type(
|
||||
&self,
|
||||
result: &mut ValidationResult,
|
||||
) -> Result<bool, ValidationError> {
|
||||
let payload_primitive = match self.instance {
|
||||
serde_json::Value::Null => "null",
|
||||
serde_json::Value::Bool(_) => "boolean",
|
||||
serde_json::Value::Number(n) => {
|
||||
if n.is_i64() || n.is_u64() {
|
||||
"integer"
|
||||
} else {
|
||||
"number"
|
||||
}
|
||||
}
|
||||
serde_json::Value::String(_) => "string",
|
||||
serde_json::Value::Array(_) => "array",
|
||||
serde_json::Value::Object(_) => "object",
|
||||
};
|
||||
|
||||
let mut custom_types = Vec::new();
|
||||
match &self.schema.type_ {
|
||||
Some(crate::database::object::SchemaTypeOrArray::Single(t)) => {
|
||||
if !crate::database::object::is_primitive_type(t) {
|
||||
custom_types.push(t.clone());
|
||||
}
|
||||
}
|
||||
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
|
||||
} else {
|
||||
for t in arr {
|
||||
if !crate::database::object::is_primitive_type(t) {
|
||||
custom_types.push(t.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
for t in custom_types {
|
||||
let mut target_id = t.clone();
|
||||
|
||||
// 1. DYNAMIC TYPE (Composition)
|
||||
if t.starts_with('$') {
|
||||
let parts: Vec<&str> = t.split('.').collect();
|
||||
let var_name = &parts[0][1..]; // Remove the $ prefix
|
||||
let suffix = if parts.len() > 1 {
|
||||
format!(".{}", parts[1..].join("."))
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
let mut resolved = false;
|
||||
if let Some(parent) = self.parent {
|
||||
if let Some(obj) = parent.as_object() {
|
||||
if let Some(val) = obj.get(var_name) {
|
||||
if let Some(str_val) = val.as_str() {
|
||||
target_id = format!("{}{}", str_val, suffix);
|
||||
resolved = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !resolved {
|
||||
result.errors.push(ValidationError {
|
||||
code: "DYNAMIC_TYPE_RESOLUTION_FAILED".to_string(),
|
||||
message: format!(
|
||||
"Dynamic type pointer '{}' could not resolve discriminator property '{}' on parent instance",
|
||||
t, var_name
|
||||
),
|
||||
path: self.path.to_string(),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Fetch and apply
|
||||
if let Some(global_schema) = self.db.schemas.get(&target_id) {
|
||||
let mut new_overrides = self.overrides.clone();
|
||||
if let Some(props) = &self.schema.properties {
|
||||
new_overrides.extend(props.keys().map(|k| k.to_string()));
|
||||
}
|
||||
|
||||
let mut shadow = self.derive(
|
||||
&global_schema,
|
||||
self.instance,
|
||||
&self.path,
|
||||
new_overrides,
|
||||
self.extensible,
|
||||
true, // Reporter mode
|
||||
self.parent,
|
||||
);
|
||||
shadow.root = &global_schema;
|
||||
result.merge(shadow.validate()?);
|
||||
} else {
|
||||
// 3. Error handling pathways
|
||||
if t.starts_with('$') {
|
||||
result.errors.push(ValidationError {
|
||||
code: "DYNAMIC_TYPE_RESOLUTION_FAILED".to_string(),
|
||||
message: format!(
|
||||
"Resolved dynamic type pointer '{}' was not found in schema registry",
|
||||
target_id
|
||||
),
|
||||
path: self.path.to_string(),
|
||||
});
|
||||
} else if self.schema.is_proxy() {
|
||||
result.errors.push(ValidationError {
|
||||
code: "PROXY_TYPE_RESOLUTION_FAILED".to_string(),
|
||||
message: format!(
|
||||
"Composed proxy entity pointer '{}' was not found in schema registry",
|
||||
target_id
|
||||
),
|
||||
path: self.path.to_string(),
|
||||
});
|
||||
} else {
|
||||
result.errors.push(ValidationError {
|
||||
code: "INHERITANCE_RESOLUTION_FAILED".to_string(),
|
||||
message: format!(
|
||||
"Inherited entity pointer '{}' was not found in schema registry",
|
||||
target_id
|
||||
),
|
||||
path: self.path.to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user