ObjectSchema
Validates a table field by field. Fields absent from the shape are ignored unless the schema is made ObjectSchema:strict.
The original table is returned untouched unless a field fills in a value —
a DefaultSchema field, or a nested object that filled one in. In that case
the input is shallow copied and the filled values written to the copy, so
the value handed to parse is never modified.
local User = v.object({
name = v.string():min(3),
level = v.number():integer(),
})
The field schemas stay reachable through ObjectSchema.shape, so a single field can be validated on its own.
Properties
shape
This item is read only and cannot be modified. Read OnlyReflectionObjectSchema.shape: SThe schemas this object was built from, so a field can be reused or validated on its own:
local Profile = v.object({ Level = v.number():integer():min(1) })
Profile.shape.Level:parse(42) --> 42
Profile.shape.Level:parse(0) --> errors: expected at least 1, got 0
The table is a frozen copy of the one passed to Vet.object, so mutating it fails loudly rather than corrupting the schema.
isOptional
This item is read only and cannot be modified. Read OnlyReflectionObjectSchema.isOptional: boolean
Always false. Objects carry no minimum or maximum; read
ObjectSchema.shape to reach a field's own limits.
access
This item is read only and cannot be modified. Read OnlyReflectionObjectSchema.access: "readwrite"
Always "readwrite", and describes this schema as a field of some other
object rather than its own fields. Read shape[field].access for those.
Functions
safeParse
ParsingObjectSchema:safeParse(value: unknown) → Result<objectOutput<S>>Validates every field in the shape, reporting each failure with its field name in the issue path.
parse
ParsingObjectSchema:parse(value: unknown) → TValidates value and returns it, throwing on failure.
Errors
| Type | Description |
|---|---|
| "expected ..." | Thrown when `value` fails validation |
strict
RefinementReturns a schema that also rejects keys absent from the shape. Use it for data crossing a trust boundary, where an unexpected field is more likely a mistake than a spare.
optional
WrapperWraps this schema so that nil is also accepted.
default
Wrapper
Wraps this schema so that nil yields fallback instead of failing.
Prefer passing a function, since a constant table would be shared by
every parse.
defaultWith
WrapperObjectSchema:defaultWith(produce: () → T--
Builds the value used when parsing nil
) → DefaultSchema<objectOutput<S>>
Like :default, but calls produce on every parse instead of reusing
one value. Prefer it here, since a single table would otherwise be
shared by every caller and mutable through any of them.
The produced value is validated each time.
readonly
Wrapper
Wraps this schema so that a field using it is typed read. Only the
field is affected; the nested record keeps whatever access its own
fields declare.
writeonly
WrapperWraps this schema so that a field using it is typed write.