pub struct ZObject { /* private fields */ }Implementations§
Source§impl ZObject
impl ZObject
pub fn new() -> ZObject
Sourcepub fn field<S>(self, name: impl Into<String>, schema: S) -> ZObjectwhere
S: DynSchema + 'static,
pub fn field<S>(self, name: impl Into<String>, schema: S) -> ZObjectwhere
S: DynSchema + 'static,
Add a field with its validation schema.
Sourcepub fn field_optional<S>(self, name: impl Into<String>, schema: S) -> ZObjectwhere
S: DynSchema + 'static,
pub fn field_optional<S>(self, name: impl Into<String>, schema: S) -> ZObjectwhere
S: DynSchema + 'static,
Add a field that is automatically optional (null/missing → null).
Shorthand for .field(name, OptionalDynSchema(schema)) — the field won’t
cause a validation error if it is missing or 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’s fields into this one.
If both schemas define the same field, the one from other wins.
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) -> ZObjectwhere
S: DynSchema + 'static,
pub fn catchall<S>(self, schema: S) -> ZObjectwhere
S: DynSchema + 'static,
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,
) -> ZObjectwhere
S: DynSchema + 'static,
pub fn when<S>(
self,
condition_field: impl Into<String>,
condition_value: impl Into<Value>,
target_field: impl Into<String>,
schema: S,
) -> ZObjectwhere
S: DynSchema + 'static,
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.
§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
pub fn deep_partial(self) -> ZObject
Make all fields optional recursively.
Currently equivalent to partial() — nested objects
must apply partial() separately.
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 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>.