This is a library for validating and modeling user input and this crate provides a procedural macro to define validators with optional parameters.
toml
[dependencies]
validators-derive = "*"
validators = "*"
```rust
extern crate validators;
use validators::prelude::*;
/*
DEFINEYOURSTRUCT_HERE */ ```
When you add the #[validator(validator_name)]
attribute for your structs, one or more traits in the validators::traits
are implemented. They can be used for validation and deserialization.
The struct used as a validator should have specific components according to its validator and the parameters of that validator. For example, a base32 validator must be struct(String)
and a base32_decoded validator must be struct(Vec<u8>)
.
The #[validator(validator_name)]
attribute cannot be used on fields in any structs or enums. The reason that this crate uses a procedural macro to define a validator (i.e. a struct) instead of providing built-in structs for each configuration is to make the configurable validations have no overhead at runtime and also to increase the compilation speed.
Some validators such as ip, ipv4, and ipv6 depend on std. If you don't need them, you can disable the default features to compile this crate and your validators without std.
```toml [dependencies] validators = "*"
[dependencies.validators-derive] version = "*" default-features = false features = ["base32"] ```
Enable the serde
feature to let your validators support the serde framework.
```toml [dependencies] validators = "*"
[dependencies.validators-derive] version = "*" features = ["serde"] ```
Enable the rocket
feature to let your validators support the Rocket framework.
```toml [dependencies] validators = "*"
[dependencies.validators-derive] version = "*" features = ["rocket"] ```
traits: ValidateString
, ValidateBytes
```rust
extern crate validators;
use validators::prelude::*;
pub struct Base32WithPadding(String);
assert!(Base32WithPadding::parsestring("GEZDGNBVGY3TQOI=").isok()); assert!(Base32WithPadding::parsestring("GEZDGNBVGY3TQOI").iserr()); ```
traits: ValidateString
, ValidateBytes
, CollectionLength
```rust
extern crate validators;
use validators::prelude::*;
pub struct Base32WithPaddingDecoded(Vec
asserteq!(b"123456789", Base32WithPaddingDecoded::parsestring("GEZDGNBVGY3TQOI=").unwrap().0.as_slice()); ```
traits: ValidateString
, ValidateBytes
```rust
extern crate validators;
use validators::prelude::*;
pub struct Base64WithPadding(String);
assert!(Base64WithPadding::parsestring("MTIzNDU2Nzg5MA==").isok()); assert!(Base64WithPadding::parsestring("MTIzNDU2Nzg5MA").iserr()); ```
traits: ValidateString
, ValidateBytes
, CollectionLength
```rust
extern crate validators;
use validators::prelude::*;
pub struct Base64WithPaddingDecoded(Vec
asserteq!(b"1234567890", Base64WithPaddingDecoded::parsestring("MTIzNDU2Nzg5MA==").unwrap().0.as_slice()); ```
traits: ValidateString
, ValidateBytes
```rust
extern crate validators;
use validators::prelude::*;
pub struct Base64WithoutPaddingUrl(String);
assert!(Base64WithoutPaddingUrl::parsestring("PmR8hJhjgVNcB61zqhcB2duZ7ld8Gy1GW2xSBVzeno").is_ok()); ```
traits: ValidateString
, ValidateBytes
, CollectionLength
```rust
extern crate validators;
use validators::prelude::*;
pub struct Base64WithoutPaddingUrlDecoded(Vec
asserteq!([62, 100, 124, 132, 152, 99, 129, 83, 92, 7, 173, 115, 170, 23, 63, 7, 103, 110, 103, 185, 93, 240, 108, 181, 25, 109, 177, 72, 21, 115, 122, 122], Base64WithoutPaddingUrlDecoded::parsestring("PmR8hJhjgVNcB61zqhcB2duZ7ld8Gy1GW2xSBVzeno").unwrap().0.asslice()); ```
traits: ValidateString
, ValidateChar
, ValidateSignedInteger
, ValidateUnignedInteger
```rust
extern crate validators;
use validators::prelude::*;
pub struct Boolean(bool);
asserteq!(true, Boolean::parsestr("true").unwrap().0); asserteq!(false, Boolean::parsestr("f").unwrap().0); asserteq!(true, Boolean::parsestr("y").unwrap().0); asserteq!(false, Boolean::parsestr("no").unwrap().0); asserteq!(true, Boolean::parsestr("on").unwrap().0); asserteq!(false, Boolean::parsestr("off").unwrap().0); asserteq!(true, Boolean::parsestr("1").unwrap().0);
asserteq!(true, Boolean::parsechar('t').unwrap().0); asserteq!(false, Boolean::parsechar('0').unwrap().0);
asserteq!(true, Boolean::parseisize(1).unwrap().0); ```
traits: ValidateString
additional methods: is_fully_qualified
, get_domain_non_fully_qualified
, to_uri_authority_string
```rust
extern crate validators;
use validators::prelude::*;
pub struct DomainWithoutPort(pub String);
assert!(DomainWithoutPort::parsestring("example.com").isok()); asserteq!("xn--fiq228c.com", DomainWithoutPort::parsestring("中文.com").unwrap().0);
pub struct DomainAllowPort {
pub domain: String,
port: Option
asserteq!(Some(8080), DomainAllowPort::parsestring("example.com:8080").unwrap().port); ```
traits: ValidateString
additional methods: to_email_string
```rust
extern crate validators;
use validators::prelude::*;
pub struct EmailAllowComment {
pub localpart: String,
pub needquoted: bool,
pub domainpart: validators::models::Host,
pub commentbeforelocalpart: Option
assert!(EmailAllowComment::parsestring("(john)joke@example.com").isok());
pub struct EmailNotAllowComment { pub localpart: String, pub needquoted: bool, pub domain_part: validators::models::Host, }
assert!(EmailNotAllowComment::parsestring("(john)joke@example.com").iserr()); ```
traits: ValidateString
additional methods: to_uri_authority_string
```rust
extern crate validators;
use validators::prelude::*;
pub struct HostMustAtLeastTwoLabelsAllowPort {
pub host: validators::models::Host,
pub port: Option
assert!(HostMustAtLeastTwoLabelsAllowPort::parsestring("example.com:8000").isok()); assert!(HostMustAtLeastTwoLabelsAllowPort::parsestring("example").iserr()); ```
traits: ValidateString
```rust
extern crate validators;
use validators::prelude::*; use validators_prelude::url;
pub struct HttpURL { url: url::Url, is_https: bool, }
assert!(HttpURL::parsestring("https://example.org/").isok()); assert!(HttpURL::parsestring("http://example.org/").isok()); assert!(HttpURL::parsestring("ftp://example.org/").iserr()); ```
traits: ValidateString
```rust
extern crate validators;
use validators::prelude::*; use validators_prelude::url;
pub struct HttpFtpURL { url: url::Url, protocol: validators::models::Protocol, }
assert!(HttpFtpURL::parsestring("https://example.org/").isok()); assert!(HttpFtpURL::parsestring("http://example.org/").isok()); assert!(HttpFtpURL::parsestring("ftp://example.org/").isok()); ```
traits: ValidateString
additional methods: to_uri_authority_string
```rust
extern crate validators;
use std::net::IpAddr;
use validators::prelude::*;
pub struct IPAllowPort {
pub ip: IpAddr,
pub port: Option
assert!(IPAllowPort::parsestring("127.0.0.1").isok()); assert!(IPAllowPort::parsestring("[::ffff:c000:0280]:8000").isok()); ```
traits: ValidateString
additional methods: to_uri_authority_string
```rust
extern crate validators;
use std::net::Ipv4Addr;
use validators::prelude::*;
pub struct IPv4WithoutPort(pub Ipv4Addr);
assert!(IPv4WithoutPort::parsestring("127.0.0.1").isok()); ```
traits: ValidateString
additional methods: to_uri_authority_string
```rust
extern crate validators;
use std::net::Ipv6Addr;
use validators::prelude::*;
pub struct IPv6WithoutPort(pub Ipv6Addr);
assert!(IPv6WithoutPort::parsestring("::ffff:c000:0280").isok()); assert!(IPv6WithoutPort::parsestring("[::ffff:c000:0280]").isok()); ```
traits: ValidateString
, ValidateSignedInteger
, ValidateUnignedInteger
, ValidateNumber
, ValidateBoolean
additional methods: to_minfied_json_string
, to_beautified_json_string
```rust
extern crate validators;
use validators::prelude::*;
pub struct JSONString(pub String);
pub struct JSONNumber(pub f64);
pub struct JSONBoolean(pub bool);
assert!(JSONString::parsestring("123").iserr()); assert!(JSONString::parsestring("\"123\"").isok()); assert!(JSONNumber::parseu64(123).isok()); assert!(JSONBoolean::parsebool(false).isok()); ```
traits: ValidateLength
, CollectionLength
```rust
extern crate validators;
use validators::prelude::*;
pub struct NonEmptyNotTooLongVec(pub Vec
assert!(NonEmptyNotTooLongVec::parsecollection(vec![]).iserr()); assert!(NonEmptyNotTooLongVec::parsecollection(vec![0]).isok()); assert!(NonEmptyNotTooLongVec::parsecollection(vec![0, 1, 2, 3]).iserr()); ```
traits: ValidateString
```rust
extern crate validators;
use validators::prelude::*;
pub struct LineNotAllowEmpty(pub String);
assert!(LineNotAllowEmpty::parsestring("123").isok()); assert!(LineNotAllowEmpty::parsestring("123\0").iserr()); assert!(LineNotAllowEmpty::parsestring("123\n456").iserr()); assert!(LineNotAllowEmpty::parsestring(" ").iserr()); ```
traits: ValidateString
additional methods: to_mac_address_string
```rust
extern crate validators;
use validators::prelude::*;
pub struct MacAddress(pub u64);
assert!(MacAddress::parsestring("080027B246C3").isok()); assert!(MacAddress::parsestring("08:00:27:B2:46:C3").isok()); ```
The default value of the separator
option is Allow(colon)
.
traits: ValidateString
, ValidateNumber
```rust
extern crate validators;
use validators::prelude::*;
pub struct Double(pub f64);
assert!(Double::parsestring("123.456").isok()); assert!(Double::parsestring("NaN").iserr()); assert!(Double::parsef32(123.4).isok());
pub struct SinglePercentage(pub f32);
assert!(SinglePercentage::parsestring("0").isok()); assert!(SinglePercentage::parsestring("1").isok()); assert!(SinglePercentage::parsestring("1.1").iserr()); assert!(SinglePercentage::parsestring("NaN").isok()); ```
traits: ValidateString
```rust
extern crate validators;
use validators::prelude::*; use validators_prelude::phonenumber;
use std::collections::HashMap;
pub struct InternationalPhone(pub phonenumber::PhoneNumber);
pub struct TWPhone(pub phonenumber::PhoneNumber);
pub struct TWorUSPhone(
pub HashMap
assert!(InternationalPhone::parsestring("+886912345678").isok()); assert!(InternationalPhone::parsestring("0912345678").iserr()); assert!(InternationalPhone::parsestring("+14155552671").isok());
assert!(TWPhone::parsestring("+886912345678").isok()); assert!(TWPhone::parsestring("0912345678").isok()); assert!(TWPhone::parsestring("+14155552671").iserr());
assert!(TWorUSPhone::parsestring("+886912345678").isok()); assert!(TWorUSPhone::parsestring("0912345678").isok()); assert!(TWorUSPhone::parsestring("+14155552671").isok()); ```
traits: ValidateString
```rust
extern crate validators;
extern crate once_cell;
use validators::prelude::*; use validators_prelude::regex;
use once_cell::sync::Lazy;
lazystatic! { static ref RENONZERONUMBERS: regex::Regex = regex::Regex::new("^[1-9]+$").unwrap(); }
static RE_POKER: Lazy
pub struct Hex(pub String); // this compiles the regex every time
pub struct NonZeroNumbers(pub String);
pub struct Poker(pub String);
assert!(Hex::parsestring("1Ab").isok()); assert!(Hex::parsestring("1AG").iserr());
assert!(NonZeroNumbers::parsestring("12345").isok()); assert!(NonZeroNumbers::parsestring("012345").iserr());
assert!(Poker::parsestring("1").isok()); assert!(Poker::parsestring("10").isok()); assert!(Poker::parsestring("J").isok()); assert!(Poker::parsestring("0").iserr()); ```
traits: ValidateString
```rust
extern crate validators;
use validators::prelude::*; use validators_prelude::semver;
pub struct SemVer(semver::Version);
assert!(SemVer::parsestring("0.0.0").isok()); assert!(SemVer::parsestring("0.0.0-beta.1").isok()); ```
traits: ValidateString
```rust
extern crate validators;
use validators::prelude::*; use validators_prelude::semver;
pub struct SemVerReq(semver::VersionReq);
assert!(SemVerReq::parsestring("0.0.0").isok()); assert!(SemVerReq::parsestring(">= 0.4").isok()); ```
traits: ValidateString
, ValidateSignedInteger
```rust
extern crate validators;
use validators::prelude::*;
pub struct Score(i8);
assert!(Score::parsestring("0").isok()); assert!(Score::parsestring("-2").iserr()); assert!(Score::parsei8(4).isok());
pub struct NonZeroShort(i16);
assert!(NonZeroShort::parsei8(4).isok()); assert!(NonZeroShort::parsei8(-4).isok()); assert!(NonZeroShort::parsei8(0).iserr()); ```
traits: ValidateString
```rust
extern crate validators;
use validators::prelude::*;
pub struct TextNotAllowEmpty(pub String);
assert!(TextNotAllowEmpty::parsestring("123").isok()); assert!(TextNotAllowEmpty::parsestring("123\0").iserr()); assert!(TextNotAllowEmpty::parsestring("123\n456").isok()); assert!(TextNotAllowEmpty::parsestring(" ").iserr()); ```
traits: ValidateString
, ValidateUnignedInteger
```rust
extern crate validators;
use validators::prelude::*;
pub struct Count(u8);
assert!(Count::parsestring("5").isok()); assert!(Count::parsestring("0").iserr()); assert!(Count::parseu8(4).isok()); ```
traits: ValidateString
```rust
extern crate validators;
use validators::prelude::*; use validators_prelude::url;
pub struct URL(pub url::Url);
assert!(URL::parsestring("https://example.org/").isok()); assert!(URL::parsestring("https:example.org").isok()); assert!(URL::parsestring("example:").isok()); ```
traits: ValidateString
additional methods: to_uuid_string
```rust
extern crate validators;
use validators::prelude::*;
pub struct UUID(pub u128);
assert!(UUID::parsestring("A866664AF9D34DDE89CB182015FA4F41").isok()); assert!(UUID::parsestring("A866664A-F9D3-4DDE-89CB-182015FA4F41").isok()); ```
The default value of the separator
option is Allow(hyphen)
.
https://crates.io/crates/validators
https://docs.rs/validators