pub struct ZString { /* private fields */ }Expand description
Schema for string validation. Created via vld::string().
§Example
use vld::prelude::*;
let schema = vld::string().min(3).max(20).email();Implementations§
Source§impl ZString
impl ZString
pub fn new() -> ZString
Sourcepub fn type_error(self, msg: impl Into<String>) -> ZString
pub fn type_error(self, msg: impl Into<String>) -> ZString
Set a custom error message for type mismatch (when the input is not a string).
§Example
use vld::prelude::*;
let schema = vld::string().type_error("Must be text!");
let err = schema.parse("42").unwrap_err();
assert!(err.issues[0].message.contains("Must be text!"));Sourcepub fn with_messages<F>(self, f: F) -> ZString
pub fn with_messages<F>(self, f: F) -> ZString
Override error messages in bulk by check key.
The closure receives the check key (e.g. "too_small", "invalid_email")
and should return Some(new_message) to replace, or None to keep the original.
Available keys: "too_small", "too_big", "invalid_length", "invalid_email",
"invalid_url", "invalid_uuid", "invalid_ip", "invalid_regex",
"invalid_starts_with", "invalid_ends_with", "invalid_contains", "non_empty",
"invalid_ipv4", "invalid_ipv6", "invalid_cidr", "invalid_mac", "invalid_hex",
"invalid_credit_card", "invalid_phone", "invalid_semver", "invalid_jwt",
"invalid_ascii", "invalid_alpha", "invalid_alphanumeric", "invalid_lowercase",
"invalid_uppercase", "invalid_base64", "invalid_iso_date",
"invalid_iso_datetime", "invalid_iso_time", "invalid_hostname",
"invalid_cuid2", "invalid_ulid", "invalid_nanoid", "invalid_emoji".
§Example
use vld::prelude::*;
let schema = vld::string().min(3).email()
.with_messages(|key| match key {
"too_small" => Some("Too short!".into()),
"invalid_email" => Some("Bad email!".into()),
_ => None,
});Sourcepub fn min_msg(self, len: usize, msg: impl Into<String>) -> ZString
pub fn min_msg(self, len: usize, msg: impl Into<String>) -> ZString
Minimum string length with custom message.
Sourcepub fn max_msg(self, len: usize, msg: impl Into<String>) -> ZString
pub fn max_msg(self, len: usize, msg: impl Into<String>) -> ZString
Maximum string length with custom message.
Sourcepub fn len_msg(self, len: usize, msg: impl Into<String>) -> ZString
pub fn len_msg(self, len: usize, msg: impl Into<String>) -> ZString
Exact string length with custom message.
Sourcepub fn email_msg(self, msg: impl Into<String>) -> ZString
pub fn email_msg(self, msg: impl Into<String>) -> ZString
Must be a valid email address, with custom message.
Sourcepub fn url_msg(self, msg: impl Into<String>) -> ZString
pub fn url_msg(self, msg: impl Into<String>) -> ZString
Must be a valid URL, with custom message.
Sourcepub fn uuid_msg(self, msg: impl Into<String>) -> ZString
pub fn uuid_msg(self, msg: impl Into<String>) -> ZString
Must be a valid UUID, with custom message.
Sourcepub fn ip_msg(self, msg: impl Into<String>) -> ZString
pub fn ip_msg(self, msg: impl Into<String>) -> ZString
Must be a valid IP address (IPv4 or IPv6), with custom message.
Sourcepub fn starts_with(self, prefix: impl Into<String>) -> ZString
pub fn starts_with(self, prefix: impl Into<String>) -> ZString
Must start with the given prefix.
Sourcepub fn starts_with_msg(
self,
prefix: impl Into<String>,
msg: impl Into<String>,
) -> ZString
pub fn starts_with_msg( self, prefix: impl Into<String>, msg: impl Into<String>, ) -> ZString
Must start with the given prefix, with custom message.
Sourcepub fn ends_with_msg(
self,
suffix: impl Into<String>,
msg: impl Into<String>,
) -> ZString
pub fn ends_with_msg( self, suffix: impl Into<String>, msg: impl Into<String>, ) -> ZString
Must end with the given suffix, with custom message.
Sourcepub fn contains_msg(
self,
sub: impl Into<String>,
msg: impl Into<String>,
) -> ZString
pub fn contains_msg( self, sub: impl Into<String>, msg: impl Into<String>, ) -> ZString
Must contain the given substring, with custom message.
Sourcepub fn non_empty_msg(self, msg: impl Into<String>) -> ZString
pub fn non_empty_msg(self, msg: impl Into<String>) -> ZString
Must not be empty, with custom message.
Sourcepub fn to_lowercase(self) -> ZString
pub fn to_lowercase(self) -> ZString
Convert to lowercase before validation.
Sourcepub fn to_uppercase(self) -> ZString
pub fn to_uppercase(self) -> ZString
Convert to uppercase before validation.
Sourcepub fn ipv4_msg(self, msg: impl Into<String>) -> ZString
pub fn ipv4_msg(self, msg: impl Into<String>) -> ZString
Must be a valid IPv4 address, with custom message.
Sourcepub fn ipv6_msg(self, msg: impl Into<String>) -> ZString
pub fn ipv6_msg(self, msg: impl Into<String>) -> ZString
Must be a valid IPv6 address, with custom message.
Sourcepub fn cidr_msg(self, msg: impl Into<String>) -> ZString
pub fn cidr_msg(self, msg: impl Into<String>) -> ZString
Must be a valid CIDR block, with custom message.
Sourcepub fn mac_msg(self, msg: impl Into<String>) -> ZString
pub fn mac_msg(self, msg: impl Into<String>) -> ZString
Must be a valid MAC address, with custom message.
Sourcepub fn hex_msg(self, msg: impl Into<String>) -> ZString
pub fn hex_msg(self, msg: impl Into<String>) -> ZString
Must contain only hexadecimal characters, with custom message.
Sourcepub fn credit_card(self) -> ZString
pub fn credit_card(self) -> ZString
Must be a valid credit card number (Luhn check).
Sourcepub fn credit_card_msg(self, msg: impl Into<String>) -> ZString
pub fn credit_card_msg(self, msg: impl Into<String>) -> ZString
Must be a valid credit card number, with custom message.
Sourcepub fn phone(self) -> ZString
pub fn phone(self) -> ZString
Must be a valid international phone number (E.164-like, starts with +).
Sourcepub fn phone_msg(self, msg: impl Into<String>) -> ZString
pub fn phone_msg(self, msg: impl Into<String>) -> ZString
Must be a valid international phone number, with custom message.
Sourcepub fn semver(self) -> ZString
pub fn semver(self) -> ZString
Must be a valid semantic version (MAJOR.MINOR.PATCH, optional v prefix).
Sourcepub fn semver_msg(self, msg: impl Into<String>) -> ZString
pub fn semver_msg(self, msg: impl Into<String>) -> ZString
Must be a valid semantic version, with custom message.
Sourcepub fn jwt(self) -> ZString
pub fn jwt(self) -> ZString
Must be a JWT-like token (header.payload.signature, base64url parts).
Sourcepub fn jwt_msg(self, msg: impl Into<String>) -> ZString
pub fn jwt_msg(self, msg: impl Into<String>) -> ZString
Must be a JWT-like token, with custom message.
Sourcepub fn ascii_msg(self, msg: impl Into<String>) -> ZString
pub fn ascii_msg(self, msg: impl Into<String>) -> ZString
Must contain only ASCII characters, with custom message.
Sourcepub fn alpha_msg(self, msg: impl Into<String>) -> ZString
pub fn alpha_msg(self, msg: impl Into<String>) -> ZString
Must contain only alphabetic ASCII characters, with custom message.
Sourcepub fn alphanumeric(self) -> ZString
pub fn alphanumeric(self) -> ZString
Must contain only ASCII alphanumeric characters.
Sourcepub fn alphanumeric_msg(self, msg: impl Into<String>) -> ZString
pub fn alphanumeric_msg(self, msg: impl Into<String>) -> ZString
Must contain only ASCII alphanumeric characters, with custom message.
Sourcepub fn lowercase_msg(self, msg: impl Into<String>) -> ZString
pub fn lowercase_msg(self, msg: impl Into<String>) -> ZString
Must already be lowercase, with custom message.
Sourcepub fn uppercase_msg(self, msg: impl Into<String>) -> ZString
pub fn uppercase_msg(self, msg: impl Into<String>) -> ZString
Must already be uppercase, with custom message.
Sourcepub fn base64_msg(self, msg: impl Into<String>) -> ZString
pub fn base64_msg(self, msg: impl Into<String>) -> ZString
Must be a valid Base64 string, with custom message.
Sourcepub fn iso_date_msg(self, msg: impl Into<String>) -> ZString
pub fn iso_date_msg(self, msg: impl Into<String>) -> ZString
Must be a valid ISO 8601 date, with custom message.
Sourcepub fn iso_datetime(self) -> ZString
pub fn iso_datetime(self) -> ZString
Must be a valid ISO 8601 datetime.
Sourcepub fn iso_datetime_msg(self, msg: impl Into<String>) -> ZString
pub fn iso_datetime_msg(self, msg: impl Into<String>) -> ZString
Must be a valid ISO 8601 datetime, with custom message.
Sourcepub fn iso_time_msg(self, msg: impl Into<String>) -> ZString
pub fn iso_time_msg(self, msg: impl Into<String>) -> ZString
Must be a valid ISO 8601 time, with custom message.
Sourcepub fn hostname_msg(self, msg: impl Into<String>) -> ZString
pub fn hostname_msg(self, msg: impl Into<String>) -> ZString
Must be a valid hostname, with custom message.
Sourcepub fn cuid2(self) -> ZString
pub fn cuid2(self) -> ZString
Must be a valid CUID2 string (lowercase alphanumeric, starts with a letter).
Sourcepub fn cuid2_msg(self, msg: impl Into<String>) -> ZString
pub fn cuid2_msg(self, msg: impl Into<String>) -> ZString
Must be a valid CUID2 string, with custom message.
Sourcepub fn ulid_msg(self, msg: impl Into<String>) -> ZString
pub fn ulid_msg(self, msg: impl Into<String>) -> ZString
Must be a valid ULID, with custom message.
Sourcepub fn nanoid_msg(self, msg: impl Into<String>) -> ZString
pub fn nanoid_msg(self, msg: impl Into<String>) -> ZString
Must be a valid Nano ID, with custom message.
Sourcepub fn emoji_msg(self, msg: impl Into<String>) -> ZString
pub fn emoji_msg(self, msg: impl Into<String>) -> ZString
Must contain an emoji, with custom message.
pub fn slug_msg(self, msg: impl Into<String>) -> ZString
pub fn color_msg(self, msg: impl Into<String>) -> ZString
Sourcepub fn currency_code(self) -> ZString
pub fn currency_code(self) -> ZString
Must be a currency code (ISO-4217-like uppercase 3 letters).
pub fn currency_code_msg(self, msg: impl Into<String>) -> ZString
Sourcepub fn country_code(self) -> ZString
pub fn country_code(self) -> ZString
Must be a country code (ISO-3166 alpha-2).
pub fn country_code_msg(self, msg: impl Into<String>) -> ZString
pub fn locale_msg(self, msg: impl Into<String>) -> ZString
pub fn cron_msg(self, msg: impl Into<String>) -> ZString
Trait Implementations§
Source§impl VldSchema for ZString
impl VldSchema for ZString
Source§fn parse_value(&self, value: &Value) -> Result<String, VldError>
fn parse_value(&self, value: &Value) -> Result<String, VldError>
serde_json::Value.Source§fn parse<I>(&self, input: &I) -> Result<Self::Output, VldError>
fn parse<I>(&self, input: &I) -> Result<Self::Output, VldError>
serde_json::Value, etc.)Source§fn validate<T>(&self, value: &T) -> Result<Self::Output, VldError>where
T: Serialize,
fn validate<T>(&self, value: &T) -> Result<Self::Output, VldError>where
T: Serialize,
Source§fn is_valid<T>(&self, value: &T) -> boolwhere
T: Serialize,
fn is_valid<T>(&self, value: &T) -> boolwhere
T: Serialize,
Source§fn optional(self) -> ZOptional<Self>
fn optional(self) -> ZOptional<Self>
None.Source§fn with_default(self, value: Self::Output) -> ZDefault<Self>
fn with_default(self, value: Self::Output) -> ZDefault<Self>
Source§fn refine<F>(self, check: F, message: &str) -> ZRefine<Self, F>
fn refine<F>(self, check: F, message: &str) -> ZRefine<Self, F>
Source§fn transform<F, U>(self, f: F) -> ZTransform<Self, F, U>
fn transform<F, U>(self, f: F) -> ZTransform<Self, F, U>
Source§fn catch(self, fallback: Self::Output) -> ZCatch<Self>
fn catch(self, fallback: Self::Output) -> ZCatch<Self>
Source§fn pipe<S>(self, next: S) -> ZPipe<Self, S>
fn pipe<S>(self, next: S) -> ZPipe<Self, S>
Source§fn describe(self, description: &str) -> ZDescribe<Self>
fn describe(self, description: &str) -> ZDescribe<Self>
Source§fn super_refine<F>(self, check: F) -> ZSuperRefine<Self, F>
fn super_refine<F>(self, check: F) -> ZSuperRefine<Self, F>
Source§fn or<B>(self, other: B) -> ZUnion2<Self, B>where
B: VldSchema,
fn or<B>(self, other: B) -> ZUnion2<Self, B>where
B: VldSchema,
Either<Self::Output, B::Output>.Source§fn and<B>(self, other: B) -> ZIntersection<Self, B>where
B: VldSchema,
fn and<B>(self, other: B) -> ZIntersection<Self, B>where
B: VldSchema,
Auto Trait Implementations§
impl Freeze for ZString
impl RefUnwindSafe for ZString
impl Send for ZString
impl Sync for ZString
impl Unpin for ZString
impl UnsafeUnpin for ZString
impl UnwindSafe for ZString
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more