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§
Required Methods§
Provided Methods§
Sourcefn parse<I>(&self, input: &I) -> Result<Self::Output, VldError>
fn parse<I>(&self, input: &I) -> Result<Self::Output, VldError>
Parse from any supported input (JSON string, file path, serde_json::Value, etc.)
Sourcefn 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,
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());Sourcefn optional(self) -> ZOptional<Self>
fn optional(self) -> ZOptional<Self>
Make this field optional. Missing or null values become None.
Sourcefn with_default(self, value: Self::Output) -> ZDefault<Self>
fn with_default(self, value: Self::Output) -> ZDefault<Self>
Provide a default value when the field is missing or null.
Sourcefn refine<F>(self, check: F, message: &str) -> ZRefine<Self, F>
fn refine<F>(self, check: F, message: &str) -> ZRefine<Self, F>
Add a custom refinement check without changing the output type.
Sourcefn transform<F, U>(self, f: F) -> ZTransform<Self, F, U>
fn transform<F, U>(self, f: F) -> ZTransform<Self, F, U>
Transform the output value after successful parsing.
Sourcefn catch(self, fallback: Self::Output) -> ZCatch<Self>
fn catch(self, fallback: Self::Output) -> ZCatch<Self>
Return a fallback value on ANY validation error.
Sourcefn pipe<S>(self, next: S) -> ZPipe<Self, S>
fn pipe<S>(self, next: S) -> ZPipe<Self, S>
Chain this schema’s output into another schema.
The output of self is serialized to JSON, then parsed by next.
Sourcefn describe(self, description: &str) -> ZDescribe<Self>
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.
Sourcefn super_refine<F>(self, check: F) -> ZSuperRefine<Self, F>
fn super_refine<F>(self, check: F) -> ZSuperRefine<Self, F>
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.
Sourcefn or<B>(self, other: B) -> ZUnion2<Self, B>where
B: VldSchema,
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>.
Sourcefn and<B>(self, other: B) -> ZIntersection<Self, B>where
B: VldSchema,
fn and<B>(self, other: B) -> ZIntersection<Self, B>where
B: VldSchema,
Create an intersection: input must satisfy both schemas.
Sourcefn message(self, msg: impl Into<String>) -> ZMessage<Self>
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.