ReadonlySchema
Marks a field as read-only, so the record an ObjectSchema parses to
declares it read. Created by calling :readonly() on any schema, or by
wrapping one with Vet.readonly. It comes last in a chain, after the
refinements and after :optional() or :default().
local PlayerData = v.object({
id = v.number():integer():readonly(),
name = v.string(),
})
type PlayerData = v.infer<typeof(PlayerData)>
--> { read id: number, name: string }
local data = PlayerData:parse(payload)
print(data.id) --> fine
data.id = 2 --> analysis error: `id` is read-only
Validation is untouched: the field is parsed exactly as the wrapped schema would parse it, and the value is there at runtime. Only the type differs, so the restriction is caught during analysis rather than enforced by a metatable on every write.
Properties
access
This item is read only and cannot be modified. Read OnlyReflectionReadonlySchema.access: "read"Always "read".
isOptional
This item is read only and cannot be modified. Read OnlyReflectionReadonlySchema.isOptional: boolean
Forwarded from the wrapped schema, so v.string():optional():readonly()
still reports itself as optional.
minimum
This item is read only and cannot be modified. Read OnlyReflectionReadonlySchema.minimum: number?Forwarded from the wrapped schema.
maximum
This item is read only and cannot be modified. Read OnlyReflectionReadonlySchema.maximum: number?Forwarded from the wrapped schema.
Functions
safeParse
ParsingReadonlySchema:safeParse(value: unknown) → Result<T>Defers to the wrapped schema. The marker changes the type a surrounding object produces, never what is accepted.
parse
ParsingReadonlySchema:parse(value: unknown) → TValidates value and returns it, throwing on failure.
Errors
| Type | Description |
|---|---|
| "expected ..." | Thrown when `value` fails the wrapped schema |
readonly
WrapperReturns the same schema, since it is already read-only.
writeonly
WrapperReturns a write-only wrapper around the same schema, replacing the read-only marker.