1
0
Fork 0
mirror of https://github.com/kou029w/_.git synced 2025-01-30 22:08:02 +00:00
_/fastify-validation/server.ts

107 lines
2.2 KiB
TypeScript
Raw Normal View History

#!/usr/bin/env -S deno run -A
import fastify from "npm:fastify";
// import { parse as jsonParse } from "npm:secure-json-parse";
2023-09-11 17:44:55 +09:00
const server = fastify({ logger: true });
server.get("/anyOf", {
schema: {
querystring: {
type: "object",
properties: {
array: {
anyOf: [
{ type: "array", items: { type: "string" } },
{ type: "string" },
],
},
},
required: ["array"],
},
},
handler({ query }) {
console.log({ ...query });
return "ok";
},
});
server.get("/oneOf", {
schema: {
querystring: {
type: "object",
properties: {
array: {
oneOf: [
{ type: "array", items: { type: "string" } },
{ type: "string" },
],
},
},
required: ["array"],
},
},
handler({ query }) {
console.log({ ...query });
return "ok";
},
});
// valid
await server.inject(`/anyOf?array=a`);
// invalid: querystring/array must match exactly one schema in oneOf
await server.inject(`/oneOf?array=a`);
/*
2024-12-09 17:19:05 +09:00
server.get("/array", {
schema: {
querystring: {
type: "object",
properties: { array: { type: "array", items: { type: "string" } } },
required: ["array"],
},
},
async preValidation(req) {
req.query.array = [req.query.array].flat().flatMap((s) => {
try {
const parsed = jsonParse(s);
2024-12-09 17:19:05 +09:00
return Array.isArray(parsed) ? parsed : [s];
} catch {
return [s];
}
});
},
handler({ query }) {
console.log({ ...query });
return "ok";
},
});
// valid
await server.inject(`/array?array=["123",4]`);
*/
2024-12-09 17:19:05 +09:00
/*
server.get("/dateTime", {
2023-09-11 17:44:55 +09:00
schema: {
querystring: {
type: "object",
properties: { dateTime: { type: "string", format: "date-time" } },
required: ["dateTime"],
},
},
handler() {
return "ok";
},
});
// valid
2024-12-09 17:19:05 +09:00
await server.inject("/dateTime?dateTime=2021-09-10T15:30:00Z");
2023-09-11 17:44:55 +09:00
2024-12-09 17:19:05 +09:00
// valid: fasitfy@4.22.2, invalid fastify@5.1.0: querystring/dateTime must match format "date-time"
await server.inject("/dateTime?dateTime=2021-09-10T15:30:00");
2023-09-11 17:44:55 +09:00
// invalid: querystring/dateTime must match format "date-time"
2024-12-09 17:19:05 +09:00
await server.inject("/dateTime?dateTime=2021-09-10");
*/