use serde_json::{Map, Value};
use crate::{RepairError, Repaired, Rewrite, RewriteKind};
const PATH_KEYS: &[&str] = &["path", "file", "file_path", "filepath", "path_name"];
pub fn normalize(value: &Value, schema: &Value) -> Result<Repaired, RepairError> {
let (next, rewrites) = normalize_child(value, schema)?;
Ok(Repaired {
value: next,
rewrites,
})
}
fn normalize_child(value: &Value, schema: &Value) -> Result<(Value, Vec<Rewrite>), RepairError> {
let (next, rewrites) = normalize_known(value, schema)?;
if next != *value || !rewrites.is_empty() {
if value.as_str().is_some() && violates_length(schema, value) {
return Err(RepairError::Ambiguous(
"coerced string is outside the schema length".into(),
));
}
if value.as_str().is_some() && unchecked_string_keyword(schema) {
return Err(RepairError::Ambiguous(
"coerced string is outside an unchecked string keyword".into(),
));
}
if violates_not(schema, &next) {
return Err(RepairError::Ambiguous(
"coerced value is excluded by not".into(),
));
}
if violates_bounds(schema, &next) {
return Err(RepairError::Ambiguous(
"coerced value is outside the schema bounds".into(),
));
}
if violates_multiple(schema, &next) {
return Err(RepairError::Ambiguous(
"coerced value is not a multiple of the schema".into(),
));
}
if violates_unique(schema, &next) {
return Err(RepairError::Ambiguous(
"coerced value repeats an array item".into(),
));
}
if violates_contains(schema, &next) {
return Err(RepairError::Ambiguous(
"coerced value misses contains".into(),
));
}
if outside_const_or_enum(schema, &next) {
return Err(RepairError::Ambiguous(
"coerced value is not a const or enum member".into(),
));
}
if violates_size(schema, &next) {
return Err(RepairError::Ambiguous(
"coerced value is outside the schema size".into(),
));
}
if violates_dependent(schema, &next) {
return Err(RepairError::Ambiguous(
"coerced value misses a dependent property".into(),
));
}
if violates_length(schema, &next) {
return Err(RepairError::Ambiguous(
"coerced value is outside the schema length".into(),
));
}
if let Some(reason) = property_names_refusal(schema, &next) {
return Err(RepairError::Ambiguous(reason));
}
}
Ok((next, rewrites))
}
fn normalize_known(value: &Value, schema: &Value) -> Result<(Value, Vec<Rewrite>), RepairError> {
if let Some((next, rewrites)) = if_then_else(value, schema) {
return Ok((next, rewrites));
}
if let Some((next, rewrites)) = all_of(value, schema) {
return Ok((next, rewrites));
}
if let Some((next, rewrites)) = exclusive_branch(value, schema) {
return Ok((next, rewrites));
}
if let Some((next, rewrites)) = deep_constrained(value, schema) {
return Ok((next, rewrites));
}
let mut rewrites = Vec::new();
let coerced = coerce(value, schema, &mut rewrites);
if let Some(obj) = coerced.as_object() {
let props = object_properties(schema);
let mut next = coerced.clone();
let mut saw_object_schema = false;
if props.is_some()
|| denies_unknown(schema)
|| additional_properties_schema(schema).is_some()
{
let empty = Map::new();
let props = props.unwrap_or(&empty);
let (normalized, more) = normalize_object(obj, schema, props)?;
rewrites.extend(more);
next = normalized;
saw_object_schema = true;
}
if let Some((updated, extra)) = next
.as_object()
.and_then(|map| apply_dependent_schemas(map, schema))
{
rewrites.extend(extra);
return Ok((updated, rewrites));
}
if saw_object_schema {
return Ok((next, rewrites));
}
}
if describes_array(schema)
&& let Some(arr) = coerced.as_array()
{
let (next, more) = normalize_array(arr, schema)?;
rewrites.extend(more);
return Ok((next, rewrites));
}
Ok((coerced, rewrites))
}
#[derive(Clone, Copy)]
enum ArrayTail<'a> {
Keep,
Forbid,
Schema(&'a Value),
}
fn apply_dependent_schemas(
obj: &Map<String, Value>,
schema: &Value,
) -> Option<(Value, Vec<Rewrite>)> {
let active = dependent_object_schemas(schema)
.into_iter()
.filter(|(name, _)| obj.contains_key(*name))
.map(|(_, sub)| sub)
.collect::<Vec<_>>();
if active.is_empty() {
return None;
}
let mut current = Value::Object(obj.clone());
let mut rewrites = Vec::new();
for branch in &active {
let (next, more) = normalize_child(¤t, branch).ok()?;
if !branch_accepts(branch, &next) {
return None;
}
current = next;
rewrites.extend(more);
}
for branch in &active {
let (next, _) = normalize_child(¤t, branch).ok()?;
if next != current || !branch_accepts(branch, &next) {
return None;
}
}
if current == Value::Object(obj.clone()) && rewrites.is_empty() {
return None;
}
Some((current, rewrites))
}
fn dependent_object_schemas(schema: &Value) -> Vec<(&str, &Value)> {
let schema = if schema.get("properties").is_none() {
schema.get("parameters").unwrap_or(schema)
} else {
schema
};
let mut out = Vec::new();
for keyword in ["dependentSchemas", "dependencies"] {
let Some(map) = schema.get(keyword).and_then(Value::as_object) else {
continue;
};
for (name, sub) in map {
if sub.is_object() {
out.push((name.as_str(), sub));
}
}
}
out
}
fn if_then_else(value: &Value, schema: &Value) -> Option<(Value, Vec<Rewrite>)> {
if schema.get("allOf").is_some()
|| schema.get("oneOf").is_some()
|| schema.get("anyOf").is_some()
{
return None;
}
let if_schema = schema.get("if")?;
let then_schema = schema.get("then");
let else_schema = schema.get("else");
if then_schema.is_none() && else_schema.is_none() {
return None;
}
if value.is_string()
&& (unchecked_string_keyword(if_schema)
|| then_schema.is_some_and(unchecked_string_keyword)
|| else_schema.is_some_and(unchecked_string_keyword))
{
return None;
}
let if_hit = match normalize_child(value, if_schema) {
Ok((probed, probe_rewrites)) if branch_accepts(if_schema, &probed) => {
Some((probed, probe_rewrites))
}
_ => None,
};
if let Some((probed, probe_rewrites)) = if_hit {
let Some(then_schema) = then_schema else {
if probed == *value && probe_rewrites.is_empty() {
return None;
}
return Some((probed, probe_rewrites));
};
let (next, more) = normalize_child(&probed, then_schema).ok()?;
if !branch_accepts(if_schema, &next) || !branch_accepts(then_schema, &next) {
return None;
}
let mut rewrites = probe_rewrites;
rewrites.extend(more);
if next == *value && rewrites.is_empty() {
return None;
}
return Some((next, rewrites));
}
let else_schema = else_schema?;
let (next, rewrites) = normalize_child(value, else_schema).ok()?;
if !branch_accepts(else_schema, &next) {
return None;
}
if next == *value && rewrites.is_empty() {
return None;
}
Some((next, rewrites))
}
fn all_of(value: &Value, schema: &Value) -> Option<(Value, Vec<Rewrite>)> {
let branches = single_applicator(schema, "allOf")?;
if branches.is_empty() {
return None;
}
if value.is_string() && branches.iter().any(unchecked_string_keyword) {
return None;
}
let mut current = value.clone();
let mut rewrites = Vec::new();
for branch in branches {
let (next, more) = normalize_child(¤t, branch).ok()?;
if !branch_accepts(branch, &next) {
return None;
}
current = next;
rewrites.extend(more);
}
for branch in branches {
let (next, _) = normalize_child(¤t, branch).ok()?;
if next != current || !branch_accepts(branch, &next) {
return None;
}
}
if current == *value && rewrites.is_empty() {
return None;
}
Some((current, rewrites))
}
fn single_applicator<'a>(schema: &'a Value, key: &str) -> Option<&'a Vec<Value>> {
let present = ["allOf", "oneOf", "anyOf"]
.into_iter()
.filter(|name| schema.get(*name).is_some())
.count();
if present != 1 {
return None;
}
schema.get(key).and_then(Value::as_array)
}
fn exclusive_branch(value: &Value, schema: &Value) -> Option<(Value, Vec<Rewrite>)> {
let branches =
single_applicator(schema, "oneOf").or_else(|| single_applicator(schema, "anyOf"))?;
if value.is_string() && branches.iter().any(unchecked_string_keyword) {
return None;
}
let mut matched = Vec::new();
for branch in branches {
let Ok((next, rewrites)) = normalize_child(value, branch) else {
continue;
};
if branch_accepts(branch, &next) && branch_accepts(schema, &next) {
matched.push((next, rewrites));
}
}
if matched.len() == 1 {
matched.pop()
} else {
None
}
}
fn violates_not(schema: &Value, value: &Value) -> bool {
let Some(not_schema) = schema.get("not") else {
return false;
};
known_constraint(not_schema) && branch_accepts(not_schema, value)
}
fn violates_unique(schema: &Value, value: &Value) -> bool {
if schema.get("uniqueItems") != Some(&Value::Bool(true)) {
return false;
}
let Some(items) = value.as_array() else {
return false;
};
items
.iter()
.enumerate()
.any(|(index, item)| items[..index].contains(item))
}
fn violates_multiple(schema: &Value, value: &Value) -> bool {
let Some(multiple) = schema.get("multipleOf").and_then(Value::as_number) else {
return false;
};
let Some(factor) = multiple.as_f64() else {
return false;
};
if !factor.is_finite() || factor <= 0.0 {
return false;
}
let Some(number) = value.as_number() else {
return false;
};
let (Some(left), Some(right)) = (
parse_decimal(&number.to_string()),
parse_decimal(&multiple.to_string()),
) else {
return false;
};
decimal_divides(&left, &right) == Some(false)
}
fn decimal_divides(value: &Decimal, multiple: &Decimal) -> Option<bool> {
if multiple.coef == 0 {
return Some(false);
}
if value.coef == 0 {
return Some(true);
}
let exp = value.exp10.checked_sub(multiple.exp10)?;
if exp >= 0 {
let factor = pow10(exp)?;
let scaled = value.coef.checked_mul(factor)?;
Some(scaled.is_multiple_of(multiple.coef))
} else {
let factor = pow10(-exp)?;
let divisor = multiple.coef.checked_mul(factor)?;
if divisor == 0 {
return Some(false);
}
Some(value.coef.is_multiple_of(divisor))
}
}
fn pow10(exp: i32) -> Option<u128> {
if !(0..=38).contains(&exp) {
return None;
}
let mut factor: u128 = 1;
for _ in 0..exp {
factor = factor.checked_mul(10)?;
}
Some(factor)
}
fn violates_bounds(schema: &Value, value: &Value) -> bool {
let Some(number) = finite_bound(value) else {
return false;
};
below_minimum(schema, number) || above_maximum(schema, number)
}
fn below_minimum(schema: &Value, number: f64) -> bool {
if let Some(limit) = schema.get("exclusiveMinimum").and_then(finite_bound)
&& number <= limit
{
return true;
}
let Some(limit) = schema.get("minimum").and_then(finite_bound) else {
return false;
};
if matches!(schema.get("exclusiveMinimum"), Some(Value::Bool(true))) {
number <= limit
} else {
number < limit
}
}
fn above_maximum(schema: &Value, number: f64) -> bool {
if let Some(limit) = schema.get("exclusiveMaximum").and_then(finite_bound)
&& number >= limit
{
return true;
}
let Some(limit) = schema.get("maximum").and_then(finite_bound) else {
return false;
};
if matches!(schema.get("exclusiveMaximum"), Some(Value::Bool(true))) {
number >= limit
} else {
number > limit
}
}
fn finite_bound(value: &Value) -> Option<f64> {
let number = value.as_f64()?;
if !number.is_finite() || !exact_json_number(value) {
return None;
}
Some(number)
}
fn exact_json_number(value: &Value) -> bool {
let Some(number) = value.as_f64() else {
return false;
};
if let Some(int) = value.as_i64() {
return int as f64 == number && number as i64 == int;
}
if let Some(int) = value.as_u64() {
return int as f64 == number && number as u64 == int;
}
true
}
fn known_constraint(schema: &Value) -> bool {
schema.get("type").is_some()
|| schema.get("const").is_some()
|| schema.get("enum").is_some()
|| schema.get("properties").is_some()
|| schema.get("required").is_some()
|| schema.get("dependentRequired").is_some()
|| schema.get("minLength").is_some()
|| schema.get("maxLength").is_some()
|| schema.get("propertyNames").is_some()
|| schema.get("items").is_some()
|| schema.get("prefixItems").is_some()
}
fn branch_accepts(schema: &Value, value: &Value) -> bool {
if value.is_string() && unchecked_string_keyword(schema) {
return false;
}
constraints_hold(schema, value)
&& properties_match(schema, value)
&& elements_match(schema, value)
&& required_holds(schema, value)
&& !violates_not(schema, value)
&& !violates_bounds(schema, value)
&& !violates_multiple(schema, value)
&& !violates_unique(schema, value)
&& !violates_contains(schema, value)
&& !violates_size(schema, value)
&& !violates_dependent(schema, value)
&& !violates_length(schema, value)
&& !violates_property_names(schema, value)
}
fn unchecked_string_keyword(schema: &Value) -> bool {
let Some(map) = schema.as_object() else {
return false;
};
map.iter().any(|(key, child)| {
matches!(
key.as_str(),
"pattern" | "format" | "contentEncoding" | "contentMediaType" | "contentSchema"
) || (key == "not" && unchecked_string_keyword(child))
})
}
fn violates_length(schema: &Value, value: &Value) -> bool {
let Some(text) = value.as_str() else {
return false;
};
let len = u64::try_from(text.chars().count()).unwrap_or(u64::MAX);
if let Some(max) = integer_limit(schema, "maxLength")
&& len > max
{
return true;
}
if let Some(min) = integer_limit(schema, "minLength")
&& len < min
{
return true;
}
false
}
fn violates_property_names(schema: &Value, value: &Value) -> bool {
property_names_refusal(schema, value).is_some()
}
fn property_names_refusal(schema: &Value, value: &Value) -> Option<String> {
let obj = value.as_object()?;
let schema = if schema.get("properties").is_none() {
schema.get("parameters").unwrap_or(schema)
} else {
schema
};
let names = schema
.get("propertyNames")
.filter(|schema| schema.is_object())?;
if obj.is_empty() {
return None;
}
if let Some(keyword) = unevaluable_name_keyword(names) {
return Some(format!("propertyNames {keyword} was not evaluated"));
}
let outside = obj
.keys()
.any(|key| !branch_accepts(names, &Value::String(key.clone())));
if outside {
Some("coerced value has a property name outside the schema".to_string())
} else {
None
}
}
fn unevaluable_name_keyword(schema: &Value) -> Option<&str> {
let map = schema.as_object()?;
for (key, child) in map {
let checked = matches!(
key.as_str(),
"type"
| "const"
| "enum"
| "minLength"
| "maxLength"
| "not"
| "minimum"
| "maximum"
| "exclusiveMinimum"
| "exclusiveMaximum"
| "multipleOf"
);
if !checked {
return Some(key.as_str());
}
if key == "not"
&& let Some(inner) = unevaluable_name_keyword(child)
{
return Some(inner);
}
if key == "not" && !child.is_object() {
return Some("not");
}
}
None
}
fn violates_dependent(schema: &Value, value: &Value) -> bool {
let Some(obj) = value.as_object() else {
return false;
};
let schema = if schema.get("properties").is_none() {
schema.get("parameters").unwrap_or(schema)
} else {
schema
};
dependent_names_missing(schema, "dependentRequired", obj)
|| dependent_names_missing(schema, "dependencies", obj)
}
fn dependent_names_missing(schema: &Value, keyword: &str, obj: &Map<String, Value>) -> bool {
let Some(map) = schema.get(keyword).and_then(Value::as_object) else {
return false;
};
map.iter().any(|(name, need)| {
obj.contains_key(name)
&& need.as_array().is_some_and(|names| {
names
.iter()
.filter_map(Value::as_str)
.any(|needed| !obj.contains_key(needed))
})
})
}
fn violates_size(schema: &Value, value: &Value) -> bool {
let count = if let Some(obj) = value.as_object() {
let count = u64::try_from(obj.len()).unwrap_or(u64::MAX);
if let Some(max) = integer_limit(schema, "maxProperties")
&& count > max
{
return true;
}
if let Some(min) = integer_limit(schema, "minProperties")
&& count < min
{
return true;
}
return false;
} else if let Some(items) = value.as_array() {
u64::try_from(items.len()).unwrap_or(u64::MAX)
} else {
return false;
};
if let Some(max) = integer_limit(schema, "maxItems")
&& count > max
{
return true;
}
if let Some(min) = integer_limit(schema, "minItems")
&& count < min
{
return true;
}
false
}
fn integer_limit(schema: &Value, key: &str) -> Option<u64> {
schema.get(key).and_then(Value::as_u64)
}
fn violates_contains(schema: &Value, value: &Value) -> bool {
let Some(contains) = schema.get("contains").filter(|schema| schema.is_object()) else {
return false;
};
let Some(items) = value.as_array() else {
return false;
};
let Some((min, max)) = contain_limits(schema) else {
return false;
};
let count = u64::try_from(
items
.iter()
.filter(|item| branch_accepts(contains, item))
.count(),
)
.unwrap_or(u64::MAX);
count < min || max.is_some_and(|limit| count > limit)
}
fn contain_limits(schema: &Value) -> Option<(u64, Option<u64>)> {
let min = match schema.get("minContains") {
None => 1,
Some(Value::Number(number)) => number.as_u64()?,
Some(_) => return None,
};
let max = match schema.get("maxContains") {
None => None,
Some(Value::Number(number)) => Some(number.as_u64()?),
Some(_) => return None,
};
Some((min, max))
}
fn elements_match(schema: &Value, value: &Value) -> bool {
let Some(arr) = value.as_array() else {
return true;
};
if let Some((positions, tail)) = tuple_positions(schema) {
if matches!(tail, ArrayTail::Forbid) && arr.len() > positions.len() {
return false;
}
return arr
.iter()
.enumerate()
.all(|(index, item)| match positions.get(index) {
Some(position) => branch_accepts(position, item),
None => match tail {
ArrayTail::Schema(rest) => branch_accepts(rest, item),
ArrayTail::Keep | ArrayTail::Forbid => true,
},
});
}
let Some(items) = schema.get("items").filter(|items| items.is_object()) else {
return true;
};
arr.iter().all(|item| branch_accepts(items, item))
}
fn properties_match(schema: &Value, value: &Value) -> bool {
let Some(obj) = value.as_object() else {
return true;
};
let Some(props) = schema.get("properties").and_then(Value::as_object) else {
return true;
};
props.iter().all(|(key, prop_schema)| {
obj.get(key)
.is_none_or(|child| branch_accepts(prop_schema, child))
})
}
fn constraints_hold(schema: &Value, value: &Value) -> bool {
let types = schema_type_names(schema);
if !types.is_empty() && !type_matches(&types, value) {
return false;
}
if let Some(const_value) = schema.get("const")
&& const_value != value
&& !values_equivalent(const_value, value)
{
return false;
}
if let Some(items) = schema.get("enum").and_then(Value::as_array)
&& !items
.iter()
.any(|item| item == value || values_equivalent(item, value))
{
return false;
}
true
}
fn type_matches(types: &[&str], value: &Value) -> bool {
types.iter().any(|kind| match *kind {
"string" => value.is_string(),
"integer" => value.as_i64().is_some() || value.as_u64().is_some(),
"number" => value.as_number().is_some(),
"boolean" => value.is_boolean(),
"null" => value.is_null(),
"object" => value.is_object(),
"array" => value.is_array(),
_ => false,
})
}
fn tuple_positions(schema: &Value) -> Option<(&Vec<Value>, ArrayTail<'_>)> {
if let Some(Value::Array(prefix)) = schema.get("prefixItems") {
let tail = match schema.get("items") {
Some(Value::Bool(false)) => ArrayTail::Forbid,
Some(rest) if rest.is_object() => ArrayTail::Schema(rest),
_ => ArrayTail::Keep,
};
return Some((prefix, tail));
}
if let Some(Value::Array(items)) = schema.get("items") {
let tail = match schema.get("additionalItems") {
Some(Value::Bool(false)) => ArrayTail::Forbid,
Some(rest) if rest.is_object() => ArrayTail::Schema(rest),
_ => ArrayTail::Keep,
};
return Some((items, tail));
}
None
}
fn normalize_array(arr: &[Value], schema: &Value) -> Result<(Value, Vec<Rewrite>), RepairError> {
if let Some((positions, tail)) = tuple_positions(schema) {
let tail = forbid_unevaluated_items(schema, tail);
if matches!(tail, ArrayTail::Forbid) && arr.len() > positions.len() {
return Err(RepairError::Ambiguous(
"array item is past the tuple schema".into(),
));
}
let mut out = Vec::with_capacity(arr.len());
let mut rewrites = Vec::new();
for (index, item) in arr.iter().enumerate() {
if let Some(position) = positions.get(index) {
let (next, more) = normalize_child(item, position)?;
rewrites.extend(more);
out.push(next);
} else if let ArrayTail::Schema(rest) = tail {
let (next, more) = normalize_child(item, rest)?;
rewrites.extend(more);
out.push(next);
} else {
out.push(item.clone());
}
}
return Ok((Value::Array(out), rewrites));
}
if forbids_every_item(schema) {
if arr.is_empty() {
return Ok((Value::Array(Vec::new()), Vec::new()));
}
return Err(RepairError::Ambiguous(
"array item is not allowed by the schema".into(),
));
}
let Some(items) = schema.get("items").filter(|items| items.is_object()) else {
return Ok((Value::Array(arr.to_vec()), Vec::new()));
};
let mut out = Vec::with_capacity(arr.len());
let mut rewrites = Vec::new();
for item in arr {
let (next, more) = normalize_child(item, items)?;
rewrites.extend(more);
out.push(next);
}
Ok((Value::Array(out), rewrites))
}
fn normalize_object(
obj: &Map<String, Value>,
schema: &Value,
props: &Map<String, Value>,
) -> Result<(Value, Vec<Rewrite>), RepairError> {
let mut out = Map::new();
let mut rewrites = Vec::new();
let mut unknown = Vec::new();
for (key, val) in obj {
if let Some(prop_schema) = props.get(key) {
let (next, more) = normalize_child(val, prop_schema)?;
rewrites.extend(more);
out.insert(key.clone(), next);
continue;
}
match alias_target(key, props)? {
Some(target) => {
let existing = out.get(target).or_else(|| obj.get(target));
if let Some(existing) = existing {
if existing != val {
return Err(RepairError::Ambiguous(format!(
"'{key}' and '{target}' are both set and disagree"
)));
}
rewrites.push(rewrite(
RewriteKind::Alias,
format!("dropped duplicate alias {key}"),
));
continue;
}
let Some(prop_schema) = props.get(target) else {
unknown.push((key.clone(), val.clone()));
continue;
};
let (next, more) = normalize_child(val, prop_schema)?;
rewrites.extend(more);
rewrites.push(rewrite(
RewriteKind::Alias,
format!("renamed {key} to {target}"),
));
out.insert(target.to_string(), next);
}
None => unknown.push((key.clone(), val.clone())),
}
}
if denies_unknown(schema) {
if !unknown.is_empty() && has_pattern_properties(schema) {
let key = &unknown[0].0;
return Err(RepairError::Ambiguous(format!(
"unknown key '{key}' may be allowed by patternProperties"
)));
}
if unknown.len() > 1 {
let reason = unknown_denial(schema).unwrap_or("unknown keys are forbidden");
return Err(RepairError::Ambiguous(format!(
"{} unknown keys and {reason}",
unknown.len()
)));
}
if let Some((key, _)) = unknown.pop() {
let ready = required_present(schema, &out);
if ready && !out.is_empty() {
rewrites.push(rewrite(
RewriteKind::DroppedUnknown,
format!("dropped unknown key {key}"),
));
} else if ready {
return Err(RepairError::Ambiguous(format!(
"refusing to drop the only key '{key}'"
)));
} else {
return Err(RepairError::Ambiguous(format!(
"unknown key '{key}' and a required field is missing"
)));
}
}
} else if let Some(extra) = additional_properties_schema(schema) {
if !unknown.is_empty() && has_pattern_properties(schema) {
let key = &unknown[0].0;
return Err(RepairError::Ambiguous(format!(
"unknown key '{key}' may be allowed by patternProperties"
)));
}
for (key, val) in unknown {
let (next, more) = normalize_child(&val, extra)?;
rewrites.extend(more);
out.insert(key, next);
}
} else {
for (key, val) in unknown {
out.insert(key, val);
}
}
Ok((Value::Object(out), rewrites))
}
fn alias_target<'a>(
key: &str,
props: &'a Map<String, Value>,
) -> Result<Option<&'a str>, RepairError> {
let mut targets: Vec<&str> = props
.iter()
.filter(|(_, prop)| property_lists_alias(prop, key))
.map(|(name, _)| name.as_str())
.collect();
if targets.is_empty() && PATH_KEYS.contains(&key) {
for name in props.keys() {
if PATH_KEYS.contains(&name.as_str()) {
targets.push(name.as_str());
}
}
}
match targets.as_slice() {
[] => Ok(None),
[one] => Ok(Some(*one)),
_ => Err(RepairError::Ambiguous(format!(
"'{key}' matches more than one property"
))),
}
}
fn property_lists_alias(prop: &Value, key: &str) -> bool {
prop.get("x-aliases")
.and_then(Value::as_array)
.is_some_and(|aliases| aliases.iter().any(|alias| alias.as_str() == Some(key)))
}
fn object_properties(schema: &Value) -> Option<&Map<String, Value>> {
let schema = if schema.get("properties").is_none() {
schema.get("parameters").unwrap_or(schema)
} else {
schema
};
schema.get("properties")?.as_object()
}
fn has_pattern_properties(schema: &Value) -> bool {
fn nonempty(value: &Value) -> bool {
value
.get("patternProperties")
.and_then(Value::as_object)
.is_some_and(|patterns| !patterns.is_empty())
}
nonempty(schema) || schema.get("parameters").is_some_and(nonempty)
}
fn additional_properties_schema(schema: &Value) -> Option<&Value> {
let schema = if schema.get("properties").is_none() {
schema.get("parameters").unwrap_or(schema)
} else {
schema
};
schema
.get("additionalProperties")
.filter(|value| value.is_object())
}
fn denies_unknown(schema: &Value) -> bool {
unknown_denial(schema).is_some()
}
fn unknown_denial(schema: &Value) -> Option<&'static str> {
let schema = if schema.get("properties").is_none() {
schema.get("parameters").unwrap_or(schema)
} else {
schema
};
if matches!(schema.get("additionalProperties"), Some(Value::Bool(false))) {
return Some("additionalProperties is false");
}
if schema.get("additionalProperties").is_none()
&& schema.get("unevaluatedProperties") == Some(&Value::Bool(false))
{
return Some("unevaluatedProperties is false");
}
None
}
fn required_holds(schema: &Value, value: &Value) -> bool {
let Some(obj) = value.as_object() else {
let schema = if schema.get("properties").is_none() {
schema.get("parameters").unwrap_or(schema)
} else {
schema
};
return schema
.get("required")
.and_then(Value::as_array)
.is_none_or(|items| items.iter().all(|item| item.as_str().is_none()));
};
required_present(schema, obj)
}
fn required_present(schema: &Value, out: &Map<String, Value>) -> bool {
let schema = if schema.get("properties").is_none() {
schema.get("parameters").unwrap_or(schema)
} else {
schema
};
let Some(required) = schema.get("required").and_then(Value::as_array) else {
return true;
};
required
.iter()
.filter_map(Value::as_str)
.all(|name| out.contains_key(name))
}
fn describes_array(schema: &Value) -> bool {
let types = schema_type_names(schema);
if types.contains(&"array") {
return true;
}
if !types.is_empty() {
return false;
}
schema.get("items").is_some()
|| schema.get("prefixItems").is_some()
|| schema.get("unevaluatedItems").is_some()
}
fn forbid_unevaluated_items<'a>(schema: &'a Value, tail: ArrayTail<'a>) -> ArrayTail<'a> {
if matches!(tail, ArrayTail::Keep)
&& unevaluated_tuple_tail(schema)
&& schema.get("unevaluatedItems") == Some(&Value::Bool(false))
{
ArrayTail::Forbid
} else {
tail
}
}
fn unevaluated_tuple_tail(schema: &Value) -> bool {
if schema.get("prefixItems").is_some() {
return schema.get("items").is_none();
}
schema.get("items").and_then(Value::as_array).is_some()
&& schema.get("additionalItems").is_none()
}
fn forbids_every_item(schema: &Value) -> bool {
if schema.get("prefixItems").is_some() {
return false;
}
if schema.get("items") == Some(&Value::Bool(false)) {
return true;
}
schema.get("items").is_none() && schema.get("unevaluatedItems") == Some(&Value::Bool(false))
}
fn schema_type_names(schema: &Value) -> Vec<&str> {
match schema.get("type") {
Some(Value::String(text)) => vec![text.as_str()],
Some(Value::Array(items)) => items.iter().filter_map(Value::as_str).collect(),
_ => Vec::new(),
}
}
fn deep_constrained(value: &Value, schema: &Value) -> Option<(Value, Vec<Rewrite>)> {
if !value.is_object() && !value.is_array() {
return None;
}
let targets = constrained_values(schema)?;
let mut matched = Vec::new();
for target in targets {
if let Some((next, rewrites)) = coerce_toward(value, target, schema)
&& &next == target
{
matched.push((next, rewrites));
}
}
if matched.len() != 1 {
return None;
}
let (next, rewrites) = matched.pop()?;
if &next == value && rewrites.is_empty() {
return None;
}
Some((next, rewrites))
}
fn coerce_toward(value: &Value, target: &Value, schema: &Value) -> Option<(Value, Vec<Rewrite>)> {
if value == target {
return Some((value.clone(), Vec::new()));
}
if let Some(pair) = exact_float_toward(value, target, schema) {
return Some(pair);
}
match (value, target) {
(Value::Array(items), Value::Array(targets)) => {
if items.len() != targets.len() {
return None;
}
let mut out = Vec::with_capacity(items.len());
let mut rewrites = Vec::new();
for (item, target_item) in items.iter().zip(targets) {
let (next, more) = coerce_toward(item, target_item, schema)?;
rewrites.extend(more);
out.push(next);
}
Some((Value::Array(out), rewrites))
}
(Value::Object(obj), Value::Object(target_obj)) => {
if obj.len() != target_obj.len() {
return None;
}
let mut out = Map::new();
let mut rewrites = Vec::new();
for (key, target_child) in target_obj {
let child = obj.get(key)?;
let (next, more) = coerce_toward(child, target_child, schema)?;
rewrites.extend(more);
out.insert(key.clone(), next);
}
Some((Value::Object(out), rewrites))
}
(Value::String(text), _) => {
let text = text.trim_matches(char::is_whitespace);
if text.is_empty() || !string_matches_target(text, target) {
return None;
}
let mut rewrites = Vec::new();
record_constrained(text, target, schema, &mut rewrites);
Some((target.clone(), rewrites))
}
_ => None,
}
}
fn exact_float_toward(
value: &Value,
target: &Value,
schema: &Value,
) -> Option<(Value, Vec<Rewrite>)> {
let left = value.as_number()?;
let right = target.as_number()?;
if !numbers_equal(left, right) {
return None;
}
let shown = value.as_f64()?;
if !shown.is_finite() || shown.fract() != 0.0 || shown.abs() > (1u64 << 53) as f64 {
return None;
}
let word = if schema.get("const").is_some() {
"const"
} else {
"enum"
};
let mut rewrites = Vec::new();
rewrites.push(rewrite(
RewriteKind::NumberString,
format!("{word} number {shown:?}"),
));
Some((target.clone(), rewrites))
}
fn outside_const_or_enum(schema: &Value, value: &Value) -> bool {
if let Some(const_value) = schema.get("const")
&& const_value != value
&& !values_equivalent(const_value, value)
{
return true;
}
if let Some(items) = schema.get("enum").and_then(Value::as_array) {
return !items
.iter()
.any(|item| item == value || values_equivalent(item, value));
}
false
}
fn coerce(value: &Value, schema: &Value, rewrites: &mut Vec<Rewrite>) -> Value {
if let Some(number) = integer_from_exact_float(value, schema) {
rewrites.push(rewrite(
RewriteKind::NumberString,
format!("integer number {number}"),
));
return Value::Number(number);
}
if let Some(matched) = float_const_or_enum(value, schema) {
let word = if schema.get("const").is_some() {
"const"
} else {
"enum"
};
let shown = value.as_f64().unwrap_or(0.0);
rewrites.push(rewrite(
RewriteKind::NumberString,
format!("{word} number {shown:?}"),
));
return matched;
}
let Some(text) = value.as_str() else {
return value.clone();
};
let text = text.trim_matches(char::is_whitespace);
if text.is_empty() {
return value.clone();
}
let types = schema_type_names(schema);
if types.contains(&"string") {
return value.clone();
}
if types.is_empty() {
if let Some(matched) = const_or_enum_value(value, text, schema) {
record_constrained(text, &matched, schema, rewrites);
return matched;
}
return value.clone();
}
if types.contains(&"null") && text.eq_ignore_ascii_case("null") {
rewrites.push(rewrite(
RewriteKind::NullString,
format!("null string {text}"),
));
return Value::Null;
}
if types.contains(&"boolean") {
if text.eq_ignore_ascii_case("true") {
rewrites.push(rewrite(
RewriteKind::BoolString,
"boolean string true".into(),
));
return Value::Bool(true);
}
if text.eq_ignore_ascii_case("false") {
rewrites.push(rewrite(
RewriteKind::BoolString,
"boolean string false".into(),
));
return Value::Bool(false);
}
}
if types.contains(&"integer")
&& let Some(number) = integer_string(text).or_else(|| integer_from_zero_fraction(text))
{
rewrites.push(rewrite(
RewriteKind::NumberString,
format!("integer string {text}"),
));
return Value::Number(number);
}
if types.contains(&"number")
&& let Some(number) = number_string(text)
{
rewrites.push(rewrite(
RewriteKind::NumberString,
format!("number string {text}"),
));
return Value::Number(number);
}
value.clone()
}
fn integer_from_exact_float(value: &Value, schema: &Value) -> Option<serde_json::Number> {
let types = schema_type_names(schema);
if types.is_empty()
|| types.contains(&"number")
|| types.contains(&"string")
|| !types.contains(&"integer")
{
return None;
}
if value.as_i64().is_some() || value.as_u64().is_some() {
return None;
}
let number = value.as_f64()?;
if !number.is_finite() || number.fract() != 0.0 || number.abs() > (1u64 << 53) as f64 {
return None;
}
let as_int = number as i64;
if as_int as f64 != number {
return None;
}
Some(serde_json::Number::from(as_int))
}
fn float_const_or_enum(value: &Value, schema: &Value) -> Option<Value> {
if value.as_i64().is_some() || value.as_u64().is_some() {
return None;
}
let number = value.as_f64()?;
if !number.is_finite() || number.fract() != 0.0 || number.abs() > (1u64 << 53) as f64 {
return None;
}
let targets = constrained_values(schema)?;
if targets.contains(&value) {
return None;
}
let as_int = serde_json::Number::from(number as i64);
let mut chosen: Option<&Value> = None;
for target in &targets {
let Some(target_number) = target.as_number() else {
continue;
};
if !numbers_equal(&as_int, target_number) {
continue;
}
match chosen {
Some(existing) if values_equivalent(existing, target) => {
if target.as_i64().is_some() || target.as_u64().is_some() {
chosen = Some(target);
}
}
Some(_) => return None,
None => chosen = Some(target),
}
}
chosen.cloned()
}
fn const_or_enum_value(original: &Value, text: &str, schema: &Value) -> Option<Value> {
let targets = constrained_values(schema)?;
if targets.contains(&original) {
return None;
}
let mut chosen: Option<&Value> = None;
for target in &targets {
if !string_matches_target(text, target) {
continue;
}
match chosen {
Some(existing) if values_equivalent(existing, target) => {
if target.as_i64().is_some() || target.as_u64().is_some() {
chosen = Some(target);
}
}
Some(_) => return None,
None => chosen = Some(target),
}
}
chosen.cloned()
}
fn constrained_values(schema: &Value) -> Option<Vec<&Value>> {
let const_value = schema.get("const");
let enum_values = schema.get("enum").and_then(Value::as_array);
match (const_value, enum_values) {
(None, None) => None,
(Some(value), None) => Some(vec![value]),
(None, Some(items)) => Some(items.iter().collect()),
(Some(value), Some(items)) => Some(items.iter().filter(|item| *item == value).collect()),
}
}
fn string_matches_target(text: &str, target: &Value) -> bool {
match target {
Value::Bool(flag) => text.eq_ignore_ascii_case(if *flag { "true" } else { "false" }),
Value::Null => text.eq_ignore_ascii_case("null"),
Value::Number(number) => number_matches(text, number),
_ => false,
}
}
fn number_matches(text: &str, target: &serde_json::Number) -> bool {
if let Some(number) = integer_string(text).or_else(|| integer_from_zero_fraction(text))
&& numbers_equal(&number, target)
{
return true;
}
number_string(text).is_some_and(|number| numbers_equal(&number, target))
}
fn numbers_equal(left: &serde_json::Number, right: &serde_json::Number) -> bool {
if left == right {
return true;
}
match (left.as_f64(), right.as_f64()) {
(Some(lhs), Some(rhs)) => {
let limit = (1u64 << 53) as f64;
lhs == rhs && lhs.abs() <= limit && rhs.abs() <= limit
}
_ => false,
}
}
fn values_equivalent(left: &Value, right: &Value) -> bool {
if left == right {
return true;
}
match (left.as_number(), right.as_number()) {
(Some(lhs), Some(rhs)) => numbers_equal(lhs, rhs),
_ => false,
}
}
fn record_constrained(text: &str, matched: &Value, schema: &Value, rewrites: &mut Vec<Rewrite>) {
let word = if schema.get("const").is_some() {
"const"
} else {
"enum"
};
match matched {
Value::Bool(true) => rewrites.push(rewrite(
RewriteKind::BoolString,
format!("{word} boolean {text}"),
)),
Value::Bool(false) => rewrites.push(rewrite(
RewriteKind::BoolString,
format!("{word} boolean {text}"),
)),
Value::Null => rewrites.push(rewrite(
RewriteKind::NullString,
format!("{word} null {text}"),
)),
Value::Number(_) => rewrites.push(rewrite(
RewriteKind::NumberString,
format!("{word} number {text}"),
)),
_ => {}
}
}
fn integer_string(text: &str) -> Option<serde_json::Number> {
let text = without_digit_underscores(text)?;
let text = text.strip_prefix('+').unwrap_or(&text);
if text.is_empty() || !is_integer_token(text) || has_leading_zero(text) {
return None;
}
let parsed = text.parse::<i64>().ok()?;
Some(serde_json::Number::from(parsed))
}
fn integer_from_zero_fraction(text: &str) -> Option<serde_json::Number> {
let text = without_digit_underscores(text)?;
let text = text.strip_prefix('+').unwrap_or(&text);
let (negative, rest) = match text.strip_prefix('-') {
Some(rest) => (true, rest),
None => (false, text),
};
let (whole, frac) = rest.split_once('.')?;
if whole.is_empty()
|| !frac.bytes().all(|byte| byte == b'0')
|| !is_integer_token(whole)
|| has_leading_zero(whole)
{
return None;
}
let mut digits = String::with_capacity(whole.len() + 1);
if negative && whole.bytes().any(|byte| byte != b'0') {
digits.push('-');
}
digits.push_str(whole);
let parsed = digits.parse::<i64>().ok()?;
Some(serde_json::Number::from(parsed))
}
fn number_string(text: &str) -> Option<serde_json::Number> {
let text = without_digit_underscores(text)?;
let text = text.strip_prefix('+').unwrap_or(&text);
if let Some(number) = integer_string(text) {
return Some(number);
}
if let Some(number) = integer_from_zero_fraction(text) {
return Some(number);
}
if (!is_decimal_token(text) && !is_scientific_token(text)) || has_leading_zero(text) {
return None;
}
let parsed = text.parse::<f64>().ok()?;
if !parsed.is_finite() {
return None;
}
let number = serde_json::Number::from_f64(parsed)?;
if !same_decimal(text, &number.to_string()) {
return None;
}
Some(number)
}
#[derive(PartialEq, Eq)]
struct Decimal {
neg: bool,
coef: u128,
exp10: i32,
}
fn same_decimal(left: &str, right: &str) -> bool {
match (parse_decimal(left), parse_decimal(right)) {
(Some(left), Some(right)) => left == right,
_ => false,
}
}
fn parse_decimal(text: &str) -> Option<Decimal> {
let neg = text.starts_with('-');
let text = text.strip_prefix(['+', '-']).unwrap_or(text);
let (mantissa, exp_text) = text
.split_once(['e', 'E'])
.map_or((text, None), |(mantissa, exp)| (mantissa, Some(exp)));
let exp: i32 = match exp_text {
Some(exp) => exp.parse().ok()?,
None => 0,
};
let (whole, frac) = mantissa
.split_once('.')
.map_or((mantissa, ""), |(whole, frac)| (whole, frac));
if whole.is_empty() || !whole.bytes().all(|b| b.is_ascii_digit()) {
return None;
}
if !frac.bytes().all(|b| b.is_ascii_digit()) {
return None;
}
let frac = frac.trim_end_matches('0');
let mut digits = String::new();
digits.push_str(whole.trim_start_matches('0'));
digits.push_str(frac);
let digits = digits.trim_start_matches('0');
if digits.is_empty() {
return Some(Decimal {
neg: false,
coef: 0,
exp10: 0,
});
}
if digits.len() > 38 {
return None;
}
let mut coef: u128 = digits.parse().ok()?;
let mut exp10 = exp.checked_sub(i32::try_from(frac.len()).ok()?)?;
while coef.is_multiple_of(10) {
coef /= 10;
exp10 = exp10.checked_add(1)?;
}
Some(Decimal {
neg: neg && coef != 0,
coef,
exp10,
})
}
fn is_scientific_token(text: &str) -> bool {
let Some((mantissa, exp)) = text.split_once(['e', 'E']) else {
return false;
};
let exp = exp.strip_prefix('+').unwrap_or(exp);
let exp_digits = exp.strip_prefix('-').unwrap_or(exp);
if exp_digits.is_empty() || !exp_digits.bytes().all(|byte| byte.is_ascii_digit()) {
return false;
}
is_integer_token(mantissa) || is_decimal_token(mantissa)
}
fn without_digit_underscores(text: &str) -> Option<String> {
if !text.contains('_') {
return Some(text.to_string());
}
let chars: Vec<char> = text.chars().collect();
let mut out = String::with_capacity(text.len());
for (index, ch) in chars.iter().copied().enumerate() {
if ch == '_' {
let before_digit = index > 0 && chars[index - 1].is_ascii_digit();
let after_digit = chars.get(index + 1).is_some_and(char::is_ascii_digit);
if before_digit && after_digit {
continue;
}
return None;
}
out.push(ch);
}
Some(out)
}
fn has_leading_zero(text: &str) -> bool {
let mantissa = text.split(['e', 'E']).next().unwrap_or(text);
let digits = mantissa.strip_prefix('-').unwrap_or(mantissa);
let whole = digits.split('.').next().unwrap_or(digits);
whole.len() > 1 && whole.starts_with('0')
}
fn is_integer_token(text: &str) -> bool {
let digits = text.strip_prefix('-').unwrap_or(text);
!digits.is_empty() && digits.bytes().all(|b| b.is_ascii_digit())
}
fn is_decimal_token(text: &str) -> bool {
let digits = text.strip_prefix('-').unwrap_or(text);
let mut parts = digits.split('.');
let whole = parts.next().unwrap_or("");
let frac = parts.next().unwrap_or("");
parts.next().is_none()
&& !whole.is_empty()
&& !frac.is_empty()
&& whole.bytes().all(|b| b.is_ascii_digit())
&& frac.bytes().all(|b| b.is_ascii_digit())
}
fn rewrite(kind: RewriteKind, detail: String) -> Rewrite {
Rewrite { kind, detail }
}