Skip to main content

ZString

Struct ZString 

Source
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

Source

pub fn new() -> ZString

Source

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!"));
Source

pub fn with_messages<F>(self, f: F) -> ZString
where F: Fn(&str) -> Option<String>,

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,
    });
Source

pub fn min(self, len: usize) -> ZString

Minimum string length (inclusive).

Source

pub fn min_msg(self, len: usize, msg: impl Into<String>) -> ZString

Minimum string length with custom message.

Source

pub fn max(self, len: usize) -> ZString

Maximum string length (inclusive).

Source

pub fn max_msg(self, len: usize, msg: impl Into<String>) -> ZString

Maximum string length with custom message.

Source

pub fn len(self, len: usize) -> ZString

Exact string length.

Source

pub fn len_msg(self, len: usize, msg: impl Into<String>) -> ZString

Exact string length with custom message.

Source

pub fn email(self) -> ZString

Must be a valid email address.

Source

pub fn email_msg(self, msg: impl Into<String>) -> ZString

Must be a valid email address, with custom message.

Source

pub fn url(self) -> ZString

Must be a valid URL (http/https).

Source

pub fn url_msg(self, msg: impl Into<String>) -> ZString

Must be a valid URL, with custom message.

Source

pub fn uuid(self) -> ZString

Must be a valid UUID.

Source

pub fn uuid_msg(self, msg: impl Into<String>) -> ZString

Must be a valid UUID, with custom message.

Source

pub fn ip(self) -> ZString

Must be a valid IP address (IPv4 or IPv6).

Source

pub fn ip_msg(self, msg: impl Into<String>) -> ZString

Must be a valid IP address (IPv4 or IPv6), with custom message.

Source

pub fn starts_with(self, prefix: impl Into<String>) -> ZString

Must start with the given prefix.

Source

pub fn starts_with_msg( self, prefix: impl Into<String>, msg: impl Into<String>, ) -> ZString

Must start with the given prefix, with custom message.

Source

pub fn ends_with(self, suffix: impl Into<String>) -> ZString

Must end with the given suffix.

Source

pub fn ends_with_msg( self, suffix: impl Into<String>, msg: impl Into<String>, ) -> ZString

Must end with the given suffix, with custom message.

Source

pub fn contains(self, sub: impl Into<String>) -> ZString

Must contain the given substring.

Source

pub fn contains_msg( self, sub: impl Into<String>, msg: impl Into<String>, ) -> ZString

Must contain the given substring, with custom message.

Source

pub fn non_empty(self) -> ZString

Must not be empty.

Source

pub fn non_empty_msg(self, msg: impl Into<String>) -> ZString

Must not be empty, with custom message.

Source

pub fn trim(self) -> ZString

Trim whitespace before validation.

Source

pub fn to_lowercase(self) -> ZString

Convert to lowercase before validation.

Source

pub fn to_uppercase(self) -> ZString

Convert to uppercase before validation.

Source

pub fn coerce(self) -> ZString

Coerce non-string values (numbers, booleans) to string.

Source

pub fn ipv4(self) -> ZString

Must be a valid IPv4 address.

Source

pub fn ipv4_msg(self, msg: impl Into<String>) -> ZString

Must be a valid IPv4 address, with custom message.

Source

pub fn ipv6(self) -> ZString

Must be a valid IPv6 address.

Source

pub fn ipv6_msg(self, msg: impl Into<String>) -> ZString

Must be a valid IPv6 address, with custom message.

Source

pub fn cidr(self) -> ZString

Must be a valid CIDR block (IPv4/IPv6 with prefix).

Source

pub fn cidr_msg(self, msg: impl Into<String>) -> ZString

Must be a valid CIDR block, with custom message.

Source

pub fn mac(self) -> ZString

Must be a valid MAC address (xx:xx:xx:xx:xx:xx).

Source

pub fn mac_msg(self, msg: impl Into<String>) -> ZString

Must be a valid MAC address, with custom message.

Source

pub fn hex(self) -> ZString

Must contain only hexadecimal characters.

Source

pub fn hex_msg(self, msg: impl Into<String>) -> ZString

Must contain only hexadecimal characters, with custom message.

