Serde Valid
This is JSON Schema based validation tool using with serde.
Usage
You derive Validate trait, and write validations.
use Validate;
let s = Named ;
assert!;
Validations
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 |
| String | #[validate(min_length = 5)] |
ValidateMinLength |
minLength |
| String | #[validate(pattern = r"^\d{5}$")] |
ValidatePattern |
pattern |
| Numeric | #[validate(maximum = 5)] |
ValidateMaximum |
maximum |
| Numeric | #[validate(minimum = 5)] |
ValidateMinimum |
minimum |
| Numeric | #[validate(exclusive_maximum = 5)] |
ValidateExclusiveMaximum |
exclusiveMaximum |
| Numeric | #[validate(exclusive_minimum = 5)] |
ValidateExclusiveMinimum |
exclusiveMinimum |
| Numeric | #[validate(multiple_of = 5)] |
ValidateMultipleOf |
multipleOf |
| Object | #[validate(max_properties = 5)] |
ValidateMaxProperties |
maxProperties |
| Object | #[validate(min_properties = 5)] |
ValidateMinProperties |
minProperties |
| Array | #[validate(max_items = 5)] |
ValidateMaxItems |
maxItems |
| Array | #[validate(min_items = 5)] |
ValidateMinItems |
minItems |
| Array | #[validate(unique_items)] |
ValidateUniqueItems |
uniqueItems |
| Generic | #[validate(enumerate(5, 10, 15))] |
ValidateEnumerate |
enum |
Complete Constructor (Deserialization)
Serde Valid support complete constructor method using by serde_valid::json::FromJsonValue trait.
use Deserialize;
use Validate;
use ;
// Deserialization and Validation!! 🚀
let err = from_json_value.unwrap_err;
assert_eq!;
You can force validation by only deserialization through serde_valid, and removing serde_json from Cargo.toml of your project.
Serialization
For serialization, provides serde_valid::json::ToJsonString trait.
use Serialize;
use Validate;
use ;
assert_eq!;
Custom Message
For user custom message, Serde Valid provides message_fn or message.
use json;
use Validate;
let s = SampleStruct ;
assert_eq!;
Custom method
You can use your custom validation using by #[validate(custom)].
use Validate;
let s = SampleStruct ;
assert!;
Rules
If you want to check multi fields validation, can use #[rule].
use json;
use Validate;
let s = SampleStruct ;
let errors = s.validate.unwrap_err;
assert_eq!;
If you want to use rule to unnamed fields struct, just like this,
use json;
use Validate;
;
let s = SampleStruct;
assert!;
Validate Traits
By implementing the validation trait, Your original type can uses Serde Valid validations.
use Validate;
;
let s = SampleStruct ;
assert!;
Validation Errors Format
Named Struct
Field errors are output to properties.
use json;
use Validate;
let s = SampleStruct ;
assert_eq!;
Unnamed Struct
Field errors are output to items. The key for items is guaranteed to be a string of positive numbers.
use json;
use Validate;
] u32,
u32,
);
let s = SampleStruct ;
assert_eq!;
New Type
Field errors are output to errors.
use json;
use Validate;
] u32
);
let s = SampleStruct ;
assert_eq!;
Named Enum
Variant errors are output to properties.
use json;
use Validate;
let s = Named ;
assert_eq!;
Unnamed Enum
Variant errors are output to items. The key for items is guaranteed to be a string of positive numbers.
use json;
use Validate;
let s = Unnamed ;
assert_eq!;
Newtype Enum
Variant errors are output to errors.
use json;
use Validate;
let s = NewType ;
assert_eq!;