Skip to content

Commit

Permalink
Merge pull request #108 from nxht/dev-#102
Browse files Browse the repository at this point in the history
Move parameters schema keywords location
  • Loading branch information
marclave committed Apr 5, 2024
2 parents ca692c4 + 3e26524 commit 5e78190
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
18 changes: 17 additions & 1 deletion example/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}),
Expand All @@ -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(
{
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 28 additions & 5 deletions test/validateSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand All @@ -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(),
Expand All @@ -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' }
)
Expand Down

0 comments on commit 5e78190

Please sign in to comment.