/schemars/tests/validate.rs

https://github.com/GREsau/schemars · Rust · 121 lines · 109 code · 11 blank · 1 comment · 0 complexity · 5150b809bc371607edc449f79405e155 MD5 · raw file

  1. mod util;
  2. use schemars::JsonSchema;
  3. use std::collections::HashMap;
  4. use util::*;
  5. // In real code, this would typically be a Regex, potentially created in a `lazy_static!`.
  6. static STARTS_WITH_HELLO: &'static str = r"^[Hh]ello\b";
  7. const MIN: u32 = 1;
  8. const MAX: u32 = 1000;
  9. #[derive(Debug, JsonSchema)]
  10. pub struct Struct {
  11. #[validate(range(min = 0.01, max = 100))]
  12. min_max: f32,
  13. #[validate(range(min = "MIN", max = "MAX"))]
  14. min_max2: f32,
  15. #[validate(regex = "STARTS_WITH_HELLO")]
  16. regex_str1: String,
  17. #[validate(regex(path = "STARTS_WITH_HELLO", code = "foo"))]
  18. regex_str2: String,
  19. #[validate(regex(pattern = r"^\d+$"))]
  20. regex_str3: String,
  21. #[validate(contains = "substring...")]
  22. contains_str1: String,
  23. #[validate(contains(pattern = "substring...", message = "bar"))]
  24. contains_str2: String,
  25. #[validate(email)]
  26. email_address: String,
  27. #[validate(phone)]
  28. tel: String,
  29. #[validate(url)]
  30. homepage: String,
  31. #[validate(length(min = 1, max = 100))]
  32. non_empty_str: String,
  33. #[validate(length(min = "MIN", max = "MAX"))]
  34. non_empty_str2: String,
  35. #[validate(length(equal = 2))]
  36. pair: Vec<i32>,
  37. #[validate(contains = "map_key")]
  38. map_contains: HashMap<String, ()>,
  39. #[validate(required)]
  40. required_option: Option<bool>,
  41. #[validate(required)]
  42. #[validate]
  43. #[serde(flatten)]
  44. required_flattened: Option<Inner>,
  45. }
  46. #[derive(Debug, JsonSchema)]
  47. pub struct Inner {
  48. x: i32,
  49. }
  50. #[test]
  51. fn validate() -> TestResult {
  52. test_default_generated_schema::<Struct>("validate")
  53. }
  54. #[derive(Debug, JsonSchema)]
  55. pub struct Struct2 {
  56. #[schemars(range(min = 0.01, max = 100))]
  57. min_max: f32,
  58. #[schemars(range(min = "MIN", max = "MAX"))]
  59. min_max2: f32,
  60. #[validate(regex = "overridden")]
  61. #[schemars(regex = "STARTS_WITH_HELLO")]
  62. regex_str1: String,
  63. #[schemars(regex(path = "STARTS_WITH_HELLO"))]
  64. regex_str2: String,
  65. #[schemars(regex(pattern = r"^\d+$"))]
  66. regex_str3: String,
  67. #[validate(regex = "overridden")]
  68. #[schemars(contains = "substring...")]
  69. contains_str1: String,
  70. #[schemars(contains(pattern = "substring..."))]
  71. contains_str2: String,
  72. #[schemars(email)]
  73. email_address: String,
  74. #[schemars(phone)]
  75. tel: String,
  76. #[schemars(url)]
  77. homepage: String,
  78. #[schemars(length(min = 1, max = 100))]
  79. non_empty_str: String,
  80. #[schemars(length(min = "MIN", max = "MAX"))]
  81. non_empty_str2: String,
  82. #[schemars(length(equal = 2))]
  83. pair: Vec<i32>,
  84. #[schemars(contains = "map_key")]
  85. map_contains: HashMap<String, ()>,
  86. #[schemars(required)]
  87. required_option: Option<bool>,
  88. #[schemars(required)]
  89. #[serde(flatten)]
  90. required_flattened: Option<Inner>,
  91. }
  92. #[test]
  93. fn validate_schemars_attrs() -> TestResult {
  94. test_default_generated_schema::<Struct>("validate_schemars_attrs")
  95. }
  96. #[derive(Debug, JsonSchema)]
  97. pub struct Tuple(
  98. #[validate(range(max = 10))] u8,
  99. #[validate(required)] Option<bool>,
  100. );
  101. #[test]
  102. fn validate_tuple() -> TestResult {
  103. test_default_generated_schema::<Tuple>("validate_tuple")
  104. }
  105. #[derive(Debug, JsonSchema)]
  106. pub struct NewType(#[validate(range(max = 10))] u8);
  107. #[test]
  108. fn validate_newtype() -> TestResult {
  109. test_default_generated_schema::<NewType>("validate_newtype")
  110. }