Skip to main content

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 OnlyReflection
DefaultSchema.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 OnlyReflection
DefaultSchema.access: "readwrite"

Always "readwrite". Chain :readonly() or :writeonly() to change it.

minimum​

This item is read only and cannot be modified. Read OnlyReflection
DefaultSchema.minimum: number?

Forwarded from the wrapped schema.

maximum​

This item is read only and cannot be modified. Read OnlyReflection
DefaultSchema.maximum: number?

Forwarded from the wrapped schema.

Functions​

safeParse​

Parsing
DefaultSchema: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​

Parsing
DefaultSchema:parse(value: unknown) → T

Validates value and returns it, throwing on failure.

Errors

TypeDescription
"expected ..."Thrown when a non-nil `value` fails validation

readonly​

Wrapper
DefaultSchema:readonly() → ReadonlySchema<T>

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​

Wrapper
DefaultSchema:writeonly() → WriteonlySchema<T>

Wraps this schema so that a field using it is typed write.

Show raw api
{
    "functions": [
        {
            "name": "safeParse",
            "desc": "Returns the fallback when `value` is `nil`, and otherwise defers to the\nwrapped schema. A fallback built by a function is validated too.\n\t",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "unknown"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Result<T>"
                }
            ],
            "function_type": "method",
            "tags": [
                "Parsing"
            ],
            "source": {
                "line": 974,
                "path": "src/init.luau"
            }
        },
        {
            "name": "parse",
            "desc": "Validates `value` and returns it, throwing on failure.\n\t",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "unknown"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "function_type": "method",
            "tags": [
                "Parsing"
            ],
            "errors": [
                {
                    "lua_type": "\"expected ...\"",
                    "desc": "Thrown when a non-nil `value` fails validation"
                }
            ],
            "source": {
                "line": 1015,
                "path": "src/init.luau"
            }
        },
        {
            "name": "readonly",
            "desc": "Wraps this schema so that a field using it is typed `read`. The fallback\nis still filled in, so the field is read-only but never missing.\n\t",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "ReadonlySchema<T>"
                }
            ],
            "function_type": "method",
            "tags": [
                "Wrapper"
            ],
            "source": {
                "line": 1034,
                "path": "src/init.luau"
            }
        },
        {
            "name": "writeonly",
            "desc": "Wraps this schema so that a field using it is typed `write`.\n\t",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "WriteonlySchema<T>"
                }
            ],
            "function_type": "method",
            "tags": [
                "Wrapper"
            ],
            "source": {
                "line": 1046,
                "path": "src/init.luau"
            }
        }
    ],
    "properties": [
        {
            "name": "isOptional",
            "desc": "Always `false`. A defaulted schema accepts a missing value but never\nproduces `nil`, so callers do not have to handle one.\n\t",
            "lua_type": "boolean",
            "tags": [
                "Reflection"
            ],
            "readonly": true,
            "source": {
                "line": 918,
                "path": "src/init.luau"
            }
        },
        {
            "name": "access",
            "desc": "Always `\"readwrite\"`. Chain `:readonly()` or `:writeonly()` to change it.\n\t",
            "lua_type": "\"readwrite\"",
            "tags": [
                "Reflection"
            ],
            "readonly": true,
            "source": {
                "line": 928,
                "path": "src/init.luau"
            }
        },
        {
            "name": "minimum",
            "desc": "Forwarded from the wrapped schema.\n\t",
            "lua_type": "number?",
            "tags": [
                "Reflection"
            ],
            "readonly": true,
            "source": {
                "line": 938,
                "path": "src/init.luau"
            }
        },
        {
            "name": "maximum",
            "desc": "Forwarded from the wrapped schema.\n\t",
            "lua_type": "number?",
            "tags": [
                "Reflection"
            ],
            "readonly": true,
            "source": {
                "line": 948,
                "path": "src/init.luau"
            }
        }
    ],
    "types": [],
    "name": "DefaultSchema",
    "desc": "Substitutes a fallback when the value is `nil`. Unlike [OptionalSchema] the\nresult is never `nil`, so downstream code does not have to handle a missing\nvalue.\n\n```lua\nlocal Volume = v.number():min(0):max(1):default(0.5)\n\nVolume:parse(nil)  --> 0.5\nVolume:parse(0.2)  --> 0.2\nVolume:parse(2)    --> errors\n```\n\nPass a function to build the fallback per parse, which matters for tables\nsince a constant would be shared by every caller:\n\n```lua\nlocal Tags = v.array(v.string()):default(function()\n\treturn {}\nend)\n```",
    "source": {
        "line": 887,
        "path": "src/init.luau"
    }
}