Wary
An optionally no_std and no_alloc validation and transformation library.
Why use wary over other libraries?
| - | wary |
garde |
validator |
validify |
|---|---|---|---|---|
no_std |
✅ | ❌ | ❌ | |
no_alloc |
✅ | ❌ | ❌ | ❌ |
| async | ✅ (optional) | ❌ | ❌ | ❌ |
| enums | ✅ | ✅ | ❌ | ✅ |
| transform input | ✅ | ❌ | ❌ | ✅ |
| custom rules | ✅ | ✅ | ✅ | ✅ |
| pass context | ✅ | ✅ | ✅ | ❌ |
respect serde field attributes |
✅ | ❌ | ❌ | ❌ |
Basic struct example
use Cow;
use Wary;
, equals)]
);
let mut person = Person ;
if let Err = person.wary
Basic enum example
use Cow;
use Wary;
, equals)]
&'n mut str
);
// for length(bytes)
let mut name = "Jane".to_string;
let mut person = Adult ;
if let Err = person.wary else
Accessing context
use Wary;
use *;
use Range;
// allows one context to be passed to all rules
#
Validation rules
Validation rules applied through the proc-macro Wary attribute are (for the most part) simply forwarded
directly to their respective builders inside the rule module. As a result of this
decision, all rules (except and, or, inner, and dive) will have auto-completion when writing macro attributes!
If you're providing no options to a rule, you can omit the parentheses. For example: #[validate(alphanumeric)]
and #[validate(alphanumeric())] are equivalent.
| rule | trait | feature | dependency |
|---|---|---|---|
addr |
AsRef<str> |
- | - |
alphanumeric |
AsRef<str> |
- | - |
and |
- | - | - |
ascii |
AsRef<str> |
- | - |
contains |
AsSlice |
- | - |
credit_card |
AsRef<str> |
credit_card |
creditcard |
custom |
Rule<T> |
- | - |
dive |
Validate |
- | - |
email |
AsRef<str> |
email |
email_address |
equals |
std::cmp::PartialEq |
- | - |
func |
Fn(&T) -> Result<(), wary::Error> |
- | - |
inner |
AsSlice |
- | - |
length |
Length |
graphemes* |
unicode-segmentation |
lowercase |
AsRef<str> |
- | - |
or |
- | - | - |
prefix |
AsSlice |
- | - |
range |
Compare |
- | - |
regex |
AsRef<str> |
regex |
regex |
required |
AsSlice |
- | - |
semver |
AsRef<str> |
semver |
semver |
suffix |
AsSlice |
- | - |
time |
- | - | jiff or chrono |
uppercase |
AsRef<str> |
- | - |
url |
AsRef<str> |
url |
url |
uuid |
AsRef<str> |
uuid |
uuid |
* optional
addr
Validates an address (currently only an IP).
use Wary;
alphanumeric
Validates that the input is alphanumeric.
use Wary;
and
Meta-rule that combines multiple rules. Unlike other rule lists, this one short-circuits on the first error.
use ;
let name = NameAnd ;
let report = name.validate.unwrap_err;
assert_eq!;
let name = Name ;
let report = name.validate.unwrap_err;
assert_eq!;
ascii
Validates that the input is ascii.
use Wary;
]
String
);
contains
Validates that the input contains a substring or subslice.
use Wary;
)]
String
);
credit_card (requires feature credit_card)
Validates that the input is a credit card number (PAN).
use Wary;
]
String
);
custom
Validates the input with a custom Rule.
use Wary;
use *;
;
#
dive
Validates the inner fields of a struct or enum.
use Wary;
email (requires feature email)
Validates that the input is an email.
use Wary;
]
String
);
equals
Validates that the input is equal to a value. Currently does not support self fields.
use Wary;
)]
String
);
func
Validates the input with a function.
use ;
inner
Validates the inner fields of a slice-like type.
use Wary;
length
Validates the length of the input.
use Wary;
lowercase
Validates that the input is lowercase.
use Wary;
or
Meta-rule that combines multiple rules. Short-circuits on the first success.
use ;
use ;
;
static DEBUG_COUNTER: AtomicUsize = new;
#
prefix
Validates that the input starts with a substring or subslice.
use Wary;
)]
String
);
range
Validates that the input is within a range.
use Wary;
regex (requires feature regex)
Validates that the input matches a regex.
use Wary;
)]
String
);
required
Validates that the input is not empty. For example, that an Option is Some or a Vec is not empty.
use Wary;
semver (requires feature semver)
Validates that the input is a semver.
use Wary;
]
String
);
suffix
Validates that the input ends with a substring or subslice.
use Wary;
)]
String
);
time (requires feature chrono or jiff)
Validates that the input time is within a range.
use Wary;
use Zoned;
))]
Zoned
);
uppercase
Validates that the input is uppercase.
use Wary;
url (requires feature url)
Validates that the input is a url.
use Wary;
]
String
);
uuid (requires feature uuid)
Validates that the input is a uuid.
use Wary;
]
String
);
Implementing Validate manually
In the rare case you need to manually implement Validate, you will need to keep in mind about reporting errors properly.
use ;
let name = Name ;
assert!;
let longer = Name ;
assert!;
Transformation rules
Transformation rules are applied similarly to validation rules, but are implemented in the Transform trait instead.
| rule | trait | feature | dependency |
|---|---|---|---|
custom |
Transformer |
- | - |
dive |
Transform |
- | - |
lowercase |
AsMut<str> (for ascii only) |
- | - |
inner |
AsMutSlice |
- | - |
uppercase |
AsMut<str> (for ascii only) |
- | - |
custom
Transforms the input with a custom Transformer.
use ;
;
#
dive
Transforms the inner fields of a struct or enum.
use Wary;
lowercase
Transforms the input to lowercase.
use Wary;
inner
Transforms the inner fields of a slice-like type.
use Wary;
uppercase
Transforms the input to uppercase.
use Wary;
Implementing Transform manually
use Transform;
let mut name = Name ;
name.transform;
assert_eq!;
Async support
Wary supports async validation and transformation out of the box. This is useful for cases where a validation step may need to reach out to a database or an external service.
All traits have an async variant:
Wary->AsyncWaryValidate->AsyncValidateRule->AsyncRuleTransform->AsyncTransformTransformer->AsyncTransformer
use ;
;
async