Skip to content

Commit

Permalink
fix: #1353 ignore evaluation of $schema field in json-schema (#1354)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eomm authored and delvedor committed Dec 26, 2018
1 parent fc768bf commit 0ee57e7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/schemas.js
Expand Up @@ -66,7 +66,7 @@ Schemas.prototype.resolveRefs = function (routeSchemas) {

Schemas.prototype.traverse = function (schema) {
for (var key in schema) {
if (typeof schema[key] === 'string' && schema[key].slice(-1) === '#') {
if (typeof schema[key] === 'string' && key !== '$schema' && schema[key].slice(-1) === '#') {
schema[key] = this.resolve(schema[key].slice(0, -1))
}

Expand Down
36 changes: 35 additions & 1 deletion test/input-validation.test.js
Expand Up @@ -2,10 +2,11 @@

const t = require('tap')
const test = t.test
const fastify = require('..')()
const Fastify = require('..')

test('case insensitive header validation', t => {
t.plan(2)
const fastify = Fastify()
fastify.route({
method: 'GET',
url: '/',
Expand Down Expand Up @@ -33,3 +34,36 @@ test('case insensitive header validation', t => {
t.equal(res.payload, 'Baz')
})
})

test('not evaluate json-schema $schema keyword', t => {
t.plan(2)
const fastify = Fastify()
fastify.route({
method: 'POST',
url: '/',
handler: (req, reply) => {
reply.code(200).send(req.body.hello)
},
schema: {
body: {
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
properties: {
hello: {
type: 'string'
}
}
}
}
})
fastify.inject({
method: 'POST',
url: '/',
body: {
hello: 'world'
}
}, (err, res) => {
t.error(err)
t.equal(res.payload, 'world')
})
})

0 comments on commit 0ee57e7

Please sign in to comment.