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 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 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 Freeze for ZObject
impl !RefUnwindSafe for ZObject
impl !Send for ZObject
impl !Sync for ZObject
impl Unpin for ZObject
impl UnsafeUnpin for ZObject
impl !UnwindSafe 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