Skip to content

Commit

Permalink
Don't allow multiple format in string constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDiekmann committed Nov 29, 2024
1 parent 0cf43a6 commit 73249e2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ pub enum ResolveClosedDataTypeError {
ConflictingConstEnumValue(JsonValue, Vec<JsonValue>),
#[error("The constraint is unsatisfiable: {}", json!(.0))]
UnsatisfiableConstraint(ValueConstraints),
#[error("The constraints are incompatible: {} <=> {}", json!(.0), json!(.1))]
IncompatibleConstraints(ValueConstraints, ValueConstraints),
#[error("The combined constraints results in an empty `anyOf`")]
EmptyAnyOf,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,28 @@ impl Constraint for StringConstraints {
Some((lhs, rhs)) => Some(lhs.min(rhs)),
None => self.max_length.or(other.max_length),
};
match self.format.zip(other.format) {
Some((lhs, rhs)) if lhs == rhs => {}
Some((_, rhs)) => {
remainder.get_or_insert_default().format = Some(rhs);
self.format = match self.format.zip(other.format) {
Some((lhs, rhs)) => {
ensure!(
lhs == rhs,
ResolveClosedDataTypeError::IncompatibleConstraints(
ValueConstraints::Typed(SingleValueConstraints::String(
StringSchema::Constrained(Self {
format: Some(lhs),
..Self::default()
}),
)),
ValueConstraints::Typed(SingleValueConstraints::String(
StringSchema::Constrained(Self {
format: Some(rhs),
..Self::default()
}),
))
)
);
Some(lhs)
}
None => self.format = self.format.or(other.format),
None => self.format.or(other.format),
};
match self.pattern.as_ref().zip(other.pattern.as_ref()) {
Some((lhs, rhs)) if lhs.as_str() == rhs.as_str() => {}
Expand Down Expand Up @@ -854,17 +870,7 @@ mod tests {

#[test]
fn intersect_format_both_different() {
check_schema_intersection(
[
json!({
"type": "string",
"format": "uri",
}),
json!({
"type": "string",
"format": "hostname",
}),
],
check_schema_intersection_error(
[
json!({
"type": "string",
Expand All @@ -875,6 +881,22 @@ mod tests {
"format": "hostname",
}),
],
[ResolveClosedDataTypeError::IncompatibleConstraints(
from_value(json!(
{
"type": "string",
"format": "uri",
}
))
.expect("schema should be a valid string schema"),
from_value(json!(
{
"type": "string",
"format": "hostname",
}
))
.expect("schema should be a valid string schema"),
)],
);
}

Expand Down

0 comments on commit 73249e2

Please sign in to comment.