Validation
Check params, query and body with Standard Schema (Zod, Valibot, ArkType)
Give a route schemas, and they are checked before the handler is called. Schemas from libraries that support Standard Schema (Zod, Valibot, ArkType and others) work as is.
import { } from "@senrijs/senri";
import { } from "zod";
const = ();
.(
"/users/:id",
{
: .({ : ..() }),
: .({ : .().(1) }),
},
({ }, ) => {
const { name } = .("body"); return { , };
},
);- You can give
params,queryandbody - When
paramspasses, the handler's first argument is the converted value (idabove is anumber) - Read the
queryandbodyvalues withc.valid("query")andc.valid("body") - For table routes, write
endpoint({ body }, handler)(Route tables)
Read a path value as an integer, without a schema
Path values are always strings. To read one as an integer, write "int" or "uint" instead of a schema.
.("/notes/:id", { : { : "uint" } }, ({ }) => {
return { id };});"int"is an integer (negative allowed), and"uint"an integer of 0 or more. Use"uint"for an:id- Only the names you list become
number. Other parameters stay strings - Only the handler's first argument has numbers.
c.paramskeeps the strings, as its type says - A value other than
"int"or"uint"is a type error, and is also refused when the route is registered (in production too) - A name that is not in the path is a type error with
app.get()and the other methods, and withcreateRouter(). Development mode also refuses it when anapp.routes()table is registered - It works together with
queryandbodyschemas.paramstakes either a schema or{ id: "int" } - It works the same with
createRouter()andendpoint(). In OpenAPI, it becomesinteger(withminimum: 0for"uint")
What counts as an integer
Only values that match ^(0|-?[1-9][0-9]*)$ and for which Number.isSafeInteger() is true. "uint" takes the ones of 0 or more. Ambiguous values are never turned into integers.
| Path value | "int" | "uint" |
|---|---|---|
0, 7, 42, 9007199254740991 (Number.MAX_SAFE_INTEGER) | that number | that number |
-1, -42, -9007199254740991 | that number | 404 |
01, 00 (leading zero) | 404 | 404 |
1.5, 1.0 (decimals) | 404 | 404 |
" 1 " (spaces around it; %201%20 in the URL) | 404 | 404 |
+1, -0 | 404 | 404 |
1e3, 0x10, 1_000 (other ways to write a number) | 404 | 404 |
9007199254740992, -9007199254740992 (outside the safe integer range) | 404 | 404 |
1 (full-width digit) | 404 | 404 |
abc, -, Infinity, NaN | 404 | 404 |
When it is not an integer
The handler is not called, and the response is 404: the same as "there is nothing with that id" (not the 400 of a failed schema).
- The
app.notFoundhandler responds (the same asc.notFound()) - Production: the body is only
404 Not Found - Development mode: the body says which name got what
404 Not Found: GET /notes/abc
:id got "abc", which is not an integer of 0 or more ("uint": 0 or digits without a leading zero, up to Number.MAX_SAFE_INTEGER)When a value does not pass
When a schema does not pass, the response is 400.
- Production: the body is only
400 Bad Request - Development mode: the body lists what is wrong. Mistakes in the schema itself are reported when the route is registered
Respond in your own format
Catch ValidationError in app.onError. It has err.target ("params", "query" or "body") and err.issues (the list of problems).
import { } from "@senrijs/senri";
.((, ) => {
if ( instanceof ) {
.(422);
return { : ., : ..(() => .) };
}
throw ;
});For the RFC 9457 format, you can also use problemDetails({ validationErrors: true }) from Problem Details.