Skip to content

Commit

Permalink
Merge pull request #73 from yassun7010/change_enumerate_interface
Browse files Browse the repository at this point in the history
Change enumerate interface
  • Loading branch information
yassun7010 authored Jun 21, 2024
2 parents 005d7f2 + 5ad33ef commit 5f93b20
Show file tree
Hide file tree
Showing 28 changed files with 570 additions and 175 deletions.
32 changes: 16 additions & 16 deletions serde_valid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ assert!(s.validate().is_ok());

Serde Valid support standard validation based JSON Schema.

| Type | Serde Valid (validate derive) | Serde Valid (validate trait) | JSON Schema |
| :-----: | :----------------------------------- | :--------------------------- | :-------------------------------------------------------------------------------------------- |
| String | `#[validate(max_length = 5)]` | [`ValidateMaxLength`] | [maxLength](https://json-schema.org/understanding-json-schema/reference/string#length) |
| String | `#[validate(min_length = 5)]` | [`ValidateMinLength`] | [minLength](https://json-schema.org/understanding-json-schema/reference/string#length) |
| String | `#[validate(pattern = r"^\d{5}$")]` | [`ValidatePattern`] | [pattern](https://json-schema.org/understanding-json-schema/reference/string#regexp) |
| Numeric | `#[validate(maximum = 5)]` | [`ValidateMaximum`] | [maximum](https://json-schema.org/understanding-json-schema/reference/numeric#range) |
| Numeric | `#[validate(minimum = 5)]` | [`ValidateMinimum`] | [minimum](https://json-schema.org/understanding-json-schema/reference/numeric#range) |
| Numeric | `#[validate(exclusive_maximum = 5)]` | [`ValidateExclusiveMaximum`] | [exclusiveMaximum](https://json-schema.org/understanding-json-schema/reference/numeric#range) |
| Numeric | `#[validate(exclusive_minimum = 5)]` | [`ValidateExclusiveMinimum`] | [exclusiveMinimum](https://json-schema.org/understanding-json-schema/reference/numeric#range) |
| Numeric | `#[validate(multiple_of = 5)]` | [`ValidateMultipleOf`] | [multipleOf](https://json-schema.org/understanding-json-schema/reference/numeric#multiples) |
| Object | `#[validate(max_properties = 5)]` | [`ValidateMaxProperties`] | [maxProperties](https://json-schema.org/understanding-json-schema/reference/object#size) |
| Object | `#[validate(min_properties = 5)]` | [`ValidateMinProperties`] | [minProperties](https://json-schema.org/understanding-json-schema/reference/object#size) |
| Array | `#[validate(max_items = 5)]` | [`ValidateMaxItems`] | [maxItems](https://json-schema.org/understanding-json-schema/reference/array#length) |
| Array | `#[validate(min_items = 5)]` | [`ValidateMinItems`] | [minItems](https://json-schema.org/understanding-json-schema/reference/array#length) |
| Array | `#[validate(unique_items)]` | [`ValidateUniqueItems`] | [uniqueItems](https://json-schema.org/understanding-json-schema/reference/array#uniqueItems) |
| Generic | `#[validate(enumerate(5, 10, 15))]` | [`ValidateEnumerate`] | [enum](https://json-schema.org/understanding-json-schema/reference/enum) |
| Type | Serde Valid (validate derive) | Serde Valid (validate trait) | JSON Schema |
| :-----: | :------------------------------------- | :--------------------------- | :-------------------------------------------------------------------------------------------- |
| String | `#[validate(max_length = 5)]` | [`ValidateMaxLength`] | [maxLength](https://json-schema.org/understanding-json-schema/reference/string#length) |
| String | `#[validate(min_length = 5)]` | [`ValidateMinLength`] | [minLength](https://json-schema.org/understanding-json-schema/reference/string#length) |
| String | `#[validate(pattern = r"^\d{5}$")]` | [`ValidatePattern`] | [pattern](https://json-schema.org/understanding-json-schema/reference/string#regexp) |
| Numeric | `#[validate(maximum = 5)]` | [`ValidateMaximum`] | [maximum](https://json-schema.org/understanding-json-schema/reference/numeric#range) |
| Numeric | `#[validate(minimum = 5)]` | [`ValidateMinimum`] | [minimum](https://json-schema.org/understanding-json-schema/reference/numeric#range) |
| Numeric | `#[validate(exclusive_maximum = 5)]` | [`ValidateExclusiveMaximum`] | [exclusiveMaximum](https://json-schema.org/understanding-json-schema/reference/numeric#range) |
| Numeric | `#[validate(exclusive_minimum = 5)]` | [`ValidateExclusiveMinimum`] | [exclusiveMinimum](https://json-schema.org/understanding-json-schema/reference/numeric#range) |
| Numeric | `#[validate(multiple_of = 5)]` | [`ValidateMultipleOf`] | [multipleOf](https://json-schema.org/understanding-json-schema/reference/numeric#multiples) |
| Object | `#[validate(max_properties = 5)]` | [`ValidateMaxProperties`] | [maxProperties](https://json-schema.org/understanding-json-schema/reference/object#size) |
| Object | `#[validate(min_properties = 5)]` | [`ValidateMinProperties`] | [minProperties](https://json-schema.org/understanding-json-schema/reference/object#size) |
| Array | `#[validate(max_items = 5)]` | [`ValidateMaxItems`] | [maxItems](https://json-schema.org/understanding-json-schema/reference/array#length) |
| Array | `#[validate(min_items = 5)]` | [`ValidateMinItems`] | [minItems](https://json-schema.org/understanding-json-schema/reference/array#length) |
| Array | `#[validate(unique_items)]` | [`ValidateUniqueItems`] | [uniqueItems](https://json-schema.org/understanding-json-schema/reference/array#uniqueItems) |
| Generic | `#[validate(enumerate = [5, 10, 15])]` | [`ValidateEnumerate`] | [enum](https://json-schema.org/understanding-json-schema/reference/enum) |

In addition, [serde_valid::utils][module@crate::utils] provides a type of validation not described in the JSON schema specification.

Expand Down
32 changes: 16 additions & 16 deletions serde_valid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@
//!
//! Serde Valid support standard validation based JSON Schema.
//!
//! | Type | Serde Valid (validate derive) | Serde Valid (validate trait) | JSON Schema |
//! | :-----: | :----------------------------------- | :--------------------------- | :-------------------------------------------------------------------------------------------- |
//! | String | `#[validate(max_length = 5)]` | [`ValidateMaxLength`] | [maxLength](https://json-schema.org/understanding-json-schema/reference/string#length) |
//! | String | `#[validate(min_length = 5)]` | [`ValidateMinLength`] | [minLength](https://json-schema.org/understanding-json-schema/reference/string#length) |
//! | String | `#[validate(pattern = r"^\d{5}$")]` | [`ValidatePattern`] | [pattern](https://json-schema.org/understanding-json-schema/reference/string#regexp) |
//! | Numeric | `#[validate(maximum = 5)]` | [`ValidateMaximum`] | [maximum](https://json-schema.org/understanding-json-schema/reference/numeric#range) |
//! | Numeric | `#[validate(minimum = 5)]` | [`ValidateMinimum`] | [minimum](https://json-schema.org/understanding-json-schema/reference/numeric#range) |
//! | Numeric | `#[validate(exclusive_maximum = 5)]` | [`ValidateExclusiveMaximum`] | [exclusiveMaximum](https://json-schema.org/understanding-json-schema/reference/numeric#range) |
//! | Numeric | `#[validate(exclusive_minimum = 5)]` | [`ValidateExclusiveMinimum`] | [exclusiveMinimum](https://json-schema.org/understanding-json-schema/reference/numeric#range) |
//! | Numeric | `#[validate(multiple_of = 5)]` | [`ValidateMultipleOf`] | [multipleOf](https://json-schema.org/understanding-json-schema/reference/numeric#multiples) |
//! | Object | `#[validate(max_properties = 5)]` | [`ValidateMaxProperties`] | [maxProperties](https://json-schema.org/understanding-json-schema/reference/object#size) |
//! | Object | `#[validate(min_properties = 5)]` | [`ValidateMinProperties`] | [minProperties](https://json-schema.org/understanding-json-schema/reference/object#size) |
//! | Array | `#[validate(max_items = 5)]` | [`ValidateMaxItems`] | [maxItems](https://json-schema.org/understanding-json-schema/reference/array#length) |
//! | Array | `#[validate(min_items = 5)]` | [`ValidateMinItems`] | [minItems](https://json-schema.org/understanding-json-schema/reference/array#length) |
//! | Array | `#[validate(unique_items)]` | [`ValidateUniqueItems`] | [uniqueItems](https://json-schema.org/understanding-json-schema/reference/array#uniqueItems) |
//! | Generic | `#[validate(enumerate(5, 10, 15))]` | [`ValidateEnumerate`] | [enum](https://json-schema.org/understanding-json-schema/reference/enum) |
//! | Type | Serde Valid (validate derive) | Serde Valid (validate trait) | JSON Schema |
//! | :-----: | :------------------------------------- | :--------------------------- | :-------------------------------------------------------------------------------------------- |
//! | String | `#[validate(max_length = 5)]` | [`ValidateMaxLength`] | [maxLength](https://json-schema.org/understanding-json-schema/reference/string#length) |
//! | String | `#[validate(min_length = 5)]` | [`ValidateMinLength`] | [minLength](https://json-schema.org/understanding-json-schema/reference/string#length) |
//! | String | `#[validate(pattern = r"^\d{5}$")]` | [`ValidatePattern`] | [pattern](https://json-schema.org/understanding-json-schema/reference/string#regexp) |
//! | Numeric | `#[validate(maximum = 5)]` | [`ValidateMaximum`] | [maximum](https://json-schema.org/understanding-json-schema/reference/numeric#range) |
//! | Numeric | `#[validate(minimum = 5)]` | [`ValidateMinimum`] | [minimum](https://json-schema.org/understanding-json-schema/reference/numeric#range) |
//! | Numeric | `#[validate(exclusive_maximum = 5)]` | [`ValidateExclusiveMaximum`] | [exclusiveMaximum](https://json-schema.org/understanding-json-schema/reference/numeric#range) |
//! | Numeric | `#[validate(exclusive_minimum = 5)]` | [`ValidateExclusiveMinimum`] | [exclusiveMinimum](https://json-schema.org/understanding-json-schema/reference/numeric#range) |
//! | Numeric | `#[validate(multiple_of = 5)]` | [`ValidateMultipleOf`] | [multipleOf](https://json-schema.org/understanding-json-schema/reference/numeric#multiples) |
//! | Object | `#[validate(max_properties = 5)]` | [`ValidateMaxProperties`] | [maxProperties](https://json-schema.org/understanding-json-schema/reference/object#size) |
//! | Object | `#[validate(min_properties = 5)]` | [`ValidateMinProperties`] | [minProperties](https://json-schema.org/understanding-json-schema/reference/object#size) |
//! | Array | `#[validate(max_items = 5)]` | [`ValidateMaxItems`] | [maxItems](https://json-schema.org/understanding-json-schema/reference/array#length) |
//! | Array | `#[validate(min_items = 5)]` | [`ValidateMinItems`] | [minItems](https://json-schema.org/understanding-json-schema/reference/array#length) |
//! | Array | `#[validate(unique_items)]` | [`ValidateUniqueItems`] | [uniqueItems](https://json-schema.org/understanding-json-schema/reference/array#uniqueItems) |
//! | Generic | `#[validate(enumerate = [5, 10, 15])]` | [`ValidateEnumerate`] | [enum](https://json-schema.org/understanding-json-schema/reference/enum) |
//!
//! In addition, [serde_valid::utils][module@crate::utils] provides a type of validation not described in the JSON schema specification.
//!
Expand Down
2 changes: 1 addition & 1 deletion serde_valid/src/validation/generic/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::EnumerateError;
///
/// #[derive(Validate)]
/// struct TestStruct {
/// #[validate(enumerate("1", "2", "3"))]
/// #[validate(enumerate = ["1", "2", "3"])]
/// val: MyType,
/// }
///
Expand Down
24 changes: 12 additions & 12 deletions serde_valid/tests/complex_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn sample_rule(_val: i32) -> Result<(), serde_valid::validation::Error> {
#[validate(custom(|s| sample_rule(s.int_value)))]
struct TestStruct<'a> {
// Generic validator
#[validate(enumerate(5, 10, 15))]
#[validate(enumerate = [5, 10, 15])]
// Numeric validator
#[validate(multiple_of = 5)]
#[validate(minimum = 5)]
Expand All @@ -18,7 +18,7 @@ struct TestStruct<'a> {
int_value: i32,

// Generic validator
#[validate(enumerate(5.0, 10.0, 15.0))]
#[validate(enumerate = [5.0, 10.0, 15.0])]
// Numeric validator
#[validate(multiple_of = 5.0)]
#[validate(minimum = 5.0)]
Expand All @@ -28,31 +28,31 @@ struct TestStruct<'a> {
float_value: f32,

// Generic validator
#[validate(enumerate("12345", "67890"))]
#[validate(enumerate = ["12345", "67890"])]
// String validator
#[validate(min_length = 5)]
#[validate(max_length = 5)]
#[validate(pattern = r"^\d{5}$")]
string_value: String,

// Generic validator
#[validate(enumerate("12345", "67890"))]
#[validate(enumerate = ["12345", "67890"])]
// String validator
#[validate(min_length = 5)]
#[validate(max_length = 5)]
#[validate(pattern = r"^\d{5}$")]
str_value: &'a str,

// Generic validator
#[validate(enumerate(5, 10, 15))]
#[validate(enumerate = [5, 10, 15])]
// Numeric validator
#[validate(multiple_of = 5)]
#[validate(minimum = 5)]
#[validate(maximum = 5)]
optional_value: Option<i32>,

// Generic validator
#[validate(enumerate(5, 10, 15))]
#[validate(enumerate = [5, 10, 15])]
// Array validator
#[validate(unique_items)]
#[validate(min_items = 3)]
Expand All @@ -78,7 +78,7 @@ struct TestStruct<'a> {
#[derive(Debug, Validate)]
struct TestInnerStruct<'a> {
// Generic validator
#[validate(enumerate(5, 10, 15))]
#[validate(enumerate = [5, 10, 15])]
// Numeric validator
#[validate(multiple_of = 5)]
#[validate(minimum = 5)]
Expand All @@ -88,7 +88,7 @@ struct TestInnerStruct<'a> {
inner_int_value: i32,

// Generic validator
#[validate(enumerate(5.0, 10.0, 15.0))]
#[validate(enumerate = [5.0, 10.0, 15.0])]
// Numeric validator
#[validate(multiple_of = 5.0)]
#[validate(minimum = 5.0)]
Expand All @@ -98,31 +98,31 @@ struct TestInnerStruct<'a> {
inner_float_value: f32,

// Generic validator
#[validate(enumerate("12345", "67890"))]
#[validate(enumerate = ["12345", "67890"])]
// String validator
#[validate(min_length = 5)]
#[validate(max_length = 5)]
#[validate(pattern = r"^\d{5}$")]
inner_string_value: String,

// Generic validator
#[validate(enumerate("12345", "67890"))]
#[validate(enumerate = ["12345", "67890"])]
// String validator
#[validate(min_length = 5)]
#[validate(max_length = 5)]
#[validate(pattern = r"^\d{5}$")]
inner_str_value: &'a str,

// Generic validator
#[validate(enumerate(5, 10, 15))]
#[validate(enumerate = [5, 10, 15])]
// Numeric validator
#[validate(multiple_of = 5)]
#[validate(minimum = 5)]
#[validate(maximum = 5)]
inner_optional_value: Option<i32>,

// Generic validator
#[validate(enumerate(5, 10, 15))]
#[validate(enumerate = [5, 10, 15])]
// Array validator
#[validate(unique_items)]
#[validate(min_items = 3)]
Expand Down
Loading

0 comments on commit 5f93b20

Please sign in to comment.