dade
dade is a framework for defining data structure to Rust structures, like pydantic in Python.
For the easy handle of data, the following will support it.
- validation for (primitive) types.
- numeric types; u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64
- boolean
- String
- Optional
- Vec
- nested model
- enum
- export a data schema conforms JsonSchema.
- load a model from JSON.
- dump a JSON-string from a model.
- implements for useful types. e.g. URL, email and etc.
- publish a trait that for you implements any custom type with your validation.
See documentation for more details.
Roadmap
implements for basic idea.implements for (primitive) types, enum, and more.- support configuration of a model, for example, set a given name to a title in JsonSchema, or exclusion another key.
- implements for useful types and a trait for custom type.
Example
Basic
To define a model, You need the below module.
use ;
For example, define user-model.
Then you create an instance of the model by the below.
let input = "{\"id\": 1, \"name\": \"James Smith\"}";
let user = parse.unwrap;
And you get a Json string for the instance by the below.
let json_string = user.json;
// json_string = "{\"id\":1,\"name\":\"James Smith\",\"lang\":\"en\",\"url\":null,\"verified\":false}"
If you want to validate a value, you will get a schema that conforms JsonSchema, for the given model, by the below.
let schema = schema;
The schema is
Advance
- If you want to bind other name
- If you need a nested model
- If you need a self-reference model