Wary
An optionally no_std and no_alloc validation and transformation library.
- Basic usage
- Accessing context
- Validation rules
- Transformation rules
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 |
|---|---|---|
addr |
AsRef<str> |
- |
alphanumeric |
AsRef<str> |
- |
ascii |
AsRef<str> |
- |
contains |
AsSlice |
- |
custom |
Rule<T> |
- |
dive |
Validate |
- |
email |
AsRef<str> |
email |
equals |
std::cmp::PartialEq |
- |
func |
Fn(&T) -> Result<(), wary::Error> |
- |
inner |
AsSlice |
- |
length |
Length |
graphemes (optional, for graphemes length) |
lowercase |
AsRef<str> |
- |
prefix |
AsSlice |
- |
range |
Range |
- |
regex |
AsRef<str> |
regex |
required |
AsSlice |
- |
semver |
AsRef<str> |
semver |
suffix |
AsSlice |
- |
uppercase |
AsRef<str> |
- |
url |
AsRef<str> |
url |
addr
Validates an address (currently only an IP).
use Wary;
alphanumeric
Validates that the input is alphanumeric.
use Wary;
ascii
Validates that the input is ascii.
use Wary;
]
String
);
contains
Validates that the input contains a substring or subslice.
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;
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
);
uppercase
Validates that the input is uppercase.
use Wary;
url (requires feature url)
Validates that the input is a url.
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 Modify trait instead.
| rule | trait | feature |
|---|---|---|
custom |
Modifier |
- |
lowercase |
AsMut<str> (for ascii only) |
- |
inner |
AsMutSlice |
- |
uppercase |
AsMut<str> (for ascii only) |
- |
custom
Transforms the input with a custom Modifier.
use ;
;
#
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 Modify manually
use Modify;
let mut name = Name ;
name.modify;
assert_eq!;