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

Fix: Typebox isn't able to deref nested references #863

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 6 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,8 @@ export const getSchemaValidator = <T extends TSchema | string | undefined>(
if (schema.type === 'object' && 'additionalProperties' in schema === false)
schema.additionalProperties = additionalProperties

const cleaner = (value: unknown) => Value.Clean(schema, value)
const references = Object.values(models)
const cleaner = (value: unknown) => Value.Clean(schema, references, value)

if (dynamic) {
const validator = {
Expand Down Expand Up @@ -714,10 +715,10 @@ export const getResponseSchemaValidator = (
const compile = (schema: TSchema, references?: TSchema[]) => {
const cleaner = (value: unknown) => {
if (!value || typeof value !== 'object')
return Value.Clean(schema, value)
return Value.Clean(schema, references || [], value)

if (Array.isArray(value)) value = Value.Clean(schema, value)
else value = Value.Clean(schema, value)
if (Array.isArray(value)) value = Value.Clean(schema, references || [], value)
else value = Value.Clean(schema, references || [], value)

return value
}
Expand Down Expand Up @@ -1194,7 +1195,7 @@ export const createMacroManager =
| {
insert?: 'before' | 'after'
stack?: 'global' | 'local'
}
}
| MaybeArray<HookContainer>,
fn?: MaybeArray<HookContainer>
) => {
Expand Down
27 changes: 27 additions & 0 deletions test/validator/body.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,33 @@ describe('Body Validator', () => {
expect(res.status).toBe(200)
})

it('validate references', async () => {
const job = t.Object({
name : t.String()
}, { $id: "job" })

const person = t.Object({
name : t.String(),
job : t.Ref(job)
})

const app = new Elysia()
.model({ job, person })
.post('/', ({ body: { name, job } }) => `${name} - ${job.name}`, {
body : person
})

const res = await app.handle(
post('/', {
name : 'sucrose',
job: { name: 'alchemist' }
})
)

expect(await res.text()).toBe('sucrose - alchemist')
expect(res.status).toBe(200)
})

it('validate optional primitive', async () => {
const app = new Elysia().post('/', ({ body }) => body ?? 'sucrose', {
body: t.Optional(t.String())
Expand Down
23 changes: 23 additions & 0 deletions test/validator/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,29 @@ describe('Response Validator', () => {
expect(res.status).toBe(200)
})

it('validate nested references', async () => {
const job = t.Object({
name : t.String()
}, { $id: "job" })

const person = t.Object({
name : t.String(),
job : t.Ref(job)
})

const app = new Elysia()
.model({ job, person })
.get('/', () => ({
name : 'sucrose',
job: { name: 'alchemist' }
}), {
response : person
})

const res = await app.handle(req('/'))
expect(res.status).toBe(200)
})

it('validate optional', async () => {
const app = new Elysia().get(
'/',
Expand Down