Skip to content

Commit 5e78190

Browse files
authored
Merge pull request #108 from nxht/dev-#102
Move parameters schema keywords location
2 parents ca692c4 + 3e26524 commit 5e78190

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

example/plugin.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const plugin = new Elysia({
4444
})
4545
.post(
4646
'/json/:id',
47-
({ body, params: { id }, query: { name } }) => ({
47+
({ body, params: { id }, query: { name, email, } }) => ({
4848
...body,
4949
id
5050
}),
@@ -53,6 +53,22 @@ export const plugin = new Elysia({
5353
params: t.Object({
5454
id: t.Numeric()
5555
}),
56+
query: t.Object({
57+
name: t.String(),
58+
email: t.String({
59+
description: 'sample email description',
60+
format: 'email',
61+
examples: ['[email protected]']
62+
}),
63+
birthday: t.String({
64+
description: 'sample birthday description',
65+
pattern: '\\d{4}-\\d{2}-\\d{2}',
66+
minLength: 10,
67+
maxLength: 10,
68+
examples: ['2024-01-01']
69+
}),
70+
gender: t.Union([t.Literal('M'), t.Literal('F')])
71+
}),
5672
response: {
5773
200: t.Object(
5874
{

src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ export const mapProperties = (
2525
else throw new Error(`Can't find model ${schema}`)
2626

2727
return Object.entries(schema?.properties ?? []).map(([key, value]) => {
28-
const { type: valueType = undefined, ...rest } = value as any
28+
const { type: valueType = undefined, description, examples, ...schemaKeywords } = value as any
2929
return {
3030
// @ts-ignore
31-
...rest,
32-
schema: { type: valueType },
31+
description, examples,
32+
schema: { type: valueType, ...schemaKeywords },
3333
in: name,
3434
name: key,
3535
// @ts-ignore

test/validateSchema.test.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Elysia, t } from 'elysia'
22
import SwaggerParser from '@apidevtools/swagger-parser'
33
import { swagger } from '../src'
44

5-
import { describe, expect, it } from 'bun:test'
5+
import { it } from 'bun:test'
66
import { fail } from 'assert'
77

88
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 () => {
2727
)
2828
.post(
2929
'/json/:id',
30-
({ body, params: { id }, query: { name } }) => ({
30+
({ body, params: { id }, query: { name, email, birthday } }) => ({
3131
...body,
3232
id,
33-
name
33+
name,
34+
email,
35+
birthday
3436
}),
3537
{
3638
params: t.Object({
3739
id: t.String()
3840
}),
3941
query: t.Object({
40-
name: t.String()
42+
name: t.String(),
43+
email: t.String({
44+
description: 'sample email description',
45+
format: 'email'
46+
}),
47+
birthday: t.String({
48+
description: 'sample birthday description',
49+
pattern: '\\d{4}-\\d{2}-\\d{2}',
50+
minLength: 10,
51+
maxLength: 10
52+
}),
53+
4154
}),
4255
body: t.Object({
4356
username: t.String(),
@@ -48,7 +61,17 @@ it('returns a valid Swagger/OpenAPI json config for many routes', async () => {
4861
username: t.String(),
4962
password: t.String(),
5063
id: t.String(),
51-
name: t.String()
64+
name: t.String(),
65+
email: t.String({
66+
description: 'sample email description',
67+
format: 'email'
68+
}),
69+
birthday: t.String({
70+
description: 'sample birthday description',
71+
pattern: '\\d{4}-\\d{2}-\\d{2}',
72+
minLength: 10,
73+
maxLength: 10
74+
}),
5275
},
5376
{ description: 'sample description 3' }
5477
)

0 commit comments

Comments
 (0)