DefaultSchema
Substitutes a fallback when the value is nil. Unlike OptionalSchema the
result is never nil, so downstream code does not have to handle a missing
value.
local Volume = v.number():min(0):max(1):default(0.5)
Volume:parse(nil) --> 0.5
Volume:parse(0.2) --> 0.2
Volume:parse(2) --> errors
Pass a function to build the fallback per parse, which matters for tables since a constant would be shared by every caller:
local Tags = v.array(v.string()):default(function()
return {}
end)
Properties
isOptional
This item is read only and cannot be modified. Read OnlyReflectionDefaultSchema.isOptional: boolean
Always false. A defaulted schema accepts a missing value but never
produces nil, so callers do not have to handle one.
access
This item is read only and cannot be modified. Read OnlyReflectionDefaultSchema.access: "readwrite"Always "readwrite". Chain :readonly() or :writeonly() to change it.
minimum
This item is read only and cannot be modified. Read OnlyReflectionDefaultSchema.minimum: number?Forwarded from the wrapped schema.
maximum
This item is read only and cannot be modified. Read OnlyReflectionDefaultSchema.maximum: number?Forwarded from the wrapped schema.
Functions
safeParse
ParsingDefaultSchema:safeParse(value: unknown) → Result<T>
Returns the fallback when value is nil, and otherwise defers to the
wrapped schema. A fallback built by a function is validated too.
parse
ParsingDefaultSchema:parse(value: unknown) → TValidates value and returns it, throwing on failure.
Errors
| Type | Description |
|---|---|
| "expected ..." | Thrown when a non-nil `value` fails validation |
readonly
Wrapper
Wraps this schema so that a field using it is typed read. The fallback
is still filled in, so the field is read-only but never missing.
writeonly
WrapperWraps this schema so that a field using it is typed write.