validkit
Typed newtypes for validated domain primitives — replaces hand-rolled is_valid_* checks with parse-once, use-everywhere strong types.
Purpose
validkit is an L0 leaf crate providing validated newtypes for emails, URLs, cron expressions, tenant slugs, locale tags, flag names, S3 bucket names, object keys, and E.164 phones. Each type guarantees its invariants at construction time.
Types
| Type | Validation |
|---|---|
EmailAddr |
RFC 5321 + IDNA, injection-safe, single @, 254 chars |
HttpsUrl |
https only, no credentials, host required |
PhoneE164 |
E.164 ^\+[1-9]\d{1,14}$ |
CronExpr |
5-field cron (* or [\d,/\-]+) |
TenantIdSlug |
3–63 chars, [a-z0-9-], no .., no .lock |
LocaleTag |
BCP 47 ^[a-z]{2,3}(-[A-Za-z0-9]+)*$, max 35 |
FlagName |
^[a-z][a-z0-9_]*$ |
BucketName |
S3 bucket rules, not IP, 3–63 chars |
ObjectKey |
S3 object key, no .. traversal, ≤1024, no leading / |
Features
std(default) — enablesstdsupportserde—Serialize/Deserializetransparenturl— useurlcrate forHttpsUrlregex— regex-backed validation (more precise)chrono— chrono integration (placeholder for timestamp types)idna— IDNA domain validation for emailsderive—#[derive(Validated)]struct-field validation (proc macro; not part offull/default, keeps theno_stdcore intact)full— enables the optional integration features above (notderive)no_std—no_stdcompatible (extern crate alloc)
No unsafe code (#![forbid(unsafe_code)]), #![deny(missing_docs)].
Derive macro
With the derive feature, #[derive(Validated)] generates a
validate(&self) -> Result<(), ValidError> method from field annotations:
use ;
assert!;
Checks run in field declaration order and stop at the first failure.
See the validkit-derive crate docs for the full attribute grammar.
Usage
use ;
let email = parse.expect;
assert_eq!;
let url = parse.expect;
assert!;
let tenant = parse.expect;
assert_eq!;
// All types impl TryFrom<String>, FromStr, Display, Deref<Target=str>, AsRef<str>
let flag: FlagName = "my_flag".parse.expect;
No_Std
= { = "0.1", = false }
License
MIT OR Apache-2.0
Security
Threat model: THREAT-MODEL.md.