Skip to main content

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 OnlyReflection
ReadonlySchema.access: "read"

Always "read".

isOptional​

This item is read only and cannot be modified. Read OnlyReflection
ReadonlySchema.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 OnlyReflection
ReadonlySchema.minimum: number?

Forwarded from the wrapped schema.

maximum​

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

Forwarded from the wrapped schema.

Functions​

safeParse​

Parsing
ReadonlySchema:safeParse(value: unknown) → Result<T>

Defers to the wrapped schema. The marker changes the type a surrounding object produces, never what is accepted.

parse​

Parsing
ReadonlySchema:parse(value: unknown) → T

Validates value and returns it, throwing on failure.

Errors

TypeDescription
"expected ..."Thrown when `value` fails the wrapped schema

readonly​

Wrapper
ReadonlySchema:readonly() → ReadonlySchema<T>

Returns the same schema, since it is already read-only.

writeonly​

Wrapper
ReadonlySchema:writeonly() → WriteonlySchema<T>

Returns a write-only wrapper around the same schema, replacing the read-only marker.

Show raw api
{
    "functions": [
        {
            "name": "safeParse",
            "desc": "Defers to the wrapped schema. The marker changes the type a surrounding\nobject produces, never what is accepted.\n\t",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "unknown"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Result<T>"
                }
            ],
            "function_type": "method",
            "tags": [
                "Parsing"
            ],
            "source": {
                "line": 562,
                "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 `value` fails the wrapped schema"
                }
            ],
            "source": {
                "line": 592,
                "path": "src/init.luau"
            }
        },
        {
            "name": "readonly",
            "desc": "Returns the same schema, since it is already read-only.\n\t",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "ReadonlySchema<T>"
                }
            ],
            "function_type": "method",
            "tags": [
                "Wrapper"
            ],
            "source": {
                "line": 621,
                "path": "src/init.luau"
            }
        },
        {
            "name": "writeonly",
            "desc": "Returns a write-only wrapper around the same schema, replacing the\nread-only marker.\n\t",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "WriteonlySchema<T>"
                }
            ],
            "function_type": "method",
            "tags": [
                "Wrapper"
            ],
            "source": {
                "line": 648,
                "path": "src/init.luau"
            }
        }
    ],
    "properties": [
        {
            "name": "access",
            "desc": "Always `\"read\"`.\n\t",
            "lua_type": "\"read\"",
            "tags": [
                "Reflection"
            ],
            "readonly": true,
            "source": {
                "line": 479,
                "path": "src/init.luau"
            }
        },
        {
            "name": "isOptional",
            "desc": "Forwarded from the wrapped schema, so `v.string():optional():readonly()`\nstill reports itself as optional.\n\t",
            "lua_type": "boolean",
            "tags": [
                "Reflection"
            ],
            "readonly": true,
            "source": {
                "line": 499,
                "path": "src/init.luau"
            }
        },
        {
            "name": "minimum",
            "desc": "Forwarded from the wrapped schema.\n\t",
            "lua_type": "number?",
            "tags": [
                "Reflection"
            ],
            "readonly": true,
            "source": {
                "line": 518,
                "path": "src/init.luau"
            }
        },
        {
            "name": "maximum",
            "desc": "Forwarded from the wrapped schema.\n\t",
            "lua_type": "number?",
            "tags": [
                "Reflection"
            ],
            "readonly": true,
            "source": {
                "line": 537,
                "path": "src/init.luau"
            }
        }
    ],
    "types": [],
    "name": "ReadonlySchema",
    "desc": "Marks a field as read-only, so the record an [ObjectSchema] parses to\ndeclares it `read`. Created by calling `:readonly()` on any schema, or by\nwrapping one with [Vet.readonly]. It comes last in a chain, after the\nrefinements and after `:optional()` or `:default()`.\n\n```lua\nlocal PlayerData = v.object({\n\tid = v.number():integer():readonly(),\n\tname = v.string(),\n})\n\ntype PlayerData = v.infer<typeof(PlayerData)>\n--> { read id: number, name: string }\n\nlocal data = PlayerData:parse(payload)\n\nprint(data.id) --> fine\ndata.id = 2    --> analysis error: `id` is read-only\n```\n\nValidation is untouched: the field is parsed exactly as the wrapped schema\nwould parse it, and the value is there at runtime. Only the type differs, so\nthe restriction is caught during analysis rather than enforced by a\nmetatable on every write.",
    "source": {
        "line": 388,
        "path": "src/init.luau"
    }
}