What is Valico?
Valico is a validation and coercion tool for JSON objects, written in Rust. It designed to be a support library for the various REST-like frameworks or other tools that need to validate and coerce JSON input from outside world.
Valico has two features:
- DSL — a set of simple validators and coercers inspired by [Grape]. It has built-in support for common coercers, validators and can return detailed error messages if something goes wrong.
- JSON Schema — An implementation of JSON Schema, based on IETF's draft v7.
References:
- http://json-schema.org
- http://json-schema.org/latest/json-schema-core.html
- http://json-schema.org/latest/json-schema-validation.html
# Cargo.toml
= "2"
JSON Schema
It passes the entire JSON-Schema-Test-Suite except for remoteRefs and maxLength/minLength when using unicode surrogate pairs. It also can validate your schema and give you an explanation about what is wrong in it.
Example
extern crate serde_json;
extern crate valico;
use Value;
use json_schema;
use File;
JSON Schema builder
Valico goes with valico::json_schema::schema(|scheme| { /* .. */ }) -> json::Json
function that allows to use simple DSL to generate your schemes. It allows you not to use strings and raw JSON manipulation. It also prevent some kinds of spelling and type errors.
schema
TODO more docs about JSON Schema here
DSL
Basic Usage
All Valico stuff is making by Builder instance. Below is a simple example showing how one can create and setup Builder:
let params = build;
Later params
instance can be used to process one or more JSON objects with it's process
method with signature fn process(&self, tree: &mut JsonObject) -> ValicoResult<()>
.
Note that Valico will mutate borrowed JSON value if some coercion is needed.
Example:
extern crate valico;
extern crate serde_json;
use json_dsl;
use ;
Also you can look to the specs for more details and examples.
Validation and coercion
You can define validations and coercion options for your parameters using a Builder::build
block. Parameters can be optional and required. Requires parameters must be always present. Optional parameters can be omitted.
When parameter is present in JSON all validation and coercions will be applied and error fired if something goes wrong.
Builder
This functions are available in Builder to define parameters:
// Parameter is required, no coercion
;
// Parameter is required, with coercion
;
// Parameter is required, with coercion and nested checks
;
// Parameter is required, setup with Param DSL
;
// Parameter is optional, no coercion
;
// Parameter is optional, with coercion
;
// Parameter is optional, with coercion and nested checks
;
// Parameter is required, setup with Param DSL
;
Built-in Coercers
Available list of coercers:
- json_dsl::i64()
- json_dsl::u64()
- json_dsl::f64()
- json_dsl::string()
- json_dsl::boolean()
- json_dsl::null()
- json_dsl::array()
- json_dsl::array_of()
- json_dsl::encoded_array() — use it for string-encoded arrays e.g. "red,green,blue" -> ["red", "green", "blue"]
- json_dsl::encoded_array_of() — use it for string-encoded arrays of some type e.g. "1,2,3" -> [1, 2, 3]
- json_dsl::object()
Example of usage:
let params = build;
Nested processing
You can specify rules to nesting processing for lists and objects:
let params = build;
let params = build;
Nesting level is not limited in Valico.
Validate with JSON Schema
DSL allows to use JSON Schema validations to validate objects at the Builder level and the Param level:
let params = build;
Note that JSON Schema validates object AFTER coerce pass:
let mut params = build;
Don't forget to create a json_schema::Scope
BEFORE processing:
let mut scope = new;
params.build_schemes.unwrap;
Parameters DSL
You can use DSL block to setup parameters with more flexible way:
let params = build;
Parameter validations
DSL supports several parameter validations. They considered outdated and likely to be removed in the future in favour of JSON Schema validation.
allow_values
Parameters can be restricted to a specific set of values with allow_values:
let params = build
reject_values
Some values can be rejected with reject_values:
let params = build
regex
String values can be tested with Regex:
let params = build;
validate_with
Sometimes it's usefull to use some custom function as validator:
let params = build;
validate
One can use custom validator. Docs in Progress.
Builder validations
Some validators can be specified in Builder DSL block to validate a set of parameters.
mutually_exclusive
Parameters can be defined as mutually_exclusive, ensuring that they aren't present at the same time in a request.
let params = build;
Multiple sets can be defined:
let params = build;
Warning: Never define mutually exclusive sets with any required params. Two mutually exclusive required params will mean params are never valid. One required param mutually exclusive with an optional param will mean the latter is never valid.
exactly_one_of
Parameters can be defined as 'exactly_one_of', ensuring that exactly one parameter gets selected.
let params = build;
at_least_one_of
Parameters can be defined as 'at_least_one_of', ensuring that at least one parameter gets selected.
let params = build;
validate_with
Sometimes it's usefull to use some custom function as validator:
let params = build;
validate
One can use custom validator. Docs in Progress.
Building for other targets
Web Assembly
For WebAssembly, enable the js
feature:
# Cargo.toml
= { = "2", = ["js"] }