OptionalSchema
Accepts nil in addition to whatever the wrapped schema accepts. Created by
calling :optional() on any schema, which comes last in a chain so that the
refinements before it still apply to non-nil values.
local Nickname = v.string():max(20):optional()
Nickname:parse(nil) --> nil
Nickname:parse("kyle") --> "kyle"
Nickname:parse(5) --> errors
Properties
isOptional
This item is read only and cannot be modified. Read OnlyReflectionOptionalSchema.isOptional: booleanAlways true.
access
This item is read only and cannot be modified. Read OnlyReflectionOptionalSchema.access: "readwrite"Always "readwrite". Chain :readonly() or :writeonly() to change it.
minimum
This item is read only and cannot be modified. Read OnlyReflectionOptionalSchema.minimum: number?
Forwarded from the wrapped schema, so v.string():min(3):optional()
still reports a minimum of 3.
maximum
This item is read only and cannot be modified. Read OnlyReflectionOptionalSchema.maximum: number?Forwarded from the wrapped schema.
Functions
safeParse
ParsingOptionalSchema:safeParse(value: unknown) → Result<T?>
Succeeds with nil when value is nil, and otherwise defers to the
wrapped schema.
parse
ParsingOptionalSchema:parse(value: unknown) → T?Validates value and returns it, throwing on failure.
Errors
| Type | Description |
|---|---|
| "expected ..." | Thrown when a non-nil `value` fails validation |
optional
WrapperReturns the same schema, since it already accepts nil.
readonly
Wrapper
Wraps this schema so that a field using it is typed read. The value
type keeps the ?, so the field reads as read name: string?.
writeonly
WrapperWraps this schema so that a field using it is typed write.