Skip to content

Commit

Permalink
fix: no store compiled schema ids (#4109)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eomm committed Jul 4, 2022
1 parent 69df0e3 commit b48f608
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -177,7 +177,7 @@
"yup": "^0.32.11"
},
"dependencies": {
"@fastify/ajv-compiler": "^3.1.0",
"@fastify/ajv-compiler": "^3.1.1",
"@fastify/error": "^3.0.0",
"@fastify/fast-json-stringify-compiler": "^4.0.0",
"abstract-logging": "^2.0.1",
Expand Down
71 changes: 71 additions & 0 deletions test/schema-validation.test.js
Expand Up @@ -4,6 +4,7 @@ const { test } = require('tap')
const Fastify = require('..')

const AJV = require('ajv')
const Schema = require('fluent-json-schema')

const customSchemaCompilers = {
body: new AJV({
Expand Down Expand Up @@ -946,3 +947,73 @@ test('Custom AJV settings on different parameters - pt2', t => {
}
})
})

test("The same $id in route's schema must not overwrite others", t => {
t.plan(4)
const fastify = Fastify()

const UserSchema = Schema.object()
.id('http://mydomain.com/user')
.title('User schema')
.description('Contains all user fields')
.prop('id', Schema.integer())
.prop('username', Schema.string().minLength(4))
.prop('firstName', Schema.string().minLength(1))
.prop('lastName', Schema.string().minLength(1))
.prop('fullName', Schema.string().minLength(1))
.prop('email', Schema.string())
.prop('password', Schema.string().minLength(6))
.prop('bio', Schema.string())

const userCreateSchema = UserSchema.only([
'username',
'firstName',
'lastName',
'email',
'bio',
'password',
'password_confirm'
])
.required([
'username',
'firstName',
'lastName',
'email',
'bio',
'password'
])

const userPatchSchema = UserSchema.only([
'firstName',
'lastName',
'bio'
])

fastify
.patch('/user/:id', {
schema: { body: userPatchSchema },
handler: () => { return 'ok' }
})
.post('/user', {
schema: { body: userCreateSchema },
handler: () => { return 'ok' }
})

fastify.inject({
method: 'POST',
url: '/user',
body: {}
}, (err, res) => {
t.error(err)
t.same(res.json().message, "body must have required property 'username'")
})

fastify.inject({
url: '/user/1',
method: 'PATCH',
body: {}
}, (err, res) => {
t.error(err)
t.same(res.payload, 'ok')
})
})

0 comments on commit b48f608

Please sign in to comment.