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
Sourcepub fn field<S: DynSchema + Send + Sync + 'static>(
self,
name: impl Into<String>,
schema: S,
) -> Self
pub fn field<S: DynSchema + Send + Sync + 'static>( self, name: impl Into<String>, schema: S, ) -> Self
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: DynSchema + Send + Sync + 'static>(
self,
name: impl Into<String>,
schema: S,
) -> Self
pub fn field_optional<S: DynSchema + Send + Sync + 'static>( self, name: impl Into<String>, schema: S, ) -> Self
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 passthrough(self) -> Self
pub fn passthrough(self) -> Self
Keep unknown fields as-is in the output without validation.
Sourcepub fn omit(self, name: &str) -> Self
pub fn omit(self, name: &str) -> Self
Remove a field definition by name. Returns self for chaining.
Useful with extend() to override fields.
Sourcepub fn extend(self, other: ZObject) -> Self
pub fn extend(self, other: ZObject) -> Self
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) -> Self
pub fn partial(self) -> Self
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) -> Self
pub fn required(self) -> Self
Make all fields required: null values will fail validation.
This is the opposite of partial().
Sourcepub fn catchall<S: DynSchema + Send + Sync + 'static>(self, schema: S) -> Self
pub fn catchall<S: DynSchema + Send + Sync + 'static>(self, schema: S) -> Self
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: DynSchema + Send + Sync + 'static>(
self,
condition_field: impl Into<String>,
condition_value: impl Into<Value>,
target_field: impl Into<String>,
schema: S,
) -> Self
pub fn when<S: DynSchema + Send + Sync + 'static>( self, condition_field: impl Into<String>, condition_value: impl Into<Value>, target_field: impl Into<String>, schema: S, ) -> Self
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());Source§impl ZObject
impl ZObject
Sourcepub fn to_json_schema(&self) -> Value
Available on crate feature openapi only.
pub fn to_json_schema(&self) -> Value
openapi only.JSON Schema of this object; same as
JsonSchema::json_schema.
Trait Implementations§
Source§impl CollectNestedSchemas for ZObject
Available on crate feature openapi only.
impl CollectNestedSchemas for ZObject
openapi only.Source§fn collect_nested_schemas(&self, out: &mut Vec<NestedSchemaEntry>)
fn collect_nested_schemas(&self, out: &mut Vec<NestedSchemaEntry>)
_out (for components.schemas).Source§impl JsonSchema for ZObject
Available on crate feature openapi only.Properties from the fields (with their own schemas and required status);
additionalProperties is false for strict(), the catchall schema when
set, true otherwise.
impl JsonSchema for ZObject
openapi only.Properties from the fields (with their own schemas and required status);
additionalProperties is false for strict(), the catchall schema when
set, true otherwise.
Source§fn json_schema(&self) -> Value
fn json_schema(&self) -> Value
Source§fn is_required(&self) -> bool
fn is_required(&self) -> bool
required array. Read moreSource§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_missing(&self) -> Result<Self::Output, VldError>
fn parse_missing(&self) -> Result<Self::Output, VldError>
Source§fn parse<I: VldInput + ?Sized>(
&self,
input: &I,
) -> Result<Self::Output, VldError>
fn parse<I: VldInput + ?Sized>( &self, input: &I, ) -> Result<Self::Output, VldError>
serde_json::Value, etc.)Source§fn validate<T: Serialize>(&self, value: &T) -> Result<Self::Output, VldError>
fn validate<T: Serialize>(&self, value: &T) -> Result<Self::Output, VldError>
Source§fn is_valid<T: Serialize>(&self, value: &T) -> bool
fn is_valid<T: Serialize>(&self, value: &T) -> bool
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 blank_as(self, value: Self::Output) -> ZDefault<Self>
fn blank_as(self, value: Self::Output) -> ZDefault<Self>
with_default, and a blank string (empty or
whitespace only) also becomes value: legacy stores that write ""
or null for “unset”. Read moreSource§fn refine<F>(self, check: F, message: &str) -> ZRefine<Self, F>
fn refine<F>(self, check: F, message: &str) -> ZRefine<Self, F>
Source§fn rule_msg<R>(self, rule: R, msg: impl Into<String>) -> ZRule<Self, R>
fn rule_msg<R>(self, rule: R, msg: impl Into<String>) -> ZRule<Self, R>
rule with a custom error message.Source§fn try_transform<F, U, E>(self, f: F) -> ZTryTransform<Self, F, U>
fn try_transform<F, U, E>(self, f: F) -> ZTryTransform<Self, F, U>
Err becomes a
validation issue (Custom { code: "transform" }, message = the error). Read moreSource§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: VldSchema>(self, next: S) -> ZPipe<Self, S>
fn pipe<S: VldSchema>(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: VldSchema>(self, other: B) -> ZUnion2<Self, B>
fn or<B: VldSchema>(self, other: B) -> ZUnion2<Self, B>
Either<Self::Output, B::Output>.Source§fn and<B: VldSchema>(self, other: B) -> ZIntersection<Self, B>
fn and<B: VldSchema>(self, other: B) -> ZIntersection<Self, B>
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> DynSchema for T
impl<T> DynSchema for T
Source§fn dyn_parse(&self, value: &Value) -> Result<Value, VldError>
fn dyn_parse(&self, value: &Value) -> Result<Value, VldError>
value and return the output serialized back to JSON.Source§fn dyn_parse_missing(&self) -> Result<Value, VldError>
fn dyn_parse_missing(&self) -> Result<Value, VldError>
VldSchema::parse_missing of the erased schema.Source§fn dyn_json_schema(&self) -> Value
fn dyn_json_schema(&self) -> Value
openapi only.Source§fn dyn_is_required(&self) -> bool
fn dyn_is_required(&self) -> bool
openapi only.required array.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 moreimpl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().