Skip to content

Commit

Permalink
fixes oxidecomputer#589 allow min and max to be within range
Browse files Browse the repository at this point in the history
  • Loading branch information
davetayls committed Jan 5, 2025
1 parent ea32b6f commit ce97c07
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions typify-impl/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,14 +1011,14 @@ impl TypeSpace {
];

if let Some(format) = format {
if let Some((_, ty, imin, imax)) = formats
if let Some((_fmt, ty, imin, imax)) = formats
.iter()
.find(|(int_format, _, _, _)| int_format == format)
{
// If the type matches with other constraints, we're done.
if multiple.is_none()
&& (min.is_none() || min == Some(*imin))
&& (max.is_none() || max == Some(*imax))
&& (min.map(|f| f.ge(imin)).unwrap_or(false))
&& (max.map(|f| f.le(imax)).unwrap_or(false))
{
// If there's a default value and it's either not a number
// or outside of the range for this format, return an
Expand Down Expand Up @@ -2001,6 +2001,7 @@ mod tests {
test_util::validate_output, validate_builtin, Error, Name, TypeSpace, TypeSpaceImpl,
TypeSpaceSettings,
};
use crate::type_entry::TypeEntryDetails;

#[track_caller]
fn int_helper<T: JsonSchema>(type_name: &'static str) {
Expand Down Expand Up @@ -2115,6 +2116,35 @@ mod tests {
validate_builtin!(std::collections::BTreeSet<u32>);
}

#[test]
fn test_uint_minimum_and_maximum() {
let schema = SchemaObject {
instance_type: Some(InstanceType::Integer.into()),
format: Some("uint64".to_string()),
metadata: None,
number: Some(
NumberValidation {
minimum: Some(0.),
maximum: Some(256.0),
..Default::default()
}
.into(),
),
..Default::default()
};

let mut type_space = TypeSpace::default();
match type_space.convert_schema_object(
Name::Unknown,
&schemars::schema::Schema::Object(schema.clone()),
&schema,
) {
Ok((te, _ob)) => {
assert_eq!(te.details, TypeEntryDetails::Integer("u64".to_string()));
},
_ => panic!("unexpected result"),
}
}
#[test]
fn test_low_default() {
let schema = SchemaObject {
Expand Down

0 comments on commit ce97c07

Please sign in to comment.