Source

pub fn credit_card(self) -> ZString

Must be a valid credit card number (Luhn check).

Source

pub fn credit_card_msg(self, msg: impl Into<String>) -> ZString

Must be a valid credit card number, with custom message.

Source

pub fn phone(self) -> ZString

Must be a valid international phone number (E.164-like, starts with +).

Source

pub fn phone_msg(self, msg: impl Into<String>) -> ZString

Must be a valid international phone number, with custom message.

Source

pub fn semver(self) -> ZString

Must be a valid semantic version (MAJOR.MINOR.PATCH, optional v prefix).

Source

pub fn semver_msg(self, msg: impl Into<String>) -> ZString

Must be a valid semantic version, with custom message.

Source

pub fn jwt(self) -> ZString

Must be a JWT-like token (header.payload.signature, base64url parts).

Source

pub fn jwt_msg(self, msg: impl Into<String>) -> ZString

Must be a JWT-like token, with custom message.

Source

pub fn ascii(self) -> ZString

Must contain only ASCII characters.

Source

pub fn ascii_msg(self, msg: impl Into<String>) -> ZString

Must contain only ASCII characters, with custom message.

Source

pub fn alpha(self) -> ZString

Must contain only alphabetic ASCII characters.

Source

pub fn alpha_msg(self, msg: impl Into<String>) -> ZString

Must contain only alphabetic ASCII characters, with custom message.

Source

pub fn alphanumeric(self) -> ZString

Must contain only ASCII alphanumeric characters.

Source

pub fn alphanumeric_msg(self, msg: impl Into<String>) -> ZString

Must contain only ASCII alphanumeric characters, with custom message.

Source

pub fn lowercase(self) -> ZString

Must already be lowercase.

Source

pub fn lowercase_msg(self, msg: impl Into<String>) -> ZString

Must already be lowercase, with custom message.

Source

pub fn uppercase(self) -> ZString

Must already be uppercase.

Source

pub fn uppercase_msg(self, msg: impl Into<String>) -> ZString

Must already be uppercase, with custom message.

Source

pub fn base64(self) -> ZString

Must be a valid Base64 string.

Source

pub fn base64_msg(self, msg: impl Into<String>) -> ZString

Must be a valid Base64 string, with custom message.

Source

pub fn iso_date(self) -> ZString

Must be a valid ISO 8601 date (YYYY-MM-DD).

Source

pub fn iso_date_msg(self, msg: impl Into<String>) -> ZString

Must be a valid ISO 8601 date, with custom message.

Source

pub fn iso_datetime(self) -> ZString

Must be a valid ISO 8601 datetime.

Source

pub fn iso_datetime_msg(self, msg: impl Into<String>) -> ZString

Must be a valid ISO 8601 datetime, with custom message.

Source

pub fn iso_time(self) -> ZString

Must be a valid ISO 8601 time (HH:MM or HH:MM:SS).

Source

pub fn iso_time_msg(self, msg: impl Into<String>) -> ZString

Must be a valid ISO 8601 time, with custom message.

Source

pub fn hostname(self) -> ZString

Must be a valid hostname.

Source

pub fn hostname_msg(self, msg: impl Into<String>) -> ZString

Must be a valid hostname, with custom message.

Source

pub fn cuid2(self) -> ZString

Must be a valid CUID2 string (lowercase alphanumeric, starts with a letter).

Source

pub fn cuid2_msg(self, msg: impl Into<String>) -> ZString

Must be a valid CUID2 string, with custom message.

Source

pub fn ulid(self) -> ZString

Must be a valid ULID (26-char Crockford Base32).

Source

pub fn ulid_msg(self, msg: impl Into<String>) -> ZString

Must be a valid ULID, with custom message.

Source

pub fn nanoid(self) -> ZString

Must be a valid Nano ID (alphanumeric + _ + -).

Source

pub fn nanoid_msg(self, msg: impl Into<String>) -> ZString

Must be a valid Nano ID, with custom message.

Source

pub fn emoji(self) -> ZString

Must contain at least one emoji character.

Source

pub fn emoji_msg(self, msg: impl Into<String>) -> ZString

Must contain an emoji, with custom message.

Source

