pub struct ZObject { /* private fields */ }Expand description
Dynamic object schema with runtime-defined fields.
For compile-time type-safe objects, use the schema! macro instead.
§Unknown field handling
strip()(default) — unknown fields are silently removed from the output.strict()— unknown fields cause a validation error.passthrough()— unknown fields are kept as-is in the output.
§Example
use vld::prelude::*;
let schema = vld::object()
.field("name", vld::string().min(1))
.field("age", vld::number().int().min(0));Implementations§
Source§impl ZObject
impl ZObject
pub fn new() -> ZObject
Sourcepub fn field<S>(self, name: impl Into<String>, schema: S) -> ZObject
pub fn field<S>(self, name: impl Into<String>, schema: S) -> ZObject
Add a field with its validation schema. A field with the same name is replaced.
With the openapi feature the field keeps its JSON Schema, its
required status (.optional() / .nullish() / .with_default(...)
/ .nullable() are not required) and its named nested schemas.
Sourcepub fn field_optional<S>(self, name: impl Into<String>, schema: S) -> ZObject
pub fn field_optional<S>(self, name: impl Into<String>, schema: S) -> ZObject
Add a field that is automatically optional (null/missing → null).
The field won’t cause a validation error if it is missing or null.
When the value is missing/null the inner schema still runs first, so
.with_default(...) and .catch(...) produce their value; otherwise
the output is null.
§Example
use vld::prelude::*;
let schema = vld::object()
.field("name", vld::string().min(1))
.field_optional("nickname", vld::string().min(1));
let result = schema.parse(r#"{"name": "Alice"}"#).unwrap();
assert_eq!(result.get("nickname").unwrap(), &serde_json::Value::Null);Sourcepub fn strip(self) -> ZObject
pub fn strip(self) -> ZObject
Silently remove unknown fields from the output (default behavior).
Sourcepub fn passthrough(self) -> ZObject
pub fn passthrough(self) -> ZObject
Keep unknown fields as-is in the output without validation.
Sourcepub fn omit(self, name: &str) -> ZObject
pub fn omit(self, name: &str) -> ZObject
Remove a field definition by name. Returns self for chaining.
Useful with extend() to override fields.
Sourcepub fn extend(self, other: ZObject) -> ZObject
pub fn extend(self, other: ZObject) -> ZObject
Merge another object schema into this one.
If both schemas define the same field, the one from other wins (and
takes its place at the end). other’s settings are merged too: its
unknown-field mode (when not the default strip()), catchall,
min_keys / max_keys win when set, and its when rules are added.
Sourcepub fn partial(self) -> ZObject
pub fn partial(self) -> ZObject
Make all fields optional: null/missing values return null in the output
instead of failing validation.
Equivalent to Zod’s .partial().
Sourcepub fn required(self) -> ZObject
pub fn required(self) -> ZObject
Make all fields required: null values will fail validation.
This is the opposite of partial().
Sourcepub fn catchall<S>(self, schema: S) -> ZObject
pub fn catchall<S>(self, schema: S) -> ZObject
Validate unknown fields using the given schema instead of stripping/rejecting them.
When set, unknown fields are parsed through the catchall schema regardless of the unknown field mode.
Sourcepub fn when<S>(
self,
condition_field: impl Into<String>,
condition_value: impl Into<Value>,
target_field: impl Into<String>,
schema: S,
) -> ZObject
pub fn when<S>( self, condition_field: impl Into<String>, condition_value: impl Into<Value>, target_field: impl Into<String>, schema: S, ) -> ZObject
Add a conditional validation rule.
When condition_field has the given value, target_field is validated
with the provided schema in addition to any existing field schemas.
Under strict() an undeclared target_field is not
reported as unrecognized.
§Example
use vld::prelude::*;
let schema = vld::object()
.field("role", vld::string())
.field_optional("admin_key", vld::string())
.when("role", "admin", "admin_key", vld::string().min(10));
// When role != "admin", admin_key is optional and any value is fine
let ok = schema.parse(r#"{"role": "user"}"#);
assert!(ok.is_ok());
// When role == "admin", admin_key must pass the extra schema
let err = schema.parse(r#"{"role": "admin", "admin_key": "short"}"#);
assert!(err.is_err());Sourcepub fn min_keys(self, n: usize) -> ZObject
pub fn min_keys(self, n: usize) -> ZObject
Require at least this many keys in the input object.
Sourcepub fn deep_partial(self) -> ZObject
👎Deprecated since 0.5.0: not recursive; identical to partial(). Call partial() on nested objects explicitly.
pub fn deep_partial(self) -> ZObject
not recursive; identical to partial(). Call partial() on nested objects explicitly.
Trait Implementations§
Source§impl VldSchema for ZObject
impl VldSchema for ZObject
Source§type Output = Map<String, Value>
type Output = Map<String, Value>
Source§fn parse_value(&self, value: &Value) -> Result<Map<String, Value>, VldError>
fn parse_value(&self, value: &Value) -> Result<Map<String, Value>, 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 !RefUnwindSafe for ZObject
impl !UnwindSafe for ZObject
impl Forget for ZObject
impl Freeze for ZObject
impl Send for ZObject
impl Sync for ZObject
impl Unpin for ZObject
impl UnsafeUnpin for ZObject
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> 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