Skip to main content

VldSchema

Trait VldSchema 

Source
pub trait VldSchema: Sized {
    type Output;

Show 17 methods // Required method fn parse_value(&self, value: &Value) -> Result<Self::Output, VldError>; // Provided methods fn parse<I>(&self, input: &I) -> Result<Self::Output, VldError> where I: VldInput + ?Sized { ... } fn validate<T>(&self, value: &T) -> Result<Self::Output, VldError> where T: Serialize { ... } fn is_valid<T>(&self, value: &T) -> bool where T: Serialize { ... } fn optional(self) -> ZOptional<Self> { ... } fn nullable(self) -> ZNullable<Self> { ... } fn with_default(self, value: Self::Output) -> ZDefault<Self> where Self::Output: Clone { ... } fn refine<F>(self, check: F, message: &str) -> ZRefine<Self, F> where F: Fn(&Self::Output) -> bool { ... } fn transform<F, U>(self, f: F) -> ZTransform<Self, F, U> where F: Fn(Self::Output) -> U { ... } fn nullish(self) -> ZNullish<Self> { ... } fn catch(self, fallback: Self::Output) -> ZCatch<Self> where Self::Output: Clone { ... } fn pipe<S>(self, next: S) -> ZPipe<Self, S> where S: VldSchema, Self::Output: Serialize { ... } fn describe(self, description: &str) -> ZDescribe<Self> { ... } fn super_refine<F>(self, check: F) -> ZSuperRefine<Self, F> where F: Fn(&Self::Output, &mut VldError) { ... } fn or<B>(self, other: B) -> ZUnion2<Self, B> where B: VldSchema { ... } fn and<B>(self, other: B) -> ZIntersection<Self, B> where B: VldSchema { ... } fn message(self, msg: impl Into<String>) -> ZMessage<Self> { ... }
}
Expand description

Core validation schema trait.

Every validator in vld implements this trait. The associated type Output defines what Rust type will be produced after successful parsing.

§Example

use vld::prelude::*;

