Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move parameters schema keywords location #108

Merged
merged 5 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to keep this spread of rest at the root of the return and not in schema?

i'll be pulling and testing out tonight but wanted to give a review before that and ask ahead :)

Copy link
Contributor Author

@nxht nxht Mar 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marclave
Had a thought of it and I think it makes more sense to be under schema.

Most JSON schema keywords goes under schema except for description, required, examples. So there's lesser keywords to define

Also when we choose to define some keywords and send others to spread, it makes more sense to me to define generic keywords explicitly and send type-specific keywords like minLength, which belongs to schema, to the 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 } }) => ({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome! more tests :)

...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