Expand description
§validkit
Typed newtypes for validated domain primitives — replaces hand-rolled
is_valid_* checks with parse-once, use-everywhere strong types.
| Type | Validation |
|---|---|
EmailAddr | RFC 5321 + IDNA, injection-safe |
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 |
ObjectKey | S3 object key, no .. traversal, ≤1024, no leading / |
§Example
use validkit::{EmailAddr, HttpsUrl, TenantIdSlug};
let email = EmailAddr::parse("alice@example.com").expect("valid");
assert_eq!(email.as_str(), "alice@example.com");
let url = HttpsUrl::parse("https://example.com").expect("valid");
assert!(url.as_str().starts_with("https://"));
let tenant = TenantIdSlug::parse("my-tenant").expect("valid");
assert_eq!(tenant.as_str(), "my-tenant");All types implement TryFrom<String>, FromStr, Display, Deref<Target=str>,
and optional serde support (transparent serialization; deserialization
runs full validation, so invalid values cannot enter through JSON).
§Derive macro
Behind the optional, non-default derive feature, #[derive(Validated)]
generates a validate(&self) -> Result<(), ValidError> method that checks
fields annotated with #[validate(...)]:
#[cfg(feature = "derive")]
fn derive_example() {
use validkit::{Validated, Validate};
#[derive(Validated)]
struct Contact {
#[validate(email)]
email: String,
#[validate(url)]
homepage: Option<String>,
#[validate(length(min = 1, max = 100))]
name: String,
#[validate(range(min = 0.0, max = 1.0))]
trust: f64,
#[validate(postcode_uk)]
postcode: String,
}
let contact = Contact {
email: "alice@example.com".to_string(),
homepage: None,
name: "Alice".to_string(),
trust: 0.9,
postcode: "SW1A 1AA".to_string(),
};
assert!(contact.validate().is_ok());
}Newtype-typed fields (EmailAddr, HttpsUrl) are supported as leaves —
the derive re-uses the newtype’s validation rather than re-implementing
it.
Re-exports§
pub use bucket::is_valid_bucket_name;pub use bucket::BucketName;pub use cron::is_valid_cron;pub use cron::CronExpr;pub use derive_support::check_postcode_uk;pub use email::is_valid_email;pub use email::EmailAddr;pub use email::Validate;pub use error::invalid_field;pub use error::ValidError;pub use flag_name::is_valid_flag_name;pub use flag_name::FlagName;pub use locale::is_valid_locale;pub use locale::LocaleTag;pub use object_key::is_valid_object_key;pub use object_key::is_valid_object_key_strict;pub use object_key::ObjectKey;pub use phone::PhoneE164;pub use tenant::is_valid_tenant_id;pub use tenant::TenantIdSlug;pub use url::HttpsUrl;
Modules§
- bucket
- S3 bucket name newtype.
- cron
- Cron expression newtype (5-field).
- derive_
support - Internal support code for the
Validatedderive macro. - Email address newtype with strict validation.
- error
- Error types for
validkit. - flag_
name - Flag name newtype.
- locale
- BCP 47 locale tag newtype.
- object_
key - Object key (S3 object key) newtype.
- openapi
openapi - JSON Schema / OpenAPI support for validkit newtypes.
- phone
- E.164 phone number newtype.
- tenant
- Tenant ID slug newtype.
- url
- HTTPS URL newtype.
Derive Macros§
- Validated
derive - Derive
Validated— see the crate documentation for the attribute grammar.