1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
//! Constraints defined by this crate //! //! For each constraint the possible error codes are defined as a set of //! constants. The name of the constants follow the naming convention: //! //! ```text,ignore //! INVALID_<constraint-name>[_<variant>] //! ``` //! //! The <variant> part is optional and only present if the constraint has some //! variants. The name of a constraint and its variants is converted to //! screaming snake case. The string values of the error codes follow a similar //! naming convention but use a dash (`-`) instead of the underscore to separate //! terms. Thus the codes are compatible with the convention used in the //! [_fluent_] project. //! //! [_fluent_]: https://projectfluent.org/ use crate::property::{ HasCharCount, HasCheckedValue, HasDecimalDigits, HasEmptyValue, HasLength, HasMember, HasZeroValue, }; use crate::{ invalid_optional_value, invalid_relation, invalid_value, FieldName, RelatedFields, Validate, Validation, Value, }; use std::convert::TryFrom; /// Error code: the value does not assert to true (`AssertTrue` constraint) pub const INVALID_ASSERT_TRUE: &str = "invalid-assert-true"; /// Error code: the value does not assert to false (`AssertFalse` constraint) pub const INVALID_ASSERT_FALSE: &str = "invalid-assert-false"; /// Error code: the value is empty (`NotEmpty` constraint) pub const INVALID_NOT_EMPTY: &str = "invalid-not-empty"; /// Error code: the length is not the exactly the specified value /// (`Length::Exact` constraint) pub const INVALID_LENGTH_EXACT: &str = "invalid-length-exact"; /// Error code: the length is not less or equal the specified maximum /// (`Length::Max` constraint) pub const INVALID_LENGTH_MAX: &str = "invalid-length-max"; /// Error code: the length is not greater or equal the specified minimum /// (`Length::Min` constraint) pub const INVALID_LENGTH_MIN: &str = "invalid-length-min"; /// Error code: the number of characters is not exactly the specified value /// (`CharCount::Exact` constraint) pub const INVALID_CHAR_COUNT_EXACT: &str = "invalid-char-count-exact"; /// Error code: the number of characters is not less or equal the specified /// maximum (`CharCount::Max` constraint) pub const INVALID_CHAR_COUNT_MAX: &str = "invalid-char-count-max"; /// Error code: the number of characters is not greater or equal the specified /// minimum (`CharCount::Min` constraint) pub const INVALID_CHAR_COUNT_MIN: &str = "invalid-char-count-min"; /// Error code: the value is not exactly the specified value /// (`Bound::Exact` constraint) pub const INVALID_BOUND_EXACT: &str = "invalid-bound-exact"; /// Error code: the value is not less than or equal to the specified maximum /// (`Bound::ClosedRange` or `Bound::OpenClosedRange` constraint) pub const INVALID_BOUND_CLOSED_MAX: &str = "invalid-bound-closed-max"; /// Error code: the value is not greater than or equal to the specified minimum /// (`Bound::ClosedRange` or `Bound::ClosedOpenRange` constraint) pub const INVALID_BOUND_CLOSED_MIN: &str = "invalid-bound-closed-min"; /// Error code: the value is not less than the specified maximum /// (`Bound::OpenRange` or `Bound::ClosedOpenRange` constraint) pub const INVALID_BOUND_OPEN_MAX: &str = "invalid-bound-open-max"; /// Error code: the value is not greater than the specified minimum /// (`Bound::OpenRange` or `Bound::OpenClosedRange` constraint) pub const INVALID_BOUND_OPEN_MIN: &str = "invalid-bound-open-min"; /// Error code: the value is zero (`NonZero` constraint) pub const INVALID_NON_ZERO: &str = "invalid-non-zero"; /// Error code: the number of integer digits is not less than or equal to the /// specified maximum (`Digits::integer` constraint) pub const INVALID_DIGITS_INTEGER: &str = "invalid-digits-integer"; /// Error code: the number of fraction digits is not less than or equal to the /// specified maximum (`Digits::fraction` constraint) pub const INVALID_DIGITS_FRACTION: &str = "invalid-digits-fraction"; /// Error code: the value does not contain the specified member element /// (`Contains` constraint) pub const INVALID_CONTAINS_ELEMENT: &str = "invalid-contains-element"; /// Error code: the two values do not match (`MustMatch` constraint) pub const INVALID_MUST_MATCH: &str = "invalid-must-match"; /// Error code: the first value is not less than or equal to the second value /// (`MustDefineRange::Inclusive` constraint) pub const INVALID_MUST_DEFINE_RANGE_INCLUSIVE: &str = "invalid-must-define-range-inclusive"; /// Error code: the first value is not less than the second value /// (`MustDefineRange::Exclusive` constraint) pub const INVALID_MUST_DEFINE_RANGE_EXCLUSIVE: &str = "invalid-must-define-range-exclusive"; /// The value must be true. /// /// The validation function can be applied in the [`FieldName`] context. /// It is implemented for all types `T` that implement the [`HasCheckedValue`] /// property trait. /// /// [`FieldName`]: ../core/struct.FieldName.html /// [`HasCheckedValue`]: ../property/trait.HasCheckedValue.html #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct AssertTrue; impl<T> Validate<AssertTrue, FieldName> for T where T: HasCheckedValue, { fn validate( self, name: impl Into<FieldName>, _constraint: &AssertTrue, ) -> Validation<AssertTrue, Self> { if self.is_checked_value() { Validation::success(self) } else { Validation::failure(vec![invalid_value(INVALID_ASSERT_TRUE, name, false, true)]) } } } /// The value must be false. /// /// The validation function can be applied in the [`FieldName`] context. /// It is implemented for all types `T` that implement the [`HasCheckedValue`] /// property trait. /// /// [`FieldName`]: ../core/struct.FieldName.html /// [`HasCheckedValue`]: ../property/trait.HasCheckedValue.html #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct AssertFalse; impl<T> Validate<AssertFalse, FieldName> for T where T: HasCheckedValue, { fn validate( self, name: impl Into<FieldName>, _constraint: &AssertFalse, ) -> Validation<AssertFalse, Self> { if self.is_checked_value() { Validation::failure(vec![invalid_value(INVALID_ASSERT_FALSE, name, true, false)]) } else { Validation::success(self) } } } /// The value must not be empty. /// /// The validation function can be applied in the [`FieldName`] context. /// It is implemented for all types `T` that implement the [`HasEmptyValue`] /// property trait. /// /// [`FieldName`]: ../core/struct.FieldName.html /// [`HasEmptyValue`]: ../property/trait.HasEmptyValue.html #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct NotEmpty; impl<T> Validate<NotEmpty, FieldName> for T where T: HasEmptyValue, { fn validate( self, name: impl Into<FieldName>, _constraint: &NotEmpty, ) -> Validation<NotEmpty, Self> { if self.is_empty_value() { Validation::failure(vec![invalid_optional_value( INVALID_NOT_EMPTY, name, None, None, )]) } else { Validation::success(self) } } } /// The length of a value must be within some bounds. /// /// The validation function can be applied in the [`FieldName`] context. /// It is implemented for all types `T` that implement the [`HasLength`] /// property trait. /// /// [`FieldName`]: ../core/struct.FieldName.html /// [`HasLength`]: ../property/trait.HasLength.html #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Length { /// The length of the value must be less than or equal to the specified /// maximum Max(usize), /// The length of the value must be greater than or equal to the specified /// minimum Min(usize), /// The length of the value must be between the specified minimum and /// maximum (inclusive) MinMax(usize, usize), /// The value must be of an exact length Exact(usize), } impl<T> Validate<Length, FieldName> for T where T: HasLength, { fn validate(self, name: impl Into<FieldName>, constraint: &Length) -> Validation<Length, Self> { let length = self.length(); if let Some((code, expected)) = match *constraint { Length::Max(max) => { if length > max { Some((INVALID_LENGTH_MAX, max)) } else { None } } Length::Min(min) => { if length < min { Some((INVALID_LENGTH_MIN, min)) } else { None } } Length::MinMax(min, max) => { if length < min { Some((INVALID_LENGTH_MIN, min)) } else if length > max { Some((INVALID_LENGTH_MAX, max)) } else { None } } Length::Exact(exact_len) => { if length != exact_len { Some((INVALID_LENGTH_EXACT, exact_len)) } else { None } } } { let actual = Value::try_from(length).ok(); let expected = Value::try_from(expected).ok(); Validation::failure(vec![invalid_optional_value(code, name, actual, expected)]) } else { Validation::success(self) } } } /// The number of characters must be within some bounds. /// /// The validation function can be applied in the [`FieldName`] context. /// It is implemented for all types `T` that implement the [`HasCharCount`] /// property trait. /// /// [`FieldName`]: ../core/struct.FieldName.html /// [`HasCharCount`]: ../property/trait.HasCharCount.html #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum CharCount { /// The number of characters must be less than or equal to the specified /// maximum Max(usize), /// The number of characters must be greater than or equal to the specified /// minimum Min(usize), /// The number of characters must be between the specified minimum and /// maximum (inclusive) MinMax(usize, usize), /// The number of characters must be equal to the specified amount Exact(usize), } impl<T> Validate<CharCount, FieldName> for T where T: HasCharCount, { fn validate( self, name: impl Into<FieldName>, constraint: &CharCount, ) -> Validation<CharCount, Self> { let char_count = self.char_count(); if let Some((code, expected)) = match *constraint { CharCount::Max(max) => { if char_count > max { Some((INVALID_CHAR_COUNT_MAX, max)) } else { None } } CharCount::Min(min) => { if char_count < min { Some((INVALID_CHAR_COUNT_MIN, min)) } else { None } } CharCount::MinMax(min, max) => { if char_count < min { Some((INVALID_CHAR_COUNT_MIN, min)) } else if char_count > max { Some((INVALID_CHAR_COUNT_MAX, max)) } else { None } } CharCount::Exact(exact_val) => { if char_count != exact_val { Some((INVALID_CHAR_COUNT_EXACT, exact_val)) } else { None } } } { let actual = Value::try_from(char_count).ok(); let expected = Value::try_from(expected).ok(); Validation::failure(vec![invalid_optional_value(code, name, actual, expected)]) } else { Validation::success(self) } } } /// The value must be within some bounds. /// /// The validation function can be applied in the [`FieldName`] context. /// It is implemented for all types `T` that implement the `PartialOrd` trait /// and `Into<Value>`. /// /// [`FieldName`]: ../core/struct.FieldName.html #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Bound<T> { /// The value must be between the specified minimum (inclusive) and /// maximum (inclusive) ClosedRange(T, T), /// The value must be between the specified minimum (inclusive) and /// maximum (exclusive) ClosedOpenRange(T, T), /// The value must be between the specified minimum (exclusive) and /// maximum (inclusive) OpenClosedRange(T, T), /// The value must be between the specified minimum (exclusive) and /// maximum (exclusive) OpenRange(T, T), /// The value must have the specified value Exact(T), } impl<T> Validate<Bound<T>, FieldName> for T where T: PartialOrd + Clone + Into<Value>, { fn validate( self, name: impl Into<FieldName>, constraint: &Bound<T>, ) -> Validation<Bound<T>, Self> { if let Some((code, expected)) = match constraint { Bound::ClosedRange(min, max) => { if self < *min { Some((INVALID_BOUND_CLOSED_MIN, min.clone())) } else if self > *max { Some((INVALID_BOUND_CLOSED_MAX, max.clone())) } else { None } } Bound::ClosedOpenRange(min, max) => { if self < *min { Some((INVALID_BOUND_CLOSED_MIN, min.clone())) } else if self >= *max { Some((INVALID_BOUND_OPEN_MAX, max.clone())) } else { None } } Bound::OpenClosedRange(min, max) => { if self <= *min { Some((INVALID_BOUND_OPEN_MIN, min.clone())) } else if self > *max { Some((INVALID_BOUND_CLOSED_MAX, max.clone())) } else { None } } Bound::OpenRange(min, max) => { if self <= *min { Some((INVALID_BOUND_OPEN_MIN, min.clone())) } else if self >= *max { Some((INVALID_BOUND_OPEN_MAX, max.clone())) } else { None } } Bound::Exact(bound) => { if *bound != self { Some((INVALID_BOUND_EXACT, bound.clone())) } else { None } } } { Validation::failure(vec![invalid_value(code, name, self, expected)]) } else { Validation::success(self) } } } /// Values of zero are not allowed. /// /// The validation function can be applied in the [`FieldName`] context. /// It is implemented for all types `T` that implement the [`HasZeroValue`] /// property trait and `Into<Value>`. /// /// [`FieldName`]: ../core/struct.FieldName.html /// [`HasZeroValue`]: ../property/trait.HasZeroValue.html #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct NonZero; impl<T> Validate<NonZero, FieldName> for T where T: HasZeroValue + Into<Value>, { fn validate( self, name: impl Into<FieldName>, _constraint: &NonZero, ) -> Validation<NonZero, Self> { if self.is_zero_value() { Validation::failure(vec![invalid_optional_value( INVALID_NON_ZERO, name, Some(self.into()), None, )]) } else { Validation::success(self) } } } /// Maximum number of allowed integer digits and fraction digits. /// /// The validation function can be applied in the [`FieldName`] context. /// It is implemented for all types `T` that implement the [`HasDecimalDigits`] /// property trait. /// /// [`FieldName`]: ../core/struct.FieldName.html /// [`HasDecimalDigits`]: ../property/trait.HasDecimalDigits.html #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Digits { /// Maximum number of allowed integer digits (digits to the left of the /// decimal point) pub integer: u64, /// Maximum number of allowed fraction digits (digits to the right of the /// decimal point) pub fraction: u64, } impl<T> Validate<Digits, FieldName> for T where T: HasDecimalDigits, { fn validate(self, name: impl Into<FieldName>, constraint: &Digits) -> Validation<Digits, Self> { let integer = self.integer_digits(); let fraction = self.fraction_digits(); if integer <= constraint.integer { if fraction <= constraint.fraction { Validation::success(self) } else { Validation::failure(vec![invalid_value( INVALID_DIGITS_FRACTION, name, fraction, constraint.fraction, )]) } } else if fraction <= constraint.fraction { Validation::failure(vec![invalid_value( INVALID_DIGITS_INTEGER, name, integer, constraint.integer, )]) } else { let name = name.into(); Validation::failure(vec![ invalid_value( INVALID_DIGITS_INTEGER, name.clone(), integer, constraint.integer, ), invalid_value(INVALID_DIGITS_FRACTION, name, fraction, constraint.fraction), ]) } } } /// The value must contain the specified member or the specified member must be /// part of the value. /// /// The validation function can be applied in the [`FieldName`] context. /// It is implemented for all types `T` that implement the [`HasMember`] /// property trait and `Into<Value>`. /// /// [`FieldName`]: ../core/struct.FieldName.html /// [`HasMember`]: ../property/trait.HasMember.html #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Contains<'a, A>(pub &'a A); impl<'a, T, A> Validate<Contains<'a, A>, FieldName> for T where T: HasMember<A> + Into<Value>, A: Clone + Into<Value>, { fn validate( self, name: impl Into<FieldName>, constraint: &Contains<'a, A>, ) -> Validation<Contains<'a, A>, Self> { if self.has_member(&constraint.0) { Validation::success(self) } else { Validation::failure(vec![invalid_value( INVALID_CONTAINS_ELEMENT, name, self, constraint.0.clone(), )]) } } } /// Two related fields must be equal. /// /// The validation function can be applied in the [`RelatedFields`] context. /// It is implemented for all types `T` that implement the `PartialEq` trait. /// /// [`RelatedFields`]: ../core/struct.RelatedFields.html #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct MustMatch; impl<T> Validate<MustMatch, RelatedFields> for (T, T) where T: PartialEq + Into<Value>, { fn validate( self, fields: impl Into<RelatedFields>, _constraint: &MustMatch, ) -> Validation<MustMatch, Self> { let RelatedFields(name1, name2) = fields.into(); if self.0 == self.1 { Validation::success(self) } else { Validation::failure(vec![invalid_relation( INVALID_MUST_MATCH, name1, self.0, name2, self.1, )]) } } } /// Two related fields must define a range. /// /// This constraint is useful for structs with pairs of fields that define a /// range such as `valid_from` and `valid_until` or `min_salary` and /// `max_salary`. /// /// The validation function can be applied in the [`RelatedFields`] context. /// It is implemented for all types `T` that implement the `PartialOrd` trait /// and `Into<Value`. /// /// [`RelatedFields`]: ../core/struct.RelatedFields.html #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum MustDefineRange { /// The first value must be less than or equal to the second value Inclusive, /// The first value must be less than the second value Exclusive, } impl<T> Validate<MustDefineRange, RelatedFields> for (T, T) where T: PartialOrd + Into<Value>, { fn validate( self, fields: impl Into<RelatedFields>, constraint: &MustDefineRange, ) -> Validation<MustDefineRange, Self> { let RelatedFields(name1, name2) = fields.into(); match *constraint { MustDefineRange::Inclusive => { if self.0 <= self.1 { Validation::success(self) } else { Validation::failure(vec![invalid_relation( INVALID_MUST_DEFINE_RANGE_INCLUSIVE, name1, self.0, name2, self.1, )]) } } MustDefineRange::Exclusive => { if self.0 < self.1 { Validation::success(self) } else { Validation::failure(vec![invalid_relation( INVALID_MUST_DEFINE_RANGE_EXCLUSIVE, name1, self.0, name2, self.1, )]) } } } } } #[cfg(feature = "regex")] pub use with_regex::*; #[cfg(feature = "regex")] mod with_regex { use crate::{invalid_value, FieldName, Validate, Validation}; use regex::Regex; /// Error code: the value does not match the specified pattern /// (`Pattern` constraint) pub const INVALID_PATTERN: &str = "invalid-pattern"; /// The value must match some regular expression. /// /// The validation function can be applied in the [`FieldName`] context. /// It is implemented for `String`. /// /// [`FieldName`]: ../core/struct.FieldName.html #[derive(Debug, Clone)] pub struct Pattern(pub Regex); impl Validate<Pattern, FieldName> for String { fn validate( self, name: impl Into<FieldName>, constraint: &Pattern, ) -> Validation<Pattern, Self> { if constraint.0.is_match(&self) { Validation::success(self) } else { Validation::failure(vec![invalid_value( INVALID_PATTERN, name, self, constraint.0.to_string(), )]) } } } } #[cfg(test)] mod tests;