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 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 a custom error 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 (http/https).

With a custom error 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 a custom error 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 a custom error 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

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

With a custom error message.

Source

pub fn color(self) -> ZString

Must be a color (#RRGGBB, #RRGGBBAA, rgb(...), hsl(...)).

Source

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

Must be a color (#RRGGBB, #RRGGBBAA, rgb(...), hsl(...)).

With a custom error message.

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

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

With a custom error message.

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

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

With a custom error message.

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

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

With a custom error message.

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

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

With a custom error message.

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 a custom error message.

Source

pub fn ipv6(self) -> ZString

Must be a valid IPv6 address (IPv4-mapped forms included).

Source

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

Must be a valid IPv6 address (IPv4-mapped forms included).

With a custom error message.

Source

pub fn cidr(self) -> ZString

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

Source

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

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

With a custom error 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 (xx:xx:xx:xx:xx:xx).

With a custom error 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 a custom error message.

Source

pub fn credit_card(self) -> ZString

Must be a valid credit card number: digits with optional spaces/dashes, 12-19 digits, Luhn check.

Source

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

Must be a valid credit card number: digits with optional spaces/dashes, 12-19 digits, Luhn check.

With a custom error message.

Source

pub fn phone(self) -> ZString

Must be an international phone number: leading +, 8-15 digits, optional spaces, dashes and parentheses.

Source

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

Must be an international phone number: leading +, 8-15 digits, optional spaces, dashes and parentheses.

With a custom error message.

Source

pub fn semver(self) -> ZString

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

Source

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

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

With a custom error 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 (header.payload.signature, base64url parts).

With a custom error 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 a custom error 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 a custom error 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 a custom error 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 a custom error 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 a custom error message.

Source

pub fn base64(self) -> ZString

Must be a valid (padded) Base64 string.

Source

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

Must be a valid (padded) Base64 string.

With a custom error message.

Source

pub fn base64url(self) -> ZString

Must be a valid Base64URL string (-/_ alphabet, padding optional).

Source

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

Must be a valid Base64URL string (-/_ alphabet, padding optional).

With a custom error message.

Source

pub fn json(self) -> ZString

Must be a string containing one valid JSON document.

Source

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

Must be a string containing one valid JSON document.

With a custom error message.

Source

pub fn iso_date(self) -> ZString

Must be a valid ISO 8601 calendar date (YYYY-MM-DD, real day of month).

Source

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

Must be a valid ISO 8601 calendar date (YYYY-MM-DD, real day of month).

With a custom error message.

Source

pub fn iso_datetime(self) -> ZString

Must be a valid ISO 8601 datetime: YYYY-MM-DDTHH:MM:SS[.frac] (seconds required, T/t) with optional Z/z / ±HH:MM — the same grammar as vld::chrono::datetime() & co.

Source

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

Must be a valid ISO 8601 datetime: YYYY-MM-DDTHH:MM:SS[.frac] (seconds required, T/t) with optional Z/z / ±HH:MM — the same grammar as vld::chrono::datetime() & co.

With a custom error message.

Source

pub fn iso_time(self) -> ZString

Must be a valid ISO 8601 time (HH:MM[:SS[.frac]]).

Source

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

Must be a valid ISO 8601 time (HH:MM[:SS[.frac]]).

With a custom error 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 a custom error message.

Source

pub fn cuid2(self) -> ZString

Must be a valid CUID2 (2-32 lowercase alphanumerics, starting with a letter).

Source

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

Must be a valid CUID2 (2-32 lowercase alphanumerics, starting with a letter).

With a custom error 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 (26-char Crockford Base32).

With a custom error 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 (alphanumeric + _ + -).

With a custom error message.

Source

pub fn emoji(self) -> ZString

Must be non-empty and consist only of emoji.

Source

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

Must be non-empty and consist only of emoji.

With a custom error message.

Source§

impl ZString

Source

pub fn new() -> ZString

Create the schema (the same as its vld:: constructor function).

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> + Send + Sync + 'static,

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.

Keys: "too_small", "too_big", "invalid_length", "non_empty", "invalid_regex", "invalid_starts_with", "invalid_ends_with", "invalid_contains", and "invalid_<format>" for every format check ("invalid_email", "invalid_url", "invalid_uuid", "invalid_ip", "invalid_currency", "invalid_phone_e164", …).

The closure is consulted when an issue is built, so it covers checks added before and after this call; a per-check message (*_msg(...)) takes precedence over it. A later call replaces it.

§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 in characters (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 in characters (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 in characters.

Source

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

Exact string length with custom message.

Source

pub fn rule(self, rule: impl Rule<str>) -> ZString

Check with a reusable Rule on the string (keeps the ZString builder; see vld::rules).

Source

pub fn rule_msg(self, rule: impl Rule<str>, msg: impl Into<String>) -> ZString

rule with a 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.

Trait Implementations§

Source§

impl Clone for ZString

Source§

fn clone(&self) -> ZString

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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_missing(&self) -> Result<Self::Output, VldError>

Parse an object field whose key is absent. Read more
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 rule<R>(self, rule: R) -> ZRule<Self, R>
where Self: Sized, R: Rule<Self::Output>,

Check the output with a reusable Rule (output type unchanged). Read more
Source§

fn rule_msg<R>(self, rule: R, msg: impl Into<String>) -> ZRule<Self, R>
where Self: Sized, R: Rule<Self::Output>,

rule with a custom error message.
Source§

fn try_transform<F, U, E>(self, f: F) -> ZTryTransform<Self, F, U>
where F: Fn(Self::Output) -> Result<U, E>, E: Display,

Transform the output with a fallible function; an Err becomes a validation issue (Custom { code: "transform" }, message = the error). Read more
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>

Parse value and return the output serialized back to JSON.
Source§

fn dyn_parse_missing(&self) -> Result<Value, VldError>

VldSchema::parse_missing of the erased schema.
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> Schema for T
where T: VldSchema,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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