pub struct ZObject { /* private fields */ }utoipa-5 only.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 new() -> ZObject
Available on crate feature poem-openapi only.
pub fn new() -> ZObject
poem-openapi only.Create the schema (the same as its vld:: constructor function).
Sourcepub fn field<S>(self, name: impl Into<String>, schema: S) -> ZObject
Available on crate feature poem-openapi only.
pub fn field<S>(self, name: impl Into<String>, schema: S) -> ZObject
poem-openapi only.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(...)
are not required; .nullable() is) and its named nested schemas.
Sourcepub fn field_optional<S>(self, name: impl Into<String>, schema: S) -> ZObject
Available on crate feature poem-openapi only.
pub fn field_optional<S>(self, name: impl Into<String>, schema: S) -> ZObject
poem-openapi only.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 strict(self) -> ZObject
Available on crate feature poem-openapi only.
pub fn strict(self) -> ZObject
poem-openapi only.Reject unknown fields not defined in the schema.
Sourcepub fn strip(self) -> ZObject
Available on crate feature poem-openapi only.
pub fn strip(self) -> ZObject
poem-openapi only.Silently remove unknown fields from the output (default behavior).
Sourcepub fn passthrough(self) -> ZObject
Available on crate feature poem-openapi only.
pub fn passthrough(self) -> ZObject
poem-openapi only.Keep unknown fields as-is in the output without validation.
Sourcepub fn omit(self, name: &str) -> ZObject
Available on crate feature poem-openapi only.
pub fn omit(self, name: &str) -> ZObject
poem-openapi only.Remove a field definition by name. Returns self for chaining.
Useful with extend() to override fields.
Sourcepub fn pick(self, names: &[&str]) -> ZObject
Available on crate feature poem-openapi only.
pub fn pick(self, names: &[&str]) -> ZObject
poem-openapi only.Keep only the listed fields, removing all others.
Sourcepub fn extend(self, other: ZObject) -> ZObject
Available on crate feature poem-openapi only.
pub fn extend(self, other: ZObject) -> ZObject
poem-openapi only.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 merge(self, other: ZObject) -> ZObject
Available on crate feature poem-openapi only.
pub fn merge(self, other: ZObject) -> ZObject
poem-openapi only.Alias for extend.
Sourcepub fn partial(self) -> ZObject
Available on crate feature poem-openapi only.
pub fn partial(self) -> ZObject
poem-openapi only.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
Available on crate feature poem-openapi only.
pub fn required(self) -> ZObject
poem-openapi only.Make all fields required: null values will fail validation.
This is the opposite of partial().
Sourcepub fn catchall<S>(self, schema: S) -> ZObject
Available on crate feature poem-openapi only.
pub fn catchall<S>(self, schema: S) -> ZObject
poem-openapi only.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
Available on crate feature poem-openapi only.
pub fn when<S>( self, condition_field: impl Into<String>, condition_value: impl Into<Value>, target_field: impl Into<String>, schema: S, ) -> ZObject
poem-openapi only.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
Available on crate feature poem-openapi only.
pub fn min_keys(self, n: usize) -> ZObject
poem-openapi only.Require at least this many keys in the input object.
Source§impl ZObject
impl ZObject
Sourcepub fn to_json_schema(&self) -> Value
Available on crate features openapi and poem-openapi only.
pub fn to_json_schema(&self) -> Value
openapi and poem-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§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>(&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 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>(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 Move 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> 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> 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