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!;
Feature Flags
toml
- provide serialization/deserialization intoml
format.yaml
- provide serialization/deserialization inyaml
format.i128
- supporti128
/u128
type (default).fluent
- provide localization using fluent.
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 |
In addition, [serde_valid::utils][module@crate::utils] provides a type of validation not described in the JSON schema specification.
Type | Serde Valid (validate derive) | Serde Valid (validation function) |
---|---|---|
Duration | #[validate(custom(duration_maximum(SECOND)))] |
[duration_maximum][crate::utils::duration_maximum ] |
Duration | #[validate(custom(duration_minimum(ZERO)))] |
[duration_minimum][crate::utils::duration_minimum ] |
Duration | #[validate(custom(duration_exclusive_maximum(SECOND)))] |
[duration_exclusive_maximum][crate::utils::duration_exclusive_maximum ] |
Duration | #[validate(custom(duration_exclusive_minimum(ZERO)))] |
[duration_exclusive_minimum][crate::utils::duration_exclusive_minimum ] |
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 = Data ;
assert_eq!;
Fluent localization
You can also use fluent localization by using fluent
feature.
use LanguageIdentifier;
use json;
use ;
)]
String,
);
assert_eq!;
Custom Method
You can use your custom validation using by #[validate(custom)]
.
use Validate;
let s = Data ;
assert!;
And you can also use closure.
use Validate;
let s = Data ;
assert!;
Custom validation is suitable for handling convenience validations not defined in JSON Schema.
serde_valid::utils::*
provides convenience functions for specific types.
use json;
use Validate;
use ;
let s = Data ;
assert!;
Multi Fields Validation
Custom Validation
Now, you can use #[validate(custom)]
for multi fields validation.
use json;
use Validate;
let s = Data ;
assert!;
Validate Traits
By implementing the validation trait, Your original type can uses Serde Valid validations.
use Validate;
;
let s = Data ;
assert!;
Validation Errors Format
Named Struct
Field errors are output to properties
.
use json;
use Validate;
let s = Data ;
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 = Data ;
assert_eq!;
New Type
Field errors are output to errors
.
use json;
use Validate;
] u32
);
let s = Data ;
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!;
New Type Enum
Variant errors are output to errors
.
use json;
use Validate;
let s = NewType ;
assert_eq!;