pub fn slug(self) -> ZString

Must be a URL-friendly slug ([a-z0-9-]).

Source

pub fn slug_msg(self, msg: impl Into<String>) -> ZString

Source

pub fn color(self) -> ZString

Must be a hex color (#RRGGBB or #RRGGBBAA).

Source

pub fn color_msg(self, msg: impl Into<String>) -> ZString

Source

pub fn currency_code(self) -> ZString

Must be a currency code (ISO-4217-like uppercase 3 letters).

Source

pub fn currency_code_msg(self, msg: impl Into<String>) -> ZString

Source

pub fn country_code(self) -> ZString

Must be a country code (ISO-3166 alpha-2).

Source

pub fn country_code_msg(self, msg: impl Into<String>) -> ZString

Source

pub fn locale(self) -> ZString

Must be a locale (ll or ll-RR, example: en, en-US).

Source

pub fn locale_msg(self, msg: impl Into<String>) -> ZString

Source

pub fn cron(self) -> ZString

Must be a cron expression (5 or 6 fields).

Source

pub fn cron_msg(self, msg: impl Into<String>) -> ZString

Trait Implementations§

Source§

impl Clone for ZString

Source§

fn clone(&self) -> ZString

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for ZString

Source§

fn default() -> ZString

Returns the “default value” for a type. Read more
Source§

impl VldSchema for ZString

Source§

type Output = String

The Rust type produced by this schema after successful parsing.
Source§

fn parse_value(&self, value: &Value) -> Result<String, VldError>

Parse and validate a serde_json::Value.
Source§

fn parse<I>(&self, input: &I) -> Result<Self::Output, VldError>
where I: VldInput + ?Sized,

Parse from any supported input (JSON string, file path, serde_json::Value, etc.)
Source§

fn validate<T>(&self, value: &T) -> Result<Self::Output, VldError>
where T: Serialize,

Validate an existing Rust value against this schema. Read more
Source§

fn is_valid<T>(&self, value: &T) -> bool
where T: Serialize,

Check if an existing Rust value passes this schema’s validation. Read more
Source§

fn optional(self) -> ZOptional<Self>

Make this field optional. Missing or null values become None.
Source§

fn nullable(self) -> ZNullable<Self>

Allow null values. Null becomes None.
Source§

fn with_default(self, value: Self::Output) -> ZDefault<Self>
where Self::Output: Clone,

Provide a default value when the field is missing or null.
Source§

fn refine<F>(self, check: F, message: &str) -> ZRefine<Self, F>
where F: Fn(&Self::Output) -> bool,

Add a custom refinement check without changing the output type.
Source§

fn transform<F, U>(self, f: F) -> ZTransform<Self, F, U>
where F: Fn(Self::Output) -> U,

Transform the output value after successful parsing.
Source§

fn nullish(self) -> ZNullish<Self>

Make this field nullish (both optional and nullable).
Source§

fn catch(self, fallback: Self::Output) -> ZCatch<Self>
where Self::Output: Clone,

Return a fallback value on ANY validation error.
Source§

fn pipe<S>(self, next: S) -> ZPipe<Self, S>
where S: VldSchema, Self::Output: Serialize,

Chain this schema’s output into another schema. Read more
Source§

fn describe(self, description: &str) -> ZDescribe<Self>

Attach a human-readable description/label to this schema. Read more
Source§

fn super_refine<F>(self, check: F) -> ZSuperRefine<Self, F>
where F: Fn(&Self::Output, &mut VldError),

Add a custom refinement that can produce multiple errors. Read more
Source§

fn or<B>(self, other: B) -> ZUnion2<Self, B>
where B: VldSchema,

Create a union: this schema or another. Returns Either<Self::Output, B::Output>.
Source§

fn and<B>(self, other: B) -> ZIntersection<Self, B>
where B: VldSchema,

Create an intersection: input must satisfy both schemas.
Source§

fn message(self, msg: impl Into<String>) -> ZMessage<Self>

Override the error message for this schema. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynSchema for T
where T: VldSchema, <T as VldSchema>::Output: Serialize,

Source§

fn dyn_parse(&self, value: &Value) -> Result<Value, VldError>

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more