| 1 | use crate::{ |
| 2 | compiler, |
| 3 | error::ValidationError, |
| 4 | ext::numeric, |
| 5 | keywords::CompilationResult, |
| 6 | paths::{LazyLocation, Location}, |
| 7 | thread::ThreadBound, |
| 8 | types::JsonType, |
| 9 | validator::{Validate, ValidationContext}, |
| 10 | }; |
| 11 | use num_cmp::NumCmp; |
| 12 | use serde_json::{Map, Value}; |
| 13 | |
| 14 | macro_rules! define_numeric_keywords { |
| 15 | ($($struct_name:ident => $fn_name:path => $error_fn_name:ident),* $(,)?) => { |
| 16 | $( |
| 17 | #[derive(Debug, Clone, PartialEq)] |
| 18 | pub(crate) struct $struct_name<T> { |
| 19 | pub(super) limit: T, |
| 20 | limit_val: Value, |
| 21 | location: Location, |
| 22 | } |
| 23 | |
| 24 | impl<T> From<(T, Value, Location)> for $struct_name<T> { |
| 25 | fn from((limit, limit_val, location): (T, Value, Location)) -> Self { |
| 26 | Self { limit, limit_val, location } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | impl<T> Validate for $struct_name<T> |
| 31 | where |
| 32 | T: Copy + ThreadBound + num_traits::ToPrimitive, |
| 33 | u64: NumCmp<T>, |
| 34 | i64: NumCmp<T>, |
| 35 | f64: NumCmp<T>, |
| 36 | { |
| 37 | fn validate<'i>( |
| 38 | &self, |
| 39 | instance: &'i Value, |
| 40 | location: &LazyLocation, |
| 41 | ctx: &mut ValidationContext, |
| 42 | ) -> Result<(), ValidationError<'i>> { |
| 43 | if self.is_valid(instance, ctx) { |
| 44 | Ok(()) |
| 45 | } else { |
| 46 | Err(ValidationError::$error_fn_name( |
| 47 | self.location.clone(), |
| 48 | location.into(), |
| 49 | instance, |
| 50 | self.limit_val.clone(), |
| 51 | )) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool { |
| 56 | if let Value::Number(item) = instance { |
| 57 | $fn_name(item, self.limit) |
| 58 | } else { |
| 59 | true |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | )* |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | define_numeric_keywords!( |
| 68 | Minimum => numeric::ge => minimum, |
| 69 | Maximum => numeric::le => maximum, |
| 70 | ExclusiveMinimum => numeric::gt => exclusive_minimum, |
| 71 | ExclusiveMaximum => numeric::lt => exclusive_maximum, |
| 72 | ); |
| 73 | |
| 74 | #[cfg(feature = "arbitrary-precision")] |
| 75 | pub(crate) mod bigint_validators { |
| 76 | use super::{ |
| 77 | numeric, LazyLocation, Location, Validate, ValidationContext, ValidationError, Value, |
| 78 | }; |
| 79 | use crate::ext::numeric::bignum::{ |
| 80 | f64_ge_bigfrac, f64_ge_bigint, f64_gt_bigfrac, f64_gt_bigint, f64_le_bigfrac, |
| 81 | f64_le_bigint, f64_lt_bigfrac, f64_lt_bigint, i64_ge_bigfrac, i64_ge_bigint, |
| 82 | i64_gt_bigfrac, i64_gt_bigint, i64_le_bigfrac, i64_le_bigint, i64_lt_bigfrac, |
| 83 | i64_lt_bigint, try_parse_bigfraction, u64_ge_bigfrac, u64_ge_bigint, u64_gt_bigfrac, |
| 84 | u64_gt_bigint, u64_le_bigfrac, u64_le_bigint, u64_lt_bigfrac, u64_lt_bigint, |
| 85 | }; |
| 86 | use num_bigint::BigInt; |
| 87 | |
| 88 | macro_rules! define_bigint_validator { |
| 89 | ($struct_name:ident, $u64_cmp:path, $i64_cmp:path, $f64_cmp:path, $bigint_op:tt, $error_fn:ident, $infinity_fn:expr) => { |
| 90 | #[derive(Debug, Clone, PartialEq)] |
| 91 | pub(crate) struct $struct_name { |
| 92 | pub(super) limit: BigInt, |
| 93 | pub(super) limit_val: Value, |
| 94 | pub(super) location: Location, |
| 95 | } |
| 96 | |
| 97 | impl $struct_name { |
| 98 | pub(crate) fn new(limit: BigInt, limit_val: Value, location: Location) -> Self { |
| 99 | Self { limit, limit_val, location } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | impl Validate for $struct_name { |
| 104 | fn validate<'i>( |
| 105 | &self, |
| 106 | instance: &'i Value, |
| 107 | location: &LazyLocation, |
| 108 | ctx: &mut ValidationContext, |
| 109 | ) -> Result<(), ValidationError<'i>> { |
| 110 | if self.is_valid(instance, ctx) { |
| 111 | Ok(()) |
| 112 | } else { |
| 113 | Err(ValidationError::$error_fn( |
| 114 | self.location.clone(), |
| 115 | location.into(), |
| 116 | instance, |
| 117 | self.limit_val.clone(), |
| 118 | )) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool { |
| 123 | use fraction::BigFraction; |
| 124 | if let Value::Number(item) = instance { |
| 125 | // Try to parse instance as BigInt first |
| 126 | if let Some(instance_bigint) = numeric::bignum::try_parse_bigint(item) { |
| 127 | // Both are BigInt - direct comparison |
| 128 | instance_bigint $bigint_op self.limit |
| 129 | } else if let Some(v) = item.as_u64() { |
| 130 | $u64_cmp(v, &self.limit) |
| 131 | } else if let Some(v) = item.as_i64() { |
| 132 | $i64_cmp(v, &self.limit) |
| 133 | } else if let Some(v) = item.as_f64() { |
| 134 | $f64_cmp(v, &self.limit) |
| 135 | } else { |
| 136 | // Number doesn't fit in f64 (e.g., 1e1000) |
| 137 | // Since limit is BigInt, we need to compare with BigFraction |
| 138 | if let Some(instance_bigfrac) = numeric::bignum::try_parse_bigfraction(item) { |
| 139 | // Convert BigInt limit to BigFraction for comparison |
| 140 | // Use clone to avoid truncation through i128 |
| 141 | let limit_frac = BigFraction::from(self.limit.clone()); |
| 142 | instance_bigfrac $bigint_op limit_frac |
| 143 | } else { |
| 144 | // Can't parse as BigFraction - extremely large scientific notation |
| 145 | // (e.g., "1e10000" which exceeds BigFraction's parsing capability) |
| 146 | // These behave like positive/negative infinity for comparison purposes |
| 147 | let is_negative = item.as_str().starts_with('-'); |
| 148 | $infinity_fn(is_negative) |
| 149 | } |
| 150 | } |
| 151 | } else { |
| 152 | true |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | }; |
| 157 | } |
| 158 | |
| 159 | define_bigint_validator!( |
| 160 | BigIntMinimum, |
| 161 | u64_ge_bigint, |
| 162 | i64_ge_bigint, |
| 163 | f64_ge_bigint, |
| 164 | >=, |
| 165 | minimum, |
| 166 | |is_negative: bool| !is_negative // For >= and >, positive infinity passes |
| 167 | ); |
| 168 | define_bigint_validator!( |
| 169 | BigIntMaximum, |
| 170 | u64_le_bigint, |
| 171 | i64_le_bigint, |
| 172 | f64_le_bigint, |
| 173 | <=, |
| 174 | maximum, |
| 175 | |is_negative: bool| is_negative // For <= and <, negative infinity passes |
| 176 | ); |
| 177 | define_bigint_validator!( |
| 178 | BigIntExclusiveMinimum, |
| 179 | u64_gt_bigint, |
| 180 | i64_gt_bigint, |
| 181 | f64_gt_bigint, |
| 182 | >, |
| 183 | exclusive_minimum, |
| 184 | |is_negative: bool| !is_negative // For >= and >, positive infinity passes |
| 185 | ); |
| 186 | define_bigint_validator!( |
| 187 | BigIntExclusiveMaximum, |
| 188 | u64_lt_bigint, |
| 189 | i64_lt_bigint, |
| 190 | f64_lt_bigint, |
| 191 | <, |
| 192 | exclusive_maximum, |
| 193 | |is_negative: bool| is_negative // For <= and <, negative infinity passes |
| 194 | ); |
| 195 | |
| 196 | // BigFraction validators for arbitrary precision floats |
| 197 | use fraction::BigFraction; |
| 198 | |
| 199 | macro_rules! define_bigfrac_validator { |
| 200 | ($struct_name:ident, $u64_cmp:path, $i64_cmp:path, $f64_cmp:path, $bigfrac_op:tt, $error_fn:ident) => { |
| 201 | #[derive(Debug, Clone, PartialEq)] |
| 202 | pub(crate) struct $struct_name { |
| 203 | pub(super) limit: BigFraction, |
| 204 | pub(super) limit_val: Value, |
| 205 | pub(super) location: Location, |
| 206 | } |
| 207 | |
| 208 | impl $struct_name { |
| 209 | pub(crate) fn new(limit: BigFraction, limit_val: Value, location: Location) -> Self { |
| 210 | Self { limit, limit_val, location } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | impl Validate for $struct_name { |
| 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 | Ok(()) |
| 223 | } else { |
| 224 | Err(ValidationError::$error_fn( |
| 225 | self.location.clone(), |
| 226 | location.into(), |
| 227 | instance, |
| 228 | self.limit_val.clone(), |
| 229 | )) |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool { |
| 234 | if let Value::Number(item) = instance { |
| 235 | // Try to parse instance as BigFraction for exact precision |
| 236 | if let Some(instance_bigfrac) = try_parse_bigfraction(item) { |
| 237 | // Both are BigFraction - direct comparison |
| 238 | instance_bigfrac $bigfrac_op self.limit |
| 239 | } else if let Some(v) = item.as_u64() { |
| 240 | $u64_cmp(v, &self.limit) |
| 241 | } else if let Some(v) = item.as_i64() { |
| 242 | $i64_cmp(v, &self.limit) |
| 243 | } else if let Some(v) = item.as_f64() { |
| 244 | // Scientific notation or other f64-representable numbers |
| 245 | $f64_cmp(v, &self.limit) |
| 246 | } else { |
| 247 | // Extreme scientific notation beyond f64 range (e.g., 1e309, 1e400) |
| 248 | // These are not supported for comparison - treat as always valid |
| 249 | // since we can't reliably compare them |
| 250 | true |
| 251 | } |
| 252 | } else { |
| 253 | true |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | }; |
| 258 | } |
| 259 | |
| 260 | define_bigfrac_validator!( |
| 261 | BigFracMinimum, |
| 262 | u64_ge_bigfrac, |
| 263 | i64_ge_bigfrac, |
| 264 | f64_ge_bigfrac, |
| 265 | >=, |
| 266 | minimum |
| 267 | ); |
| 268 | define_bigfrac_validator!( |
| 269 | BigFracMaximum, |
| 270 | u64_le_bigfrac, |
| 271 | i64_le_bigfrac, |
| 272 | f64_le_bigfrac, |
| 273 | <=, |
| 274 | maximum |
| 275 | ); |
| 276 | define_bigfrac_validator!( |
| 277 | BigFracExclusiveMinimum, |
| 278 | u64_gt_bigfrac, |
| 279 | i64_gt_bigfrac, |
| 280 | f64_gt_bigfrac, |
| 281 | >, |
| 282 | exclusive_minimum |
| 283 | ); |
| 284 | define_bigfrac_validator!( |
| 285 | BigFracExclusiveMaximum, |
| 286 | u64_lt_bigfrac, |
| 287 | i64_lt_bigfrac, |
| 288 | f64_lt_bigfrac, |
| 289 | <, |
| 290 | exclusive_maximum |
| 291 | ); |
| 292 | } |
| 293 | |
| 294 | #[inline] |
| 295 | fn create_validator<T, V>( |
| 296 | ctx: &compiler::Context, |
| 297 | keyword: &str, |
| 298 | limit: T, |
| 299 | schema: &Value, |
| 300 | ) -> CompilationResult<'static> |
| 301 | where |
| 302 | V: From<(T, Value, Location)> + Validate + 'static, |
| 303 | { |
| 304 | let location = ctx.location().join(keyword); |
| 305 | Ok(Box::new(V::from((limit, schema.clone(), location)))) |
| 306 | } |
| 307 | |
| 308 | fn number_type_error<'a>(ctx: &compiler::Context, schema: &'a Value) -> CompilationResult<'a> { |
| 309 | Err(ValidationError::single_type_error( |
| 310 | Location::new(), |
| 311 | ctx.location().clone(), |
| 312 | schema, |
| 313 | JsonType::Number, |
| 314 | )) |
| 315 | } |
| 316 | |
| 317 | macro_rules! create_numeric_validator { |
| 318 | ($validator_type:ident, $ctx:expr, $keyword:expr, $limit:expr, $schema:expr) => { |
| 319 | if let Some(limit) = $limit.as_u64() { |
| 320 | Some(create_validator::<_, $validator_type<u64>>( |
| 321 | $ctx, $keyword, limit, $schema, |
| 322 | )) |
| 323 | } else if let Some(limit) = $limit.as_i64() { |
| 324 | Some(create_validator::<_, $validator_type<i64>>( |
| 325 | $ctx, $keyword, limit, $schema, |
| 326 | )) |
| 327 | } else { |
| 328 | #[cfg(feature = "arbitrary-precision")] |
| 329 | { |
| 330 | if let Some(result) = create_bigint_validator($ctx, $keyword, $limit, $schema) { |
| 331 | return Some(result); |
| 332 | } |
| 333 | } |
| 334 | // Handle numbers that don't fit in f64 (e.g., 1e10000) |
| 335 | // These are extremely large scientific notation numbers |
| 336 | if let Some(limit_f64) = $limit.as_f64() { |
| 337 | Some(create_validator::<_, $validator_type<f64>>( |
| 338 | $ctx, $keyword, limit_f64, $schema, |
| 339 | )) |
| 340 | } else { |
| 341 | #[cfg(feature = "arbitrary-precision")] |
| 342 | { |
| 343 | // Number is too large for f64 and couldn't be parsed as BigInt/BigFraction |
| 344 | // (e.g., "1e10000" with huge exponent exceeding f64 range) |
| 345 | // Use actual infinity as the comparison limit based on sign |
| 346 | let is_negative = $limit.as_str().starts_with('-'); |
| 347 | let infinity_limit = if is_negative { |
| 348 | f64::NEG_INFINITY |
| 349 | } else { |
| 350 | f64::INFINITY |
| 351 | }; |
| 352 | Some(create_validator::<_, $validator_type<f64>>( |
| 353 | $ctx, |
| 354 | $keyword, |
| 355 | infinity_limit, |
| 356 | $schema, |
| 357 | )) |
| 358 | } |
| 359 | #[cfg(not(feature = "arbitrary-precision"))] |
| 360 | { |
| 361 | unreachable!("as_f64() always returns Some without arbitrary-precision") |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | }; |
| 366 | } |
| 367 | |
| 368 | #[cfg(feature = "arbitrary-precision")] |
| 369 | fn create_bigint_validator( |
| 370 | ctx: &compiler::Context, |
| 371 | keyword: &str, |
| 372 | limit: &serde_json::Number, |
| 373 | schema: &Value, |
| 374 | ) -> Option<CompilationResult<'static>> { |
| 375 | use bigint_validators::{ |
| 376 | BigFracExclusiveMaximum, BigFracExclusiveMinimum, BigFracMaximum, BigFracMinimum, |
| 377 | BigIntExclusiveMaximum, BigIntExclusiveMinimum, BigIntMaximum, BigIntMinimum, |
| 378 | }; |
| 379 | |
| 380 | // Try BigInt first for large integers |
| 381 | if let Some(bigint_limit) = numeric::bignum::try_parse_bigint(limit) { |
| 382 | let location = ctx.location().join(keyword); |
| 383 | let validator: Box<dyn Validate> = match keyword { |
| 384 | "minimum" => Box::new(BigIntMinimum::new(bigint_limit, schema.clone(), location)), |
| 385 | "maximum" => Box::new(BigIntMaximum::new(bigint_limit, schema.clone(), location)), |
| 386 | "exclusiveMinimum" => Box::new(BigIntExclusiveMinimum::new( |
| 387 | bigint_limit, |
| 388 | schema.clone(), |
| 389 | location, |
| 390 | )), |
| 391 | "exclusiveMaximum" => Box::new(BigIntExclusiveMaximum::new( |
| 392 | bigint_limit, |
| 393 | schema.clone(), |
| 394 | location, |
| 395 | )), |
| 396 | _ => return None, |
| 397 | }; |
| 398 | return Some(Ok(validator)); |
| 399 | } |
| 400 | |
| 401 | // If not a BigInt, try BigFraction for exact decimal precision |
| 402 | if let Some(bigfrac_limit) = numeric::bignum::try_parse_bigfraction(limit) { |
| 403 | let location = ctx.location().join(keyword); |
| 404 | let validator: Box<dyn Validate> = match keyword { |
| 405 | "minimum" => Box::new(BigFracMinimum::new(bigfrac_limit, schema.clone(), location)), |
| 406 | "maximum" => Box::new(BigFracMaximum::new(bigfrac_limit, schema.clone(), location)), |
| 407 | "exclusiveMinimum" => Box::new(BigFracExclusiveMinimum::new( |
| 408 | bigfrac_limit, |
| 409 | schema.clone(), |
| 410 | location, |
| 411 | )), |
| 412 | "exclusiveMaximum" => Box::new(BigFracExclusiveMaximum::new( |
| 413 | bigfrac_limit, |
| 414 | schema.clone(), |
| 415 | location, |
| 416 | )), |
| 417 | _ => return None, |
| 418 | }; |
| 419 | return Some(Ok(validator)); |
| 420 | } |
| 421 | |
| 422 | None |
| 423 | } |
| 424 | |
| 425 | #[inline] |
| 426 | pub(crate) fn compile_minimum<'a>( |
| 427 | ctx: &compiler::Context, |
| 428 | _: &'a Map<String, Value>, |
| 429 | schema: &'a Value, |
| 430 | ) -> Option<CompilationResult<'a>> { |
| 431 | match schema { |
| 432 | Value::Number(limit) => create_numeric_validator!(Minimum, ctx, "minimum", limit, schema), |
| 433 | _ => Some(number_type_error(ctx, schema)), |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | #[inline] |
| 438 | pub(crate) fn compile_maximum<'a>( |
| 439 | ctx: &compiler::Context, |
| 440 | _: &'a Map<String, Value>, |
| 441 | schema: &'a Value, |
| 442 | ) -> Option<CompilationResult<'a>> { |
| 443 | match schema { |
| 444 | Value::Number(limit) => create_numeric_validator!(Maximum, ctx, "maximum", limit, schema), |
| 445 | _ => Some(number_type_error(ctx, schema)), |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | #[inline] |
| 450 | pub(crate) fn compile_exclusive_minimum<'a>( |
| 451 | ctx: &compiler::Context, |
| 452 | _: &'a Map<String, Value>, |
| 453 | schema: &'a Value, |
| 454 | ) -> Option<CompilationResult<'a>> { |
| 455 | match schema { |
| 456 | Value::Number(limit) => { |
| 457 | create_numeric_validator!(ExclusiveMinimum, ctx, "exclusiveMinimum", limit, schema) |
| 458 | } |
| 459 | _ => Some(number_type_error(ctx, schema)), |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | #[inline] |
| 464 | pub(crate) fn compile_exclusive_maximum<'a>( |
| 465 | ctx: &compiler::Context, |
| 466 | _: &'a Map<String, Value>, |
| 467 | schema: &'a Value, |
| 468 | ) -> Option<CompilationResult<'a>> { |
| 469 | match schema { |
| 470 | Value::Number(limit) => { |
| 471 | create_numeric_validator!(ExclusiveMaximum, ctx, "exclusiveMaximum", limit, schema) |
| 472 | } |
| 473 | _ => Some(number_type_error(ctx, schema)), |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | #[cfg(test)] |
| 478 | mod tests { |
| 479 | use crate::tests_util; |
| 480 | use serde_json::{json, Value}; |
| 481 | use test_case::test_case; |
| 482 | |
| 483 | #[cfg(feature = "arbitrary-precision")] |
| 484 | fn parse_json(json: &str) -> Value { |
| 485 | serde_json::from_str(json).unwrap() |
| 486 | } |
| 487 | |
| 488 | #[test_case(&json!({"minimum": 1_u64 << 54}), &json!((1_u64 << 54) - 1))] |
| 489 | #[test_case(&json!({"minimum": 1_i64 << 54}), &json!((1_i64 << 54) - 1))] |
| 490 | #[test_case(&json!({"maximum": 1_u64 << 54}), &json!((1_u64 << 54) + 1))] |
| 491 | #[test_case(&json!({"maximum": 1_i64 << 54}), &json!((1_i64 << 54) + 1))] |
| 492 | #[test_case(&json!({"exclusiveMinimum": 1_u64 << 54}), &json!(1_u64 << 54))] |
| 493 | #[test_case(&json!({"exclusiveMinimum": 1_i64 << 54}), &json!(1_i64 << 54))] |
| 494 | #[test_case(&json!({"exclusiveMinimum": 1_u64 << 54}), &json!((1_u64 << 54) - 1))] |
| 495 | #[test_case(&json!({"exclusiveMinimum": 1_i64 << 54}), &json!((1_i64 << 54) - 1))] |
| 496 | #[test_case(&json!({"exclusiveMaximum": 1_u64 << 54}), &json!(1_u64 << 54))] |
| 497 | #[test_case(&json!({"exclusiveMaximum": 1_i64 << 54}), &json!(1_i64 << 54))] |
| 498 | #[test_case(&json!({"exclusiveMaximum": 1_u64 << 54}), &json!((1_u64 << 54) + 1))] |
| 499 | #[test_case(&json!({"exclusiveMaximum": 1_i64 << 54}), &json!((1_i64 << 54) + 1))] |
| 500 | fn is_not_valid(schema: &Value, instance: &Value) { |
| 501 | tests_util::is_not_valid(schema, instance); |
| 502 | } |
| 503 | |
| 504 | #[test_case(&json!({"minimum": 5}), &json!(1), "/minimum")] |
| 505 | #[test_case(&json!({"minimum": 6}), &json!(1), "/minimum")] |
| 506 | #[test_case(&json!({"minimum": 7}), &json!(1), "/minimum")] |
| 507 | #[test_case(&json!({"maximum": 5}), &json!(10), "/maximum")] |
| 508 | #[test_case(&json!({"maximum": 6}), &json!(10), "/maximum")] |
| 509 | #[test_case(&json!({"maximum": 7}), &json!(10), "/maximum")] |
| 510 | #[test_case(&json!({"exclusiveMinimum": 5}), &json!(1), "/exclusiveMinimum")] |
| 511 | #[test_case(&json!({"exclusiveMinimum": 6}), &json!(1), "/exclusiveMinimum")] |
| 512 | #[test_case(&json!({"exclusiveMinimum": 7}), &json!(1), "/exclusiveMinimum")] |
| 513 | #[test_case(&json!({"exclusiveMaximum": 5}), &json!(7), "/exclusiveMaximum")] |
| 514 | #[test_case(&json!({"exclusiveMaximum": -1}), &json!(7), "/exclusiveMaximum")] |
| 515 | #[test_case(&json!({"exclusiveMaximum": -1.0}), &json!(7), "/exclusiveMaximum")] |
| 516 | fn location(schema: &Value, instance: &Value, expected: &str) { |
| 517 | tests_util::assert_schema_location(schema, instance, expected); |
| 518 | } |
| 519 | |
| 520 | // Tests for arbitrary-precision feature - valid cases |
| 521 | #[cfg(feature = "arbitrary-precision")] |
| 522 | #[test_case(r#"{"minimum": 18446744073709551616}"#, r"18446744073709551617"; "minimum valid above u64_max")] |
| 523 | #[test_case(r#"{"minimum": -9223372036854775809}"#, r"-9223372036854775808"; "minimum valid below i64_min")] |
| 524 | #[test_case(r#"{"maximum": 18446744073709551616}"#, r"18446744073709551615"; "maximum valid below limit")] |
| 525 | #[test_case(r#"{"exclusiveMinimum": 18446744073709551616}"#, r"18446744073709551617"; "exclusive_minimum valid")] |
| 526 | #[test_case(r#"{"exclusiveMaximum": 18446744073709551616}"#, r"18446744073709551615"; "exclusive_maximum valid")] |
| 527 | #[test_case(r#"{"minimum": 18446744073709551616}"#, "1e20"; "scientific notation above bigint minimum")] |
| 528 | #[test_case(r#"{"maximum": 18446744073709551616}"#, "1e15"; "scientific notation below bigint maximum")] |
| 529 | #[test_case(r#"{"minimum": 0}"#, "1e1000"; "minimum with huge positive number")] |
| 530 | #[test_case(r#"{"minimum": -1e100}"#, "1e1000"; "minimum with huge positive above limit")] |
| 531 | #[test_case(r#"{"exclusiveMinimum": 0}"#, "1e1000"; "exclusive minimum with huge positive")] |
| 532 | #[test_case(r#"{"minimum": 18446744073709551616}"#, "1e20"; "bigint minimum with huge instance valid")] |
| 533 | #[test_case(r#"{"maximum": 18446744073709551616}"#, "1e15"; "bigint maximum with huge instance valid")] |
| 534 | #[test_case(r#"{"exclusiveMinimum": 18446744073709551616}"#, "1e25"; "bigint exclusive minimum with huge instance valid")] |
| 535 | #[test_case(r#"{"minimum": 0}"#, "1e10000"; "infinity positive above minimum")] |
| 536 | #[test_case(r#"{"maximum": 0}"#, "-1e10000"; "infinity negative below maximum")] |
| 537 | #[test_case(r#"{"minimum": -100}"#, "1e10000"; "infinity positive above negative minimum")] |
| 538 | #[test_case(r#"{"maximum": 100}"#, "-1e10000"; "infinity negative below positive maximum")] |
| 539 | #[test_case(r#"{"minimum": 18446744073709551616}"#, "1e10000"; "infinity above bigint minimum")] |
| 540 | #[test_case(r#"{"maximum": 18446744073709551616}"#, "-1e10000"; "infinity below bigint maximum")] |
| 541 | fn is_valid_arbitrary_precision(schema_json: &str, instance_json: &str) { |
| 542 | let schema = parse_json(schema_json); |
| 543 | let instance = parse_json(instance_json); |
| 544 | tests_util::is_valid(&schema, &instance); |
| 545 | } |
| 546 | |
| 547 | // Tests for arbitrary-precision feature - invalid cases |
| 548 | #[cfg(feature = "arbitrary-precision")] |
| 549 | #[test_case(r#"{"minimum": 18446744073709551616}"#, r"18446744073709551615"; "minimum invalid at u64_max")] |
| 550 | #[test_case(r#"{"maximum": 18446744073709551616}"#, r"18446744073709551617"; "maximum invalid above limit")] |
| 551 | #[test_case(r#"{"exclusiveMinimum": 18446744073709551616}"#, r"18446744073709551616"; "exclusive_minimum invalid at boundary")] |
| 552 | #[test_case(r#"{"exclusiveMaximum": 18446744073709551616}"#, r"18446744073709551617"; "exclusive_maximum invalid above limit")] |
| 553 | #[test_case(r#"{"minimum": 18446744073709551616}"#, "1e10"; "scientific notation below bigint minimum")] |
| 554 | #[test_case(r#"{"maximum": 18446744073709551616}"#, "1e25"; "scientific notation above bigint maximum")] |
| 555 | #[test_case(r#"{"maximum": 1e100}"#, "1e1000"; "maximum with huge number above limit")] |
| 556 | #[test_case(r#"{"exclusiveMaximum": 1e100}"#, "1e1000"; "exclusive maximum with huge number")] |
| 557 | #[test_case(r#"{"minimum": 1e100}"#, "-1e1000"; "minimum with huge negative below limit")] |
| 558 | #[test_case(r#"{"minimum": 18446744073709551616}"#, "1e10"; "bigint minimum with huge instance invalid")] |
| 559 | #[test_case(r#"{"maximum": 18446744073709551616}"#, "1e25"; "bigint maximum with huge instance invalid")] |
| 560 | #[test_case(r#"{"exclusiveMaximum": 18446744073709551616}"#, "1e25"; "bigint exclusive maximum with huge instance invalid")] |
| 561 | #[test_case(r#"{"maximum": 0}"#, "1e10000"; "infinity positive above maximum")] |
| 562 | #[test_case(r#"{"minimum": 0}"#, "-1e10000"; "infinity negative below minimum")] |
| 563 | #[test_case(r#"{"exclusiveMaximum": 100}"#, "1e10000"; "infinity positive above exclusive maximum")] |
| 564 | #[test_case(r#"{"exclusiveMinimum": -100}"#, "-1e10000"; "infinity negative below exclusive minimum")] |
| 565 | #[test_case(r#"{"maximum": 18446744073709551616}"#, "1e10000"; "infinity above bigint maximum")] |
| 566 | #[test_case(r#"{"minimum": 18446744073709551616}"#, "-1e10000"; "infinity below bigint minimum")] |
| 567 | fn is_not_valid_arbitrary_precision(schema_json: &str, instance_json: &str) { |
| 568 | let schema = parse_json(schema_json); |
| 569 | let instance = parse_json(instance_json); |
| 570 | tests_util::is_not_valid(&schema, &instance); |
| 571 | } |
| 572 | |
| 573 | // Regression test for BigInt limits exceeding i128 range with fractional instances |
| 574 | #[cfg(feature = "arbitrary-precision")] |
| 575 | #[test] |
| 576 | fn bigint_limit_exceeds_i128_with_fraction() { |
| 577 | // 10^50 is way beyond i128::MAX (~1.7e38) |
| 578 | let limit = "1".to_string() + &"0".repeat(50); |
| 579 | let schema_min = parse_json(&format!(r#"{{"minimum": {limit}}}"#)); |
| 580 | |
| 581 | // Instance slightly below limit (10^50 - 1), should be INVALID |
| 582 | let instance_below = "9".to_string() + &"9".repeat(49); |
| 583 | let instance = parse_json(&instance_below); |
| 584 | tests_util::is_not_valid(&schema_min, &instance); |
| 585 | |
| 586 | // Instance equal to limit, should be VALID |
| 587 | let instance = parse_json(&limit); |
| 588 | tests_util::is_valid(&schema_min, &instance); |
| 589 | |
| 590 | // Test maximum as well |
| 591 | let schema_max = parse_json(&format!(r#"{{"maximum": {limit}}}"#)); |
| 592 | |
| 593 | // Instance slightly above limit (10^50 + 1), should be INVALID |
| 594 | let instance_above = "1".to_string() + &"0".repeat(50) + "1"; |
| 595 | let instance = parse_json(&instance_above); |
| 596 | tests_util::is_not_valid(&schema_max, &instance); |
| 597 | } |
| 598 | |
| 599 | // Test that schema compilation doesn't panic with extremely large scientific notation limits |
| 600 | // These numbers (e.g., 1e10000) exceed f64 range and are treated as infinity for comparison |
| 601 | #[cfg(feature = "arbitrary-precision")] |
| 602 | #[test_case(r#"{"minimum": 1e10000}"#, "999999999999"; "huge positive scientific notation minimum")] |
| 603 | #[test_case(r#"{"maximum": 1e10000}"#, "999999999999"; "huge positive scientific notation maximum")] |
| 604 | #[test_case(r#"{"minimum": -1e10000}"#, "999999999999"; "huge negative scientific notation minimum")] |
| 605 | #[test_case(r#"{"maximum": -1e10000}"#, "999999999999"; "huge negative scientific notation maximum")] |
| 606 | #[test_case(r#"{"exclusiveMinimum": 1e10000}"#, "999999999999"; "huge positive scientific notation exclusive minimum")] |
| 607 | #[test_case(r#"{"exclusiveMaximum": 1e10000}"#, "999999999999"; "huge positive scientific notation exclusive maximum")] |
| 608 | #[test_case(r#"{"exclusiveMinimum": -1e10000}"#, "999999999999"; "huge negative scientific notation exclusive minimum")] |
| 609 | #[test_case(r#"{"exclusiveMaximum": -1e10000}"#, "999999999999"; "huge negative scientific notation exclusive maximum")] |
| 610 | fn extreme_scientific_notation_schema_limits_no_panic(schema_json: &str, instance_json: &str) { |
| 611 | // This test ensures compilation doesn't panic when schema limits exceed f64 range |
| 612 | let schema = parse_json(schema_json); |
| 613 | let instance = parse_json(instance_json); |
| 614 | // The actual validation result doesn't matter - we're just ensuring no panic |
| 615 | let compiled = crate::validator_for(&schema); |
| 616 | assert!(compiled.is_ok(), "Schema compilation should not panic"); |
| 617 | |
| 618 | // Also verify validation works |
| 619 | let validator = compiled.unwrap(); |
| 620 | let _ = validator.is_valid(&instance); |
| 621 | } |
| 622 | |
| 623 | // Test validation behavior with extremely large scientific notation schema limits |
| 624 | #[cfg(feature = "arbitrary-precision")] |
| 625 | #[test_case(r#"{"minimum": 1e10000}"#, "999999999999", false; "normal number vs huge positive minimum")] |
| 626 | #[test_case(r#"{"maximum": 1e10000}"#, "999999999999", true; "normal number vs huge positive maximum")] |
| 627 | #[test_case(r#"{"minimum": -1e10000}"#, "999999999999", true; "normal number vs huge negative minimum")] |
| 628 | #[test_case(r#"{"maximum": -1e10000}"#, "999999999999", false; "normal number vs huge negative maximum")] |
| 629 | fn extreme_scientific_notation_schema_limits_validation( |
| 630 | schema_json: &str, |
| 631 | instance_json: &str, |
| 632 | expected_valid: bool, |
| 633 | ) { |
| 634 | let schema = parse_json(schema_json); |
| 635 | let instance = parse_json(instance_json); |
| 636 | if expected_valid { |
| 637 | tests_util::is_valid(&schema, &instance); |
| 638 | } else { |
| 639 | tests_util::is_not_valid(&schema, &instance); |
| 640 | } |
| 641 | } |
| 642 | } |