Merge pull request #108 from nxht/dev-#102

Move parameters schema keywords location
This commit is contained in:
Marc Laventure
2024-04-05 12:16:06 -07:00
committed by GitHub
3 changed files with 48 additions and 9 deletions
+17 -1
View File
@@ -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(
{
+3 -3
View File
@@ -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, ...schemaKeywords } = value as any
return {
// @ts-ignore
...rest,
schema: { type: valueType },
description, examples,
schema: { type: valueType, ...schemaKeywords },
in: name,
name: key,
// @ts-ignore
+28 -5
View File
@@ -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' }
)