valid

![Latest Release] ![Documentation] ![License] ![Linux Build Status] ![Windows Build Status] ![Test Coverage] ![Rustc Version 1.35+]

Let the business logic only accept valid values!

valid is a validation library for the [Rust] language. It let us write validation functions for our custom types through composition of available validators. Any custom written validation function again can be used to build validations for even more complex types.

The valid crate defines the types and traits to implement validation functions and use them to validate our values. Additionally it defines primitive constraints.

Most primitive constraints validate one property of the validated type. E.g. the Length constraint validates the length property of strings, container types or slices. If the constraint property is not covered by a trait of the std-lib, a related trait is defined, which we call a property trait.

The builtin constraints are implemented for generic types T that implement the related property trait.

One goal of valid is to provide one common API that can be used to validate all kind of business rules. Constraints are grouped into one of 3 categories:

  1. field level constraint, e.g. only a range of values is allowed
  2. the relation of 2 fields, e.g. 2 password fields must match or 2 fields must define a range
  3. business rules that verify some aspect of the application state, e.g. a username must be unique

Any violation of constraints are returned in one common error type, regardless of the category of the business rule that defines the constraint.

One principle for the core functionality of this crate is to have no dependencies other than the std-lib. Support for types of 3rd party crates such as [bigdecimal] and [chrono] are implemented as optional crate features. So you can pick and choose which types you need in your application and which dependencies you will have in your project.

Features

Usage

valid provides some of its functionality as optional crate features. To use it we must enable the relevant crate feature in our Cargo.toml file.

Serialization and deserialization of ValdiationError through the [serde] crate:

toml [dependencies] valid = { version = "0.2", features = ["serde1"] }

Support for validating BigDecimal of the [bigdecimal] crate:

toml [dependencies] valid = { version = "0.2", features = ["bigdecimal"] }

Support for validating NaiveDate and DateTime of the [chrono] crate:

toml [dependencies] valid = { version = "0.2", features = ["chrono"] }

Theses crate features can be enabled in any combination. For detailed information on how to use [valid] see the API documentation at docs.rs.