Skip to content

Commit

Permalink
Make test schemas stricter
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau committed Sep 14, 2024
1 parent 88eb609 commit cf7f4b1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
15 changes: 11 additions & 4 deletions schemars/tests/integration/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ enum External {
#[schemars(with = "u64")]
UnitAsInt,
#[serde(with = "tuple_variant_as_str")]
#[schemars(with = "str")]
#[schemars(schema_with = "tuple_variant_as_str::json_schema")]
TupleAsStr(i32, bool),
}

Expand Down Expand Up @@ -75,7 +75,7 @@ enum Internal {
// UnitAsInt,
// Internally-tagged enums don't support tuple variants
// #[serde(with = "tuple_variant_as_str")]
// #[schemars(with = "str")]
// #[schemars(schema_with = "tuple_variant_as_str::json_schema")]
// TupleAsStr(i32, bool),
}

Expand Down Expand Up @@ -122,7 +122,7 @@ enum Adjacent {
#[schemars(with = "u64")]
UnitAsInt,
#[serde(with = "tuple_variant_as_str")]
#[schemars(with = "str")]
#[schemars(schema_with = "tuple_variant_as_str::json_schema")]
TupleAsStr(i32, bool),
}

Expand Down Expand Up @@ -169,7 +169,7 @@ enum Untagged {
#[schemars(with = "u64")]
UnitAsInt,
#[serde(with = "tuple_variant_as_str")]
#[schemars(with = "str", extend("pattern" = r"^\d+ (true|false)$"))]
#[schemars(schema_with = "tuple_variant_as_str::json_schema")]
TupleAsStr(i32, bool),
}

Expand Down Expand Up @@ -241,6 +241,13 @@ mod tuple_variant_as_str {
b.parse().map_err(|_| error())?,
))
}

pub(super) fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
schemars::json_schema!({
"type": "string",
"pattern": r"^\d+ (true|false)$"
})
}
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@
"const": "TupleAsStr"
},
"content": {
"type": "string"
"type": "string",
"pattern": "^\\d+ (true|false)$"
}
},
"required": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@
"const": "TupleAsStr"
},
"content": {
"type": "string"
"type": "string",
"pattern": "^\\d+ (true|false)$"
}
},
"required": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@
"type": "object",
"properties": {
"tupleAsStr": {
"type": "string"
"type": "string",
"pattern": "^\\d+ (true|false)$"
}
},
"required": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@
"type": "object",
"properties": {
"tupleAsStr": {
"type": "string"
"type": "string",
"pattern": "^\\d+ (true|false)$"
}
},
"required": [
Expand Down

0 comments on commit cf7f4b1

Please sign in to comment.