Debian Codegraph @

src/keywords/multiple_of.rs

raw symbols SCIP
1 use crate::{
2 compiler,
3 error::ValidationError,
4 ext::numeric,
5 keywords::CompilationResult,
6 paths::{LazyLocation, Location},
7 types::JsonType,
8 validator::{Validate, ValidationContext},
9 };
10 use serde_json::{Map, Value};
11
12 pub(crate) struct MultipleOfFloatValidator {
13 multiple_of: f64,
14 #[cfg(feature = "arbitrary-precision")]
15 original_value: serde_json::Number,
16 location: Location,
17 }
18
19 impl MultipleOfFloatValidator {
20 #[inline]
21 pub(crate) fn compile<'a>(
22 multiple_of: f64,
23 #[cfg(feature = "arbitrary-precision")] original_value: &serde_json::Number,
24 location: Location,
25 ) -> CompilationResult<'a> {
26 Ok(Box::new(MultipleOfFloatValidator {
27 multiple_of,
28 #[cfg(feature = "arbitrary-precision")]
29 original_value: original_value.clone(),
30 location,
31 }))
32 }
33 }
34
35 impl Validate for MultipleOfFloatValidator {
36 fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool {
37 if let Value::Number(item) = instance {
38 numeric::is_multiple_of_float(item, self.multiple_of)
39 } else {
40 true
41 }
42 }
43
44 fn validate<'i>(
45 &self,
46 instance: &'i Value,
47 location: &LazyLocation,
48 ctx: &mut ValidationContext,
49 ) -> Result<(), ValidationError<'i>> {
50 if !self.is_valid(instance, ctx) {
51 #[cfg(feature = "arbitrary-precision")]
52 {
53 return Err(ValidationError::multiple_of(
54 self.location.clone(),
55 location.into(),
56 instance,
57 Value::Number(self.original_value.clone()),
58 ));
59 }
60 #[cfg(not(feature = "arbitrary-precision"))]
61 {
62 return Err(ValidationError::multiple_of(
63 self.location.clone(),
64 location.into(),
65 instance,
66 self.multiple_of,
67 ));
68 }
69 }
70 Ok(())
71 }
72 }
73
74 pub(crate) struct MultipleOfIntegerValidator {
75 multiple_of: f64,
76 #[cfg(feature = "arbitrary-precision")]
77 original_value: serde_json::Number,
78 location: Location,
79 }
80
81 impl MultipleOfIntegerValidator {
82 #[inline]
83 pub(crate) fn compile<'a>(
84 multiple_of: f64,
85 #[cfg(feature = "arbitrary-precision")] original_value: &serde_json::Number,
86 location: Location,
87 ) -> CompilationResult<'a> {
88 Ok(Box::new(MultipleOfIntegerValidator {
89 multiple_of,
90 #[cfg(feature = "arbitrary-precision")]
91 original_value: original_value.clone(),
92 location,
93 }))
94 }
95 }
96
97 impl Validate for MultipleOfIntegerValidator {
98 fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool {
99 if let Value::Number(item) = instance {
100 numeric::is_multiple_of_integer(item, self.multiple_of)
101 } else {
102 true
103 }
104 }
105
106 fn validate<'i>(
107 &self,
108 instance: &'i Value,
109 location: &LazyLocation,
110 ctx: &mut ValidationContext,
111 ) -> Result<(), ValidationError<'i>> {
112 if !self.is_valid(instance, ctx) {
113 #[cfg(feature = "arbitrary-precision")]
114 {
115 return Err(ValidationError::multiple_of(
116 self.location.clone(),
117 location.into(),
118 instance,
119 Value::Number(self.original_value.clone()),
120 ));
121 }
122 #[cfg(not(feature = "arbitrary-precision"))]
123 {
124 return Err(ValidationError::multiple_of(
125 self.location.clone(),
126 location.into(),
127 instance,
128 self.multiple_of,
129 ));
130 }
131 }
132 Ok(())
133 }
134 }
135
136 #[cfg(feature = "arbitrary-precision")]
137 pub(crate) struct MultipleOfBigIntValidator {
138 multiple_of: num_bigint::BigInt,
139 original_value: serde_json::Number,
140 location: Location,
141 }
142
143 #[cfg(feature = "arbitrary-precision")]
144 impl MultipleOfBigIntValidator {
145 #[inline]
146 pub(crate) fn compile<'a>(
147 multiple_of: num_bigint::BigInt,
148 original_value: &serde_json::Number,
149 location: Location,
150 ) -> CompilationResult<'a> {
151 Ok(Box::new(MultipleOfBigIntValidator {
152 multiple_of,
153 original_value: original_value.clone(),
154 location,
155 }))
156 }
157 }
158
159 #[cfg(feature = "arbitrary-precision")]
160 impl Validate for MultipleOfBigIntValidator {
161 fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool {
162 use num_bigint::BigInt;
163 use num_traits::One;
164 if let Value::Number(item) = instance {
165 if let Some(instance_bigint) = numeric::bignum::try_parse_bigint(item) {
166 numeric::bignum::is_multiple_of_bigint(&instance_bigint, &self.multiple_of)
167 } else if let Some(v) = item.as_u64() {
168 let v_bigint = BigInt::from(v);
169 numeric::bignum::is_multiple_of_bigint(&v_bigint, &self.multiple_of)
170 } else if let Some(v) = item.as_i64() {
171 let v_bigint = BigInt::from(v);
172 numeric::bignum::is_multiple_of_bigint(&v_bigint, &self.multiple_of)
173 } else if let Some(v) = item.as_f64() {
174 if v.fract() != 0.0 {
175 return false;
176 }
177 // JSON numbers that originate as f64 are only exactly representable if their
178 // absolute value is below 2^53. Within that "safe integer" window we can round-trip
179 // to i64 without losing bits, so convert and reuse the bigint path.
180 #[allow(clippy::cast_precision_loss, clippy::cast_possible_truncation)]
181 if v.is_finite() && v.abs() < (1u64 << 53) as f64 {
182 let v_bigint = BigInt::from(v as i64);
183 numeric::bignum::is_multiple_of_bigint(&v_bigint, &self.multiple_of)
184 } else {
185 // 2^53 and above can no longer be recovered exactly from the binary64 mantissa,
186 // so we bail out instead of pretending we know whether the bigint divisor divides
187 // such an approximation.
188 false
189 }
190 } else {
191 // as_f64() returning None means the literal overflowed binary64 (e.g. 1e10000) or was
192 // written in plain decimal form with more precision than f64 can carry. We attempt to
193 // parse it as an exact BigFraction and only accept it when the denominator is 1 so we
194 // can safely convert it to BigInt before running the modulo check below.
195 if let Some(instance_bigfrac) = numeric::bignum::try_parse_bigfraction(item) {
196 if instance_bigfrac.denom().is_none_or(One::is_one) {
197 if let Some(numer) = instance_bigfrac.numer() {
198 let instance_bigint = BigInt::from(numer.clone());
199 return numeric::bignum::is_multiple_of_bigint(
200 &instance_bigint,
201 &self.multiple_of,
202 );
203 }
204 }
205 }
206 // If we made it here we encountered scientific notation that can’t be normalized, or
207 // a decimal with a fractional part. Either way it cannot be an integer multiple.
208 false
209 }
210 } else {
211 true
212 }
213 }
214
215 fn validate<'i>(
216 &self,
217 instance: &'i Value,
218 location: &LazyLocation,
219 ctx: &mut ValidationContext,
220 ) -> Result<(), ValidationError<'i>> {
221 if !self.is_valid(instance, ctx) {
222 return Err(ValidationError::multiple_of(
223 self.location.clone(),
224 location.into(),
225 instance,
226 Value::Number(self.original_value.clone()),
227 ));
228 }
229 Ok(())
230 }
231 }
232
233 #[cfg(feature = "arbitrary-precision")]
234 pub(crate) struct MultipleOfBigFracValidator {
235 multiple_of: fraction::BigFraction,
236 original_value: serde_json::Number,
237 location: Location,
238 }
239
240 #[cfg(feature = "arbitrary-precision")]
241 impl MultipleOfBigFracValidator {
242 #[inline]
243 pub(crate) fn compile<'a>(
244 multiple_of: fraction::BigFraction,
245 original_value: &serde_json::Number,
246 location: Location,
247 ) -> CompilationResult<'a> {
248 Ok(Box::new(MultipleOfBigFracValidator {
249 multiple_of,
250 original_value: original_value.clone(),
251 location,
252 }))
253 }
254 }
255
256 #[cfg(feature = "arbitrary-precision")]
257 impl Validate for MultipleOfBigFracValidator {
258 fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool {
259 use num_traits::ToPrimitive;
260 if let Value::Number(item) = instance {
261 // Try to parse instance as BigFraction for exact precision
262 if let Some(instance_bigfrac) = numeric::bignum::try_parse_bigfraction(item) {
263 numeric::bignum::is_multiple_of_bigfrac(&instance_bigfrac, &self.multiple_of)
264 } else if let Some(instance_bigint) = numeric::bignum::try_parse_bigint(item) {
265 let value_frac = fraction::BigFraction::from(instance_bigint);
266 numeric::bignum::is_multiple_of_bigfrac(&value_frac, &self.multiple_of)
267 } else if let Some(v) = item.as_u64() {
268 let v_frac = fraction::BigFraction::from(v);
269 numeric::bignum::is_multiple_of_bigfrac(&v_frac, &self.multiple_of)
270 } else if let Some(v) = item.as_i64() {
271 let v_frac = fraction::BigFraction::from(v);
272 numeric::bignum::is_multiple_of_bigfrac(&v_frac, &self.multiple_of)
273 } else {
274 // Fall back to the regular float-based validation since precision is already lost
275 let multiple_f64 = self.multiple_of.to_f64().unwrap_or(f64::INFINITY);
276 numeric::is_multiple_of_float(item, multiple_f64)
277 }
278 } else {
279 true
280 }
281 }
282
283 fn validate<'i>(
284 &self,
285 instance: &'i Value,
286 location: &LazyLocation,
287 ctx: &mut ValidationContext,
288 ) -> Result<(), ValidationError<'i>> {
289 if !self.is_valid(instance, ctx) {
290 return Err(ValidationError::multiple_of(
291 self.location.clone(),
292 location.into(),
293 instance,
294 Value::Number(self.original_value.clone()),
295 ));
296 }
297 Ok(())
298 }
299 }
300
301 #[inline]
302 pub(crate) fn compile<'a>(
303 ctx: &compiler::Context,
304 _: &'a Map<String, Value>,
305 schema: &'a Value,
306 ) -> Option<CompilationResult<'a>> {
307 if let Value::Number(multiple_of) = schema {
308 let location = ctx.location().join("multipleOf");
309
310 #[cfg(feature = "arbitrary-precision")]
311 {
312 // Try BigInt first for large integers
313 if let Some(bigint_multiple) = numeric::bignum::try_parse_bigint(multiple_of) {
314 return Some(MultipleOfBigIntValidator::compile(
315 bigint_multiple,
316 multiple_of,
317 location,
318 ));
319 }
320 // Then try BigFraction for exact decimal precision
321 if let Some(bigfrac_multiple) = numeric::bignum::try_parse_bigfraction(multiple_of) {
322 return Some(MultipleOfBigFracValidator::compile(
323 bigfrac_multiple,
324 multiple_of,
325 location,
326 ));
327 }
328 }
329
330 // If not BigInt or BigFraction, try to represent as f64
331 // Note: Extreme scientific notation (e.g., 1e309, 1e400) cannot fit in f64
332 if let Some(f64_value) = multiple_of.as_f64() {
333 #[cfg(feature = "arbitrary-precision")]
334 {
335 if f64_value.fract() == 0. {
336 Some(MultipleOfIntegerValidator::compile(
337 f64_value,
338 multiple_of,
339 location,
340 ))
341 } else {
342 Some(MultipleOfFloatValidator::compile(
343 f64_value,
344 multiple_of,
345 location,
346 ))
347 }
348 }
349 #[cfg(not(feature = "arbitrary-precision"))]
350 {
351 if f64_value.fract() == 0. {
352 Some(MultipleOfIntegerValidator::compile(f64_value, location))
353 } else {
354 Some(MultipleOfFloatValidator::compile(f64_value, location))
355 }
356 }
357 } else {
358 // Extreme numbers beyond f64 range (e.g., 1e309) - not supported
359 // Skip this keyword - all instances will be considered valid
360 None
361 }
362 } else {
363 Some(Err(ValidationError::single_type_error(
364 Location::new(),
365 ctx.location().clone(),
366 schema,
367 JsonType::Number,
368 )))
369 }
370 }
371
372 #[cfg(test)]
373 mod tests {
374 use crate::tests_util;
375 use serde_json::{json, Value};
376 use test_case::test_case;
377
378 #[test_case(&json!({"multipleOf": 2}), &json!(4))]
379 #[test_case(&json!({"multipleOf": 1.0}), &json!(4.0))]
380 #[test_case(&json!({"multipleOf": 1.5}), &json!(3.0))]
381 #[test_case(&json!({"multipleOf": 1.5}), &json!(4.5))]
382 #[test_case(&json!({"multipleOf": 0.1}), &json!(12.2))]
383 #[test_case(&json!({"multipleOf": 0.0001}), &json!(3.1254))]
384 #[test_case(&json!({"multipleOf": 0.0001}), &json!(47.498))]
385 #[test_case(&json!({"multipleOf": 0.1}), &json!(1.1))]
386 #[test_case(&json!({"multipleOf": 0.1}), &json!(1.2))]
387 #[test_case(&json!({"multipleOf": 0.1}), &json!(1.3))]
388 #[test_case(&json!({"multipleOf": 0.02}), &json!(1.02))]
389 #[test_case(&json!({"multipleOf": 1e-16}), &json!(1e-15))]
390 fn multiple_of_is_valid(schema: &Value, instance: &Value) {
391 tests_util::is_valid(schema, instance);
392 }
393
394 #[test_case(&json!({"multipleOf": 1.0}), &json!(4.5))]
395 #[test_case(&json!({"multipleOf": 0.1}), &json!(4.55))]
396 #[test_case(&json!({"multipleOf": 0.2}), &json!(4.5))]
397 #[test_case(&json!({"multipleOf": 0.02}), &json!(1.01))]
398 #[test_case(&json!({"multipleOf": 1.3}), &json!(1.3e-16))]
399 fn multiple_of_is_not_valid(schema: &Value, instance: &Value) {
400 tests_util::is_not_valid(schema, instance);
401 }
402
403 #[test_case(&json!({"multipleOf": 2}), &json!(3), "/multipleOf")]
404 #[test_case(&json!({"multipleOf": 1.5}), &json!(5), "/multipleOf")]
405 fn location(schema: &Value, instance: &Value, expected: &str) {
406 tests_util::assert_schema_location(schema, instance, expected);
407 }
408
409 #[cfg(feature = "arbitrary-precision")]
410 mod arbitrary_precision {
411 use crate::tests_util;
412 use serde_json::Value;
413 use test_case::test_case;
414
415 fn parse_json(s: &str) -> Value {
416 serde_json::from_str(s).unwrap()
417 }
418
419 #[test_case(r#"{"multipleOf": 0.1}"#, "0.3", true; "bigfrac 0.3 is multiple of 0.1")]
420 #[test_case(r#"{"multipleOf": 0.1}"#, "0.35", false; "bigfrac 0.35 not multiple of 0.1")]
421 #[test_case(r#"{"multipleOf": 0.01}"#, "0.03", true; "bigfrac 0.03 is multiple of 0.01")]
422 #[test_case(r#"{"multipleOf": 0.1}"#, "10", true; "bigfrac validator with u64 instance")]
423 #[test_case(r#"{"multipleOf": 0.5}"#, "-2", true; "bigfrac validator with i64 instance")]
424 #[test_case(r#"{"multipleOf": 0.3}"#, "0.0", true; "bigfrac zero is multiple")]
425 #[test_case(r#"{"multipleOf": 0.1}"#, "10.05", false; "bigfrac 10.05 not multiple of 0.1")]
426 #[test_case(r#"{"multipleOf": 0.5}"#, "-1.3", false; "bigfrac -1.3 not multiple of 0.5")]
427 #[test_case(r#"{"minimum": 0.1}"#, "0.3", true; "bigfrac minimum 0.3 >= 0.1")]
428 #[test_case(r#"{"minimum": 0.1}"#, "0.05", false; "bigfrac minimum 0.05 < 0.1")]
429 #[test_case(r#"{"maximum": 0.5}"#, "0.3", true; "bigfrac maximum 0.3 <= 0.5")]
430 #[test_case(r#"{"maximum": 0.5}"#, "0.7", false; "bigfrac maximum 0.7 > 0.5")]
431 #[test_case(r#"{"multipleOf": 2}"#, "1e1000", true; "huge scientific integer is even")]
432 #[test_case(r#"{"multipleOf": 3}"#, "1e1000", false; "10^1000 not multiple of 3")]
433 #[test_case(r#"{"multipleOf": 0.5}"#, "1e1000", true; "huge scientific integer multiple of 0.5")]
434 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "36893488147419103232", true; "large bigint multiple")]
435 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "18446744073709551617", false; "large bigint non-multiple")]
436 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "100", false; "small int not multiple of large")]
437 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "-100", false; "negative small int not multiple")]
438 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "0", true; "zero is multiple of large bigint")]
439 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "1.5", false; "decimal not multiple of bigint")]
440 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "1e20", false; "scientific notation not multiple")]
441 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "9223372036854775808", false; "2^63 not multiple of 2^64")]
442 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "1e308", true; "huge float multiple of 2^64")]
443 #[test_case(r#"{"multipleOf": 1e19}"#, "100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", true; "1e19 scientific notation divisor with huge instance that is exact multiple")]
444 #[test_case(r#"{"multipleOf": 2e19}"#, "200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", true; "2e19 scientific notation divisor with huge instance that is exact multiple")]
445 #[test_case(r#"{"multipleOf": 1000000000000000000}"#, "1e18", true; "1e18 is 10^18")]
446 #[test_case(r#"{"multipleOf": 1000000000000000000}"#, "2e18", true; "2e18 is 2*10^18")]
447 #[test_case(r#"{"multipleOf": 1000000000000000000}"#, "1.5e18", false; "1.5e18 not integer multiple")]
448 #[test_case(r#"{"multipleOf": 3}"#, "1e20", false; "1e20 not multiple of 3")]
449 #[test_case(r#"{"minimum": 18446744073709551616}"#, "1e100", true; "bigint min with huge number")]
450 #[test_case(r#"{"maximum": 18446744073709551616}"#, "1e100", false; "bigint max with huge number")]
451 #[test_case(r#"{"multipleOf": 1e309}"#, "1e309", true; "extreme schema value beyond f64 - schema compilation should handle")]
452 #[test_case(r#"{"multipleOf": 1e400}"#, "2e400", true; "extreme schema values - both unsupported")]
453 #[test_case(r#"{"minimum": 1e309}"#, "1e310", true; "extreme minimum - instance validation")]
454 #[test_case(r#"{"maximum": 1e309}"#, "1e308", true; "extreme maximum - instance validation")]
455 #[test_case(r#"{"exclusiveMinimum": 18446744073709551616}"#, "1e100", true; "bigint exclusive min")]
456 #[test_case(r#"{"exclusiveMaximum": 18446744073709551616}"#, "1e10", true; "bigint exclusive max")]
457 #[test_case(r#"{"minimum": 18446744073709551616}"#, "100", false; "small number below bigint min")]
458 #[test_case(r#"{"maximum": 18446744073709551616}"#, "99999999999999999999", false; "large number above bigint max")]
459 fn arbitrary_precision_validation(schema_json: &str, instance_json: &str, expected: bool) {
460 let schema = parse_json(schema_json);
461 let instance = parse_json(instance_json);
462 if expected {
463 tests_util::is_valid(&schema, &instance);
464 } else {
465 tests_util::is_not_valid(&schema, &instance);
466 }
467 }
468
469 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "4.0", false; "safe_float_not_multiple")]
470 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "0.0", true; "safe_float_zero_multiple")]
471 #[test_case(
472 r#"{"multipleOf": 18446744073709551616}"#,
473 "18014398509481984.0",
474 false;
475 "float_beyond_safe_range_rejected"
476 )]
477 fn bigint_validator_float_instances(
478 schema_json: &str,
479 instance_json: &str,
480 expected: bool,
481 ) {
482 let schema = parse_json(schema_json);
483 let instance = parse_json(instance_json);
484 if expected {
485 tests_util::is_valid(&schema, &instance);
486 } else {
487 tests_util::is_not_valid(&schema, &instance);
488 }
489 }
490
491 #[test]
492 fn bigint_validator_accepts_bigfraction_integer_instances() {
493 let schema = parse_json(r#"{"multipleOf": 18446744073709551616}"#);
494 let mut huge = "18446744073709551616".to_owned();
495 huge.push_str(&"0".repeat(400));
496 huge.push_str(".0");
497 let instance = parse_json(&huge);
498 tests_util::is_valid(&schema, &instance);
499 }
500
501 // Test cases for dynamically generated huge numbers
502 #[test_case("2", 100, 2, true; "2 followed by 100 zeros is multiple of 2")]
503 #[test_case("2", 200, 3, false; "2 followed by 200 zeros not multiple of 3")]
504 #[test_case("1", 100, 3, false; "1 followed by 100 zeros not multiple of 3")]
505 #[test_case("3", 400, 7, false; "3 followed by 400 zeros not multiple of 7")]
506 #[test_case("2", 100, 2, true; "huge bigint is multiple")]
507 fn generated_huge_numbers(prefix: &str, zeros: usize, divisor: u64, expected: bool) {
508 let schema_json = format!(r#"{{"multipleOf": {divisor}}}"#);
509 let schema = parse_json(&schema_json);
510 let huge = prefix.to_string() + &"0".repeat(zeros);
511 let instance = parse_json(&huge);
512 if expected {
513 tests_util::is_valid(&schema, &instance);
514 } else {
515 tests_util::is_not_valid(&schema, &instance);
516 }
517 }
518
519 // Test non-number instances with MultipleOfFloatValidator
520 // Use serde_json::json! macro to avoid arbitrary-precision parsing
521 #[test_case(&serde_json::json!({"multipleOf": 2.5}), &serde_json::json!("string"); "float string")]
522 #[test_case(&serde_json::json!({"multipleOf": 2.5}), &serde_json::json!(true); "float bool")]
523 #[test_case(&serde_json::json!({"multipleOf": 2.5}), &serde_json::json!([]); "float array")]
524 #[test_case(&serde_json::json!({"multipleOf": 2.5}), &serde_json::json!({}); "float object")]
525 fn non_number_with_float_validator(schema: &Value, instance: &Value) {
526 tests_util::is_valid(schema, instance);
527 }
528
529 // Test non-number instances with BigInt/BigFrac validators (via JSON parsing)
530 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, r#""string""#; "bigint string")]
531 #[test_case(r#"{"multipleOf": 0.1}"#, r"true"; "bigfrac bool")]
532 #[test_case(r#"{"minimum": 18446744073709551616}"#, r"[]"; "bigint min array")]
533 fn non_number_with_bignum_validators(schema_json: &str, instance_json: &str) {
534 let schema = parse_json(schema_json);
535 let instance = parse_json(instance_json);
536 tests_util::is_valid(&schema, &instance);
537 }
538
539 // Test validate() error paths
540 #[test_case(r#"{"multipleOf": 2.5}"#, "5.1"; "float validator error")]
541 #[test_case(r#"{"multipleOf": 3}"#, "7"; "integer validator error")]
542 #[test_case(r#"{"multipleOf": 0.3}"#, "0.7"; "bigfrac validator error")]
543 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "123"; "bigint validator error")]
544 #[test_case(r#"{"minimum": 18446744073709551616}"#, "100"; "bigint minimum error")]
545 #[test_case(r#"{"maximum": 18446744073709551616}"#, "99999999999999999999"; "bigint maximum error")]
546 fn validator_error_paths(schema_json: &str, instance_json: &str) {
547 let schema = parse_json(schema_json);
548 let validator = crate::validator_for(&schema).unwrap();
549 let instance = parse_json(instance_json);
550 assert!(validator.validate(&instance).is_err());
551 }
552
553 #[test]
554 fn invalid_multiple_of_schema() {
555 let schema = parse_json(r#"{"multipleOf": "not a number"}"#);
556 assert!(crate::validator_for(&schema).is_err());
557 }
558
559 #[test]
560 fn huge_bigfraction_beyond_f64() {
561 // Test BigFraction parsing for numbers way beyond f64::MAX
562 let schema = parse_json(r#"{"multipleOf": 7}"#);
563 // Number not divisible by 7 (sum of digits = 3, not divisible by 7)
564 let huge = "3".to_string() + &"0".repeat(400);
565 let instance = parse_json(&huge);
566 tests_util::is_not_valid(&schema, &instance);
567 }
568
569 #[test]
570 fn large_u64_divisor_beyond_i64_max() {
571 // Test for the bug where u64 divisors beyond i64::MAX were incorrectly handled
572 // 10^19 = 10000000000000000000 is beyond i64::MAX but within u64::MAX
573 // This should now use BigIntValidator to preserve exact value
574 let schema = parse_json(r#"{"multipleOf": 10000000000000000000}"#);
575
576 // 10^320 is a valid multiple of 10^19 (it's 10^19 * 10^301)
577 let huge_multiple = "1".to_string() + &"0".repeat(320);
578 let instance_valid = parse_json(&huge_multiple);
579 tests_util::is_valid(&schema, &instance_valid);
580
581 // 10^320 + 1 is NOT a multiple of 10^19
582 let mut huge_non_multiple = "1".to_string() + &"0".repeat(320);
583 huge_non_multiple = huge_non_multiple[..huge_non_multiple.len() - 1].to_string() + "1";
584 let instance_invalid = parse_json(&huge_non_multiple);
585 tests_util::is_not_valid(&schema, &instance_invalid);
586 }
587
588 #[test]
589 fn u64_max_divisor() {
590 // Test with exactly u64::MAX as divisor
591 let schema = parse_json(r#"{"multipleOf": 18446744073709551615}"#);
592
593 // u64::MAX * 2 (expressed as a large number)
594 let double = "36893488147419103230";
595 let instance = parse_json(double);
596 tests_util::is_valid(&schema, &instance);
597 }
598
599 #[test]
600 fn huge_decimal_beyond_f64_but_parseable() {
601 // This number is so large it overflows f64 to infinity (as_f64() returns None)
602 // but can still be parsed as BigFraction - hits MultipleOfBigIntValidator else branch
603 let divisor = "18446744073709551616"; // 2^64
604 let schema_json = format!(r#"{{"multipleOf": {divisor}}}"#);
605 let schema = parse_json(&schema_json);
606
607 // Valid: 2^64 * 10^400 - parseable as BigFraction
608 let huge = divisor.to_string() + &"0".repeat(400) + ".0";
609 let instance = parse_json(&huge);
610 tests_util::is_valid(&schema, &instance);
611
612 // Invalid: all 1s - parseable as BigFraction but not a multiple
613 let huge_non_multiple = "1".repeat(400) + ".0";
614 let instance = parse_json(&huge_non_multiple);
615 tests_util::is_not_valid(&schema, &instance);
616 }
617
618 #[test]
619 fn huge_scientific_notation_else_branch_unparsable() {
620 // This hits the else branch but CANNOT be parsed as BigFraction (scientific notation)
621 // as_f64() returns None (infinity), try_parse_bigfraction returns None (has exponent)
622 let divisor = "18446744073709551616"; // 2^64
623 let schema_json = format!(r#"{{"multipleOf": {divisor}}}"#);
624 let schema = parse_json(&schema_json);
625
626 // Scientific notation that overflows to infinity - cannot be parsed as BigFraction
627 let infinity_positive = parse_json("1e1000001");
628 tests_util::is_not_valid(&schema, &infinity_positive);
629
630 let infinity_negative = parse_json("-1e1000001");
631 tests_util::is_not_valid(&schema, &infinity_negative);
632
633 // Huge scientific notation decimal (not just integer exponent)
634 let huge_scientific = parse_json("1.5e1000002");
635 tests_util::is_not_valid(&schema, &huge_scientific);
636 }
637
638 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "1.0", false; "bigint divisor small float")]
639 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "2.0", false; "bigint divisor 2.0")]
640 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "1000.0", false; "bigint divisor 1000.0")]
641 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "18446744073709551616", true; "bigint schema u64 instance")]
642 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "-18446744073709551616", true; "bigint schema neg i64")]
643 #[test_case(r#"{"minimum": 18446744073709551616}"#, "18446744073709551616", true; "bigint min u64 exact")]
644 #[test_case(r#"{"maximum": 18446744073709551616}"#, "0", true; "bigint max small i64")]
645 #[test_case(r#"{"exclusiveMinimum": 9223372036854775807}"#, "9223372036854775808", true; "i64_max exclusive u64")]
646 #[test_case(r#"{"minimum": 0.1}"#, "18446744073709551616", true; "bigfrac schema large int")]
647 #[test_case(r#"{"multipleOf": 0.5}"#, "36893488147419103232", true; "bigfrac schema huge int")]
648 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "9223372036854775808", false; "bigint half value")]
649 #[test_case(r#"{"minimum": 18446744073709551616}"#, "9223372036854775807", false; "bigint min i64_max")]
650 #[test_case(r#"{"maximum": 9223372036854775807}"#, "18446744073709551616", false; "i64_max max u64")]
651 #[test_case(r#"{"exclusiveMaximum": 18446744073709551616}"#, "18446744073709551616", false; "bigint exmax boundary")]
652 #[test_case(r#"{"multipleOf": 0.3}"#, "18446744073709551616", false; "bigfrac 0.3 large int")]
653 fn mixed_type_comparisons(schema_json: &str, instance_json: &str, expected: bool) {
654 let schema = parse_json(schema_json);
655 let instance = parse_json(instance_json);
656 if expected {
657 tests_util::is_valid(&schema, &instance);
658 } else {
659 tests_util::is_not_valid(&schema, &instance);
660 }
661 }
662
663 #[test_case(r#"{"multipleOf": 0.1}"#, "0.1", true; "0.1 multiple 0.1")]
664 #[test_case(r#"{"multipleOf": 0.1}"#, "0.2", true; "0.2 multiple 0.1")]
665 #[test_case(r#"{"multipleOf": 0.1}"#, "0.7", true; "0.7 multiple 0.1")]
666 #[test_case(r#"{"multipleOf": 0.1}"#, "0.11", false; "0.11 not multiple 0.1")]
667 #[test_case(r#"{"multipleOf": 0.01}"#, "0.99", true; "0.99 multiple 0.01")]
668 #[test_case(r#"{"multipleOf": 0.01}"#, "0.999", false; "0.999 not multiple 0.01")]
669 #[test_case(r#"{"multipleOf": 0.001}"#, "1.234", true; "1.234 multiple 0.001")]
670 #[test_case(r#"{"multipleOf": 0.001}"#, "1.2345", false; "1.2345 not multiple 0.001")]
671 #[test_case(r#"{"minimum": 0.1}"#, "0.1", true; "min 0.1 equal")]
672 #[test_case(r#"{"maximum": 0.3}"#, "0.3", true; "max 0.3 equal")]
673 #[test_case(r#"{"minimum": 0.1}"#, "0.09999999999999999", false; "below 0.1")]
674 #[test_case(r#"{"maximum": 0.1}"#, "0.10000000000000001", false; "above 0.1")]
675 #[test_case(r#"{"const": 0.3}"#, "0.3", true; "const 0.3")]
676 #[test_case(r#"{"const": 0.1}"#, "0.10000000000000002", false; "const close not equal")]
677 #[test_case(r#"{"minimum": 1.0}"#, "1", true; "decimal min int equal")]
678 #[test_case(r#"{"minimum": 1.5}"#, "1", false; "decimal min int below")]
679 #[test_case(r#"{"minimum": 1.5}"#, "2", true; "decimal min int above")]
680 #[test_case(r#"{"maximum": 2.5}"#, "2", true; "decimal max int below")]
681 #[test_case(r#"{"maximum": 2.5}"#, "3", false; "decimal max int above")]
682 #[test_case(r#"{"multipleOf": 0.5}"#, "1", true; "0.5 multiple int 1")]
683 #[test_case(r#"{"multipleOf": 0.5}"#, "2", true; "0.5 multiple int 2")]
684 #[test_case(r#"{"multipleOf": 0.5}"#, "3", true; "0.5 multiple int 3")]
685 #[test_case(r#"{"multipleOf": 0.3}"#, "1", false; "0.3 not multiple int 1")]
686 fn decimal_precision_tests(schema_json: &str, instance_json: &str, expected: bool) {
687 let schema = parse_json(schema_json);
688 let instance = parse_json(instance_json);
689 if expected {
690 tests_util::is_valid(&schema, &instance);
691 } else {
692 tests_util::is_not_valid(&schema, &instance);
693 }
694 }
695
696 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "-36893488147419103232", true; "pos bigint neg instance")]
697 #[test_case(r#"{"minimum": -18446744073709551616}"#, "-18446744073709551615", true; "neg bigint minimum")]
698 #[test_case(r#"{"maximum": -18446744073709551616}"#, "-18446744073709551617", true; "neg bigint maximum")]
699 #[test_case(r#"{"minimum": -99999999999999999999}"#, "-99999999999999999998", true; "huge neg minimum")]
700 #[test_case(r#"{"maximum": -99999999999999999999}"#, "-100000000000000000000", true; "huge neg maximum")]
701 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "0", true; "zero multiple large")]
702 #[test_case(r#"{"minimum": -18446744073709551616}"#, "0", true; "zero above neg min")]
703 #[test_case(r#"{"maximum": 18446744073709551616}"#, "0", true; "zero below large max")]
704 fn negative_large_numbers(schema_json: &str, instance_json: &str, expected: bool) {
705 let schema = parse_json(schema_json);
706 let instance = parse_json(instance_json);
707 if expected {
708 tests_util::is_valid(&schema, &instance);
709 } else {
710 tests_util::is_not_valid(&schema, &instance);
711 }
712 }
713
714 #[test_case(r#"{"minimum": 9223372036854775807}"#, "9223372036854775807", true; "i64_max min equal")]
715 #[test_case(r#"{"minimum": 9223372036854775807}"#, "9223372036854775808", true; "i64_max min plus")]
716 #[test_case(r#"{"maximum": 18446744073709551615}"#, "18446744073709551615", true; "u64_max equal")]
717 #[test_case(r#"{"maximum": 18446744073709551615}"#, "18446744073709551616", false; "u64_max plus")]
718 #[test_case(r#"{"maximum": -9223372036854775808}"#, "-9223372036854775808", true; "i64_min equal")]
719 #[test_case(r#"{"maximum": -9223372036854775808}"#, "-9223372036854775809", true; "i64_min minus")]
720 fn boundary_conditions(schema_json: &str, instance_json: &str, expected: bool) {
721 let schema = parse_json(schema_json);
722 let instance = parse_json(instance_json);
723 if expected {
724 tests_util::is_valid(&schema, &instance);
725 } else {
726 tests_util::is_not_valid(&schema, &instance);
727 }
728 }
729
730 #[test_case(r#"{"minimum": 99999999999999999999}"#, "99999999999999999999", true; "min equal")]
731 #[test_case(r#"{"minimum": 99999999999999999999}"#, "100000000000000000000", true; "min greater")]
732 #[test_case(r#"{"minimum": 99999999999999999999}"#, "99999999999999999998", false; "min less")]
733 #[test_case(r#"{"maximum": 99999999999999999999}"#, "99999999999999999999", true; "max equal")]
734 #[test_case(r#"{"maximum": 99999999999999999999}"#, "100000000000000000000", false; "max greater")]
735 #[test_case(r#"{"maximum": 99999999999999999999}"#, "99999999999999999998", true; "max less")]
736 #[test_case(r#"{"exclusiveMinimum": 99999999999999999999}"#, "99999999999999999999", false; "exmin equal")]
737 #[test_case(r#"{"exclusiveMinimum": 99999999999999999999}"#, "100000000000000000000", true; "exmin greater")]
738 #[test_case(r#"{"exclusiveMinimum": 99999999999999999999}"#, "99999999999999999998", false; "exmin less")]
739 #[test_case(r#"{"exclusiveMaximum": 99999999999999999999}"#, "99999999999999999999", false; "exmax equal")]
740 #[test_case(r#"{"exclusiveMaximum": 99999999999999999999}"#, "100000000000000000000", false; "exmax greater")]
741 #[test_case(r#"{"exclusiveMaximum": 99999999999999999999}"#, "99999999999999999998", true; "exmax less")]
742 #[test_case(r#"{"minimum": 0.5}"#, "0.5", true; "frac min equal")]
743 #[test_case(r#"{"minimum": 0.5}"#, "0.6", true; "frac min greater")]
744 #[test_case(r#"{"minimum": 0.5}"#, "0.4", false; "frac min less")]
745 #[test_case(r#"{"maximum": 0.5}"#, "0.5", true; "frac max equal")]
746 #[test_case(r#"{"maximum": 0.5}"#, "0.6", false; "frac max greater")]
747 #[test_case(r#"{"maximum": 0.5}"#, "0.4", true; "frac max less")]
748 fn comparison_operators(schema_json: &str, instance_json: &str, expected: bool) {
749 let schema = parse_json(schema_json);
750 let instance = parse_json(instance_json);
751 if expected {
752 tests_util::is_valid(&schema, &instance);
753 } else {
754 tests_util::is_not_valid(&schema, &instance);
755 }
756 }
757
758 #[test_case(r#"{"const": 18446744073709551616}"#, "18446744073709551616", true; "bigint match")]
759 #[test_case(r#"{"const": 18446744073709551616}"#, "18446744073709551617", false; "bigint mismatch")]
760 #[test_case(r#"{"const": 0.1}"#, "0.1", true; "decimal match")]
761 #[test_case(r#"{"const": 0.1}"#, "0.10", true; "decimal trailing zero")]
762 #[test_case(r#"{"const": 0.1}"#, "0.2", false; "decimal mismatch")]
763 #[test_case(r#"{"const": -99999999999999999999}"#, "-99999999999999999999", true; "neg bigint")]
764 fn const_large_numbers(schema_json: &str, instance_json: &str, expected: bool) {
765 let schema = parse_json(schema_json);
766 let instance = parse_json(instance_json);
767 if expected {
768 tests_util::is_valid(&schema, &instance);
769 } else {
770 tests_util::is_not_valid(&schema, &instance);
771 }
772 }
773
774 #[test_case(r#"{"multipleOf": "not a number"}"#; "string")]
775 #[test_case(r#"{"multipleOf": null}"#; "null")]
776 #[test_case(r#"{"multipleOf": []}"#; "array")]
777 #[test_case(r#"{"multipleOf": {}}"#; "object")]
778 #[test_case(r#"{"multipleOf": true}"#; "boolean")]
779 fn malformed_schema_errors(schema_json: &str) {
780 let schema = parse_json(schema_json);
781 assert!(crate::validator_for(&schema).is_err());
782 }
783
784 #[test_case(r#"{"multipleOf": 18446744073709551616}"#, "18446744073709551617"; "multiple_of bigint")]
785 #[test_case(r#"{"minimum": 99999999999999999999}"#, "99999999999999999998"; "minimum bigint")]
786 #[test_case(r#"{"multipleOf": 0.1}"#, "0.15"; "multiple_of decimal")]
787 fn error_messages_preserve_precision(schema_json: &str, instance_json: &str) {
788 let schema = parse_json(schema_json);
789 let instance = parse_json(instance_json);
790 let validator = crate::validator_for(&schema).unwrap();
791 assert!(validator.validate(&instance).is_err());
792 }
793
794 #[test_case(r#"{"multipleOf": 0.0000000000000001}"#; "tiny decimal")]
795 #[test_case(r#"{"multipleOf": 999999999999999999999999999999}"#; "huge integer")]
796 #[test_case(r#"{"minimum": -999999999999999999999999999999}"#; "huge negative")]
797 #[test_case(r#"{"maximum": 999999999999999999999999999999}"#; "huge positive")]
798 #[test_case(r#"{"multipleOf": 1e-100}"#; "tiny scientific")]
799 #[test_case(r#"{"minimum": 1e308}"#; "near f64_max")]
800 #[test_case(r#"{"maximum": -1e308}"#; "near f64_min")]
801 fn schema_compilation_edge_cases(schema_json: &str) {
802 let schema = parse_json(schema_json);
803 assert!(crate::validator_for(&schema).is_ok());
804 }
805 }
806 }