let schema = vld::string().min(3);
let result = schema.parse(r#""hello""#);
assert!(result.is_ok());

Required Associated Types§

Source

type Output

The Rust type produced by this schema after successful parsing.

Required Methods§

Source

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

Parse and validate a serde_json::Value.

Provided Methods§

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.

The value is serialized to JSON via serde, then validated. Returns the parsed output on success.

Requires the serialize feature.

§Example
use vld::prelude::*;

let schema = vld::array(vld::number().int().positive()).min_len(1);
let data = vec![1, 2, 3];
assert!(schema.validate(&data).is_ok());
Source

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

Check if an existing Rust value passes this schema’s validation.

Requires the serialize feature.

§Example
use vld::prelude::*;

let schema = vld::string().email();
assert!(schema.is_valid(&"user@example.com"));
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.

The output of self is serialized to JSON, then parsed by next.

Source

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

Attach a human-readable description/label to this schema.

The description is stored as metadata and does not affect validation.

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.

Unlike refine() which returns a single bool, super_refine receives a mutable VldError collector and can push multiple issues.

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.

On validation failure all issues produced by the inner schema will have their message replaced with the provided string.

Similar to Zod’s .message("...").

§Example
use vld::prelude::*;

let schema = vld::string().min(3).message("Too short");
let err = schema.parse(r#""ab""#).unwrap_err();
assert_eq!(err.issues[0].message, "Too short");

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<A> VldSchema for (A,)
where A: VldSchema,

Source§

type Output = (<A as VldSchema>::Output,)

Source§

fn parse_value( &self, value: &Value, ) -> Result<<(A,) as VldSchema>::Output, VldError>

Source§

impl<A, B> VldSchema for (A, B)
where A: VldSchema, B: VldSchema,

Source§

type Output = (<A as VldSchema>::Output, <B as VldSchema>::Output)

Source§

fn parse_value( &self, value: &Value, ) -> Result<<(A, B) as VldSchema>::Output, VldError>

Source§

impl<A, B, C> VldSchema for (A, B, C)
where A: VldSchema, B: VldSchema, C: VldSchema,

Source§

impl<A, B, C, D> VldSchema for (A, B, C, D)
where A: VldSchema, B: VldSchema, C: VldSchema, D: VldSchema,

Source§

impl<A, B, C, D, E> VldSchema for (A, B, C, D, E)
where A: VldSchema, B: VldSchema, C: VldSchema, D: VldSchema, E: VldSchema,

Source§

impl<A, B, C, D, E, F> VldSchema for (A, B, C, D, E, F)

Implementors§

Source§

impl VldSchema for ZAny

Source§

impl VldSchema for ZBoolean

Source§

impl VldSchema for ZBytes

Source§

impl VldSchema for ZDiscriminatedUnion

Source§

impl VldSchema for ZDuration

Source§

impl VldSchema for ZEnum

Source§

impl VldSchema for ZInt

Source§

impl VldSchema for ZJsonValue

Source§

impl VldSchema for ZNumber

Source§

impl VldSchema for ZObject

Source§

impl VldSchema for ZPath

Source§

impl VldSchema for ZSocketAddr

Source§

impl VldSchema for ZString

Source§

impl<A, B> VldSchema for ZIntersection<A, B>
where A: VldSchema, B: VldSchema,

Source§

impl<A, B> VldSchema for ZPipe<A, B>
where A: VldSchema, B: VldSchema, <A as VldSchema>::Output: Serialize,

Source§

impl<A, B> VldSchema for ZUnion2<A, B>
where A: VldSchema, B: VldSchema,

Source§

impl<A, B, C> VldSchema for ZUnion3<A, B, C>
where A: VldSchema, B: VldSchema, C: VldSchema,

Source§

impl<F, S> VldSchema for ZPreprocess<F, S>
where F: Fn(&Value) -> Value, S: VldSchema,

Source§

impl<F, T> VldSchema for ZCustom<F, T>
where F: Fn(&Value) -> Result<T, String>,

Source§

impl<K, V> VldSchema for ZMap<K, V>
where K: VldSchema, V: VldSchema, <K as VldSchema>::Output: Eq + Hash,

Source§

impl<T> VldSchema for ZArray<T>
where T: VldSchema,

Source§

impl<T> VldSchema for ZCatch<T>
where T: VldSchema, <T as VldSchema>::Output: Clone,

Source§

impl<T> VldSchema for ZDefault<T>
where T: VldSchema, <T as VldSchema>::Output: Clone,

Source§

impl<T> VldSchema for ZDescribe<T>
where T: VldSchema,

Source§

impl<T> VldSchema for ZLiteral<T>
where T: IntoLiteral,

Source§

impl<T> VldSchema for ZMessage<T>
where T: VldSchema,

Source§

impl<T> VldSchema for ZNullable<T>
where T: VldSchema,

Source§

impl<T> VldSchema for ZNullish<T>
where T: VldSchema,

Source§

impl<T> VldSchema for ZOptional<T>
where T: VldSchema,

Source§

impl<T> VldSchema for ZSet<T>
where T: VldSchema, <T as VldSchema>::Output: Eq + Hash,

Source§

impl<T, F> VldSchema for NestedSchema<T, F>
where F: Fn(&Value) -> Result<T, VldError>,

Source§

impl<T, F> VldSchema for ZLazy<T, F>
where F: Fn() -> T, T: VldSchema,

Source§

impl<T, F> VldSchema for ZRefine<T, F>
where T: VldSchema, F: Fn(&<T as VldSchema>::Output) -> bool,

Source§

impl<T, F> VldSchema for ZSuperRefine<T, F>
where T: VldSchema, F: Fn(&<T as VldSchema>::Output, &mut VldError),

Source§

impl<T, F, U> VldSchema for ZTransform<T, F, U>
where T: VldSchema, F: Fn(<T as VldSchema>::Output) -> U,

Source§

impl<V> VldSchema for ZRecord<V>
where V: VldSchema,