From 1348638cf8d5099f835a7a2d457f429450e66ea2 Mon Sep 17 00:00:00 2001 From: hgpark Date: Fri, 22 Mar 2024 12:25:15 +0900 Subject: [PATCH 1/5] fix: move parameter schema keywords location --- src/utils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 1efa070..4ed6275 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -25,11 +25,11 @@ export const mapProperties = ( else throw new Error(`Can't find model ${schema}`) return Object.entries(schema?.properties ?? []).map(([key, value]) => { - const { type: valueType = undefined, ...rest } = value as any + const { type: valueType = undefined, description, examples, ...schemaProperties } = value as any return { // @ts-ignore - ...rest, - schema: { type: valueType }, + description, examples, + schema: { type: valueType, ...schemaProperties }, in: name, name: key, // @ts-ignore From dfb41bdc080a90e49d77c14660b892abca98af17 Mon Sep 17 00:00:00 2001 From: hgpark Date: Fri, 22 Mar 2024 12:30:11 +0900 Subject: [PATCH 2/5] feat: add schema examples --- example/plugin.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/example/plugin.ts b/example/plugin.ts index a57a204..fbb14d2 100644 --- a/example/plugin.ts +++ b/example/plugin.ts @@ -44,7 +44,7 @@ export const plugin = new Elysia({ }) .post( '/json/:id', - ({ body, params: { id }, query: { name } }) => ({ + ({ body, params: { id }, query: { name, email, } }) => ({ ...body, id }), @@ -53,6 +53,22 @@ export const plugin = new Elysia({ params: t.Object({ id: t.Numeric() }), + query: t.Object({ + name: t.String(), + email: t.String({ + description: 'sample email description', + format: 'email', + examples: ['test@test.com'] + }), + birthday: t.String({ + description: 'sample birthday description', + pattern: '\\d{4}-\\d{2}-\\d{2}', + minLength: 10, + maxLength: 10, + examples: ['2024-01-01'] + }), + gender: t.Union([t.Literal('M'), t.Literal('F')]) + }), response: { 200: t.Object( { From c6727c7144f152403af54eeb82306bca591ecd0f Mon Sep 17 00:00:00 2001 From: hgpark Date: Fri, 22 Mar 2024 12:30:28 +0900 Subject: [PATCH 3/5] refactor: rename schemaProperties to schemaKeywords --- src/utils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 4ed6275..84c085b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -25,11 +25,11 @@ export const mapProperties = ( else throw new Error(`Can't find model ${schema}`) return Object.entries(schema?.properties ?? []).map(([key, value]) => { - const { type: valueType = undefined, description, examples, ...schemaProperties } = value as any + const { type: valueType = undefined, description, examples, ...schemaKeywords } = value as any return { // @ts-ignore description, examples, - schema: { type: valueType, ...schemaProperties }, + schema: { type: valueType, ...schemaKeywords }, in: name, name: key, // @ts-ignore From 4ae982987fec7acc9798ba4b1dea706ca526f2b4 Mon Sep 17 00:00:00 2001 From: hgpark Date: Fri, 22 Mar 2024 12:30:43 +0900 Subject: [PATCH 4/5] test: add schemaKeyword tests --- test/validateSchema.test.ts | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/test/validateSchema.test.ts b/test/validateSchema.test.ts index 96b9bad..b9306ed 100644 --- a/test/validateSchema.test.ts +++ b/test/validateSchema.test.ts @@ -2,7 +2,7 @@ import { Elysia, t } from 'elysia' import SwaggerParser from '@apidevtools/swagger-parser' import { swagger } from '../src' -import { describe, expect, it } from 'bun:test' +import { it } from 'bun:test' import { fail } from 'assert' const req = (path: string) => new Request(`http://localhost${path}`) @@ -27,17 +27,30 @@ it('returns a valid Swagger/OpenAPI json config for many routes', async () => { ) .post( '/json/:id', - ({ body, params: { id }, query: { name } }) => ({ + ({ body, params: { id }, query: { name, email, birthday } }) => ({ ...body, id, - name + name, + email, + birthday }), { params: t.Object({ id: t.String() }), query: t.Object({ - name: t.String() + name: t.String(), + email: t.String({ + description: 'sample email description', + format: 'email' + }), + birthday: t.String({ + description: 'sample birthday description', + pattern: '\\d{4}-\\d{2}-\\d{2}', + minLength: 10, + maxLength: 10 + }), + }), body: t.Object({ username: t.String(), @@ -48,7 +61,17 @@ it('returns a valid Swagger/OpenAPI json config for many routes', async () => { username: t.String(), password: t.String(), id: t.String(), - name: t.String() + name: t.String(), + email: t.String({ + description: 'sample email description', + format: 'email' + }), + birthday: t.String({ + description: 'sample birthday description', + pattern: '\\d{4}-\\d{2}-\\d{2}', + minLength: 10, + maxLength: 10 + }), }, { description: 'sample description 3' } ) @@ -56,5 +79,6 @@ it('returns a valid Swagger/OpenAPI json config for many routes', async () => { ) const res = await app.handle(req('/swagger/json')).then((x) => x.json()) + console.log(res.paths['/json/{id}'].post.parameters) await SwaggerParser.validate(res).catch((err) => fail(err)) }) From 3e26524254b07a88a0ce88471060303d9f51544b Mon Sep 17 00:00:00 2001 From: hgpark Date: Fri, 22 Mar 2024 12:41:07 +0900 Subject: [PATCH 5/5] style: remove unnecessary console.log --- test/validateSchema.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/validateSchema.test.ts b/test/validateSchema.test.ts index b9306ed..bcc74da 100644 --- a/test/validateSchema.test.ts +++ b/test/validateSchema.test.ts @@ -79,6 +79,5 @@ it('returns a valid Swagger/OpenAPI json config for many routes', async () => { ) const res = await app.handle(req('/swagger/json')).then((x) => x.json()) - console.log(res.paths['/json/{id}'].post.parameters) await SwaggerParser.validate(res).catch((err) => fail(err)) })