TypeCoercer → Coerce

Coerce

Convert a single raw value to a typed Roblox value, or validate an entire payload table against a field schema and get back a typed result alongside any per-field errors.

Coerce

TypeCoercer.Coerce(value: any, typeName: string) → (boolean, any)

Attempts to convert value to the type named by typeName. Returns (true, result) on success and (false, nil) on failure. The Router calls this automatically for typed route params — you only need it when coercing manually.

local TC = RoExpress.TypeCoercer

local ok, vec = TC.Coerce("10,5,-20", "vector3")
if ok then
    print(vec)   -- Vector3.new(10, 5, -20)
end

local ok2, num = TC.Coerce("abc", "number")
print(ok2)      -- false

CoerceTable

TypeCoercer.CoerceTable(t: \{\}, schema: \{\}) → (boolean, \{\}, \{\})

Validates and coerces every field in t against the corresponding type name in schema. Returns three values: overall success, a table of coerced values (may be partial on failure), and a table of per-field error messages for any field that failed.

local TC = RoExpress.TypeCoercer

-- Validate an incoming payload
app:Post("spawn", function(req, res)
    local ok, data, errs = TC.CoerceTable(req.data, {
        position = "vector3",
        rotation = "number",
        tool     = "string",
    })
    if not ok then
        return res:Error(400, errs)
    end
    -- data.position is now a Vector3, data.rotation a number, etc.
    res:Send({ ok = true })
end)
The Router calls Coerce automatically for typed route params such as :id=number. You only need to call Coerce or CoerceTable directly when validating raw req.data payload fields.

See also

← TypeCoercer  ·  Supported Types | every recognised type name and wire format  ·  Router | typed route params that call Coerce automatically