Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions parser/src/json/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,11 @@ impl Compiler {
})?;
let mut ast = RegexAst::Regex(rx);
if let Some(d) = num.multiple_of.as_ref() {
ast = RegexAst::And(vec![ast, RegexAst::MultipleOf(d.coef, d.exp)]);
let multiple_rx = RegexAst::Concat(vec![
RegexAst::Regex("-?".to_string()),
RegexAst::MultipleOf(d.coef, d.exp),
]);
ast = RegexAst::And(vec![ast, multiple_rx]);
}
Ok(ast)
}
Expand All @@ -314,7 +318,11 @@ impl Compiler {
})?;
let mut ast = RegexAst::Regex(rx);
if let Some(d) = num.multiple_of.as_ref() {
ast = RegexAst::And(vec![ast, RegexAst::MultipleOf(d.coef, d.exp)]);
let multiple_rx = RegexAst::Concat(vec![
RegexAst::Regex("-?".to_string()),
RegexAst::MultipleOf(d.coef, d.exp),
]);
ast = RegexAst::And(vec![ast, multiple_rx]);
}
Ok(ast)
}
Expand Down
6 changes: 4 additions & 2 deletions parser/src/json/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,8 +920,10 @@ fn compile_numeric(schema: &HashMap<&str, &Value>, integer: bool) -> Result<Sche
let f = val.as_f64().ok_or_else(|| {
anyhow!("Expected f64 for 'multipleOf', got {}", limited_str(val))
})?;
// Can discard the sign of f
Some(Decimal::try_from(f.abs())?)
if f <= 0.0 {
bail!("'multipleOf' must be a positive number, got {}", f);
}
Some(Decimal::try_from(f)?)
}
};
Ok(Schema::Number(NumberSchema {
Expand Down
37 changes: 37 additions & 0 deletions sample_parser/tests/test_json_primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,40 @@ fn string_length_unsatisfiable() {
"Unsatisfiable schema: minLength (2) is greater than maxLength (1)",
);
}

#[rstest]
#[case::zero(0.0)]
#[case::negative(-1.0)]
fn test_multiple_of_bad_value(#[case] value: f64) {
let schema = &json!({"type":"integer", "multipleOf": value});
json_err_test(
schema,
&format!("'multipleOf' must be a positive number, got {}", value),
);
}

#[rstest]
#[case::zero(0, true)]
#[case::one(1, false)]
#[case::three(3, true)]
#[case::six(6, true)]
#[case::negative_one(-1, false)]
#[case::negative_two(-2, false)]
#[case::negative_three(-3, true)]
fn test_multiple_of_int(#[case] value: i64, #[case] should_pass: bool) {
let schema = &json!({"type":"integer", "multipleOf": 3});
json_schema_check(schema, &json!(value), should_pass);
}

#[rstest]
#[case(0.0, true)]
#[case(0.3, true)]
#[case(0.6, true)]
#[case(1.0, false)]
#[case(-1.0, false)]
#[case(-1.2, true)]
#[case(-0.3, true)]
fn test_multiple_of_float(#[case] value: f64, #[case] should_pass: bool) {
let schema = &json!({"type":"number", "multipleOf": 0.3});
json_schema_check(schema, &json!(value), should_pass);
}
Loading