From f542d5c298d37012c090ec5131e90468a827db48 Mon Sep 17 00:00:00 2001 From: Paolo Brasolin <paolo.brasolin@eurac.edu> Date: Thu, 17 Mar 2022 12:01:50 +0100 Subject: [PATCH] feat: #be define Nullable for OpenAPI --- backend/src/api.ts | 10 ++++++++-- backend/src/index.ts | 2 -- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/backend/src/api.ts b/backend/src/api.ts index 3db0e61..5c5c0ab 100644 --- a/backend/src/api.ts +++ b/backend/src/api.ts @@ -1,8 +1,14 @@ import { FastifyPluginCallback } from "fastify"; -import { Static, Type } from "@sinclair/typebox"; + +import { Static, Type, TSchema, TUnion, TNull } from "@sinclair/typebox"; + +function Nullable<T extends TSchema>(schema: T): TUnion<[T, TNull]> { + return { ...schema, nullable: true } as any; +} import { connection } from "./db"; +// NOTE: refer to https://cloud.google.com/apis/design/ // NOTE: see https://www.npmjs.com/package/fastify-plugin for TS plugin definition const IdInParamsSchema = Type.Object({ @@ -25,7 +31,7 @@ type ClueType = Static<typeof ClueSchema>; const ShotSchema = Type.Object({ id: Type.Readonly(Type.String({ format: "uuid" })), game_id: Type.Readonly(Type.String({ format: "uuid" })), - clue_id: Type.Optional(Type.Readonly(Type.String({ format: "uuid" }))), + clue_id: Nullable(Type.String({ format: "uuid" })), began_at: Type.String({ format: "date-time" }), ended_at: Type.String({ format: "date-time" }), typed: Type.String(), diff --git a/backend/src/index.ts b/backend/src/index.ts index 26f5d5a..1519cb6 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -1,7 +1,5 @@ import fastify from "fastify"; import fastifyCors from "fastify-cors"; -import { connection } from "./db"; -import { FromSchema } from "json-schema-to-ts"; import fastifySwagger from "fastify-swagger"; const server = fastify({ -- GitLab