Lesson 9. Route Validation with Fastify
Hi, everyone.
1.- I'm testing the response schema in the post route, if I intentionally change the return line inside the handler to {test: 'test'} to break the schema validation for 201 response the route still works and return a 201 statusCode without any error. Unlike the get route where if I intentionally change the return line inside the handler to {test: 'test'} the response is 500 with the message "\"brand\" is required!". Here the code of /routes/bicycle/index.js:
'use strict'
const { promisify } = require('util')
const { bicycle } = require('../../model')
const read = promisify(bicycle.read)
const create = promisify(bicycle.create)
const update = promisify(bicycle.update)
const del = promisify(bicycle.del)
const dataSchema = {
type: 'object',
required: ['brand', 'color'],
additionalProperties: false,
properties:{
brand: {
type: 'string'
},
color: {
type: 'string'
}
}
}
const idSchema = {
type: 'integer'
}
const paramSchema = {
id: idSchema
}
const bodySchema = {
type: 'object',
required: ['data'],
additionalProperties: false,
properties: {
data: dataSchema
}
}
module.exports = async (fastify, opts) => {
const { httpErrors} = fastify
fastify.get('/:id', {
schema: {
params: paramSchema,
response: {
200: dataSchema
}
}
}, async(request, reply) => {
return { test: 'test'}
}
)
fastify.post('/', {
schema: {
body: bodySchema,
response: {
201: {
id: idSchema
}
}
}
}
, async (request, reply) => {
reply.code(201)
return { test: 'test'}
}
)
}
Testing the routes:
For GET:
[vagrant@localhost practices-nodejs-course-lfw212]$ node -e "http.get('http://localhost:3000/bicycle/3', (res)=> res.setEncoding('utf8').once('data', console.log))"
The output: {"statusCode":500,"error":"Internal Server Error","message":"\"brand\" is required!"}
For POST:
[vagrant@localhost practices-nodejs-course-lfw212]$ node -e "http.request('http://localhost:3000/bicycle', { method: 'post', headers: {'content-type': 'application/json'}}, (res) => res.setEncoding('utf8').once('data', console.log.bind(null, res.statusCode))).end(JSON.stringify({data: {brand: 'AnotherBrand', color: 'brown'}}))"
The output: 201 {}
2.- Changing the return line inside the handler in the post route to { id: 'id with wrong type'} that is the correct property but with a wrong type (string instead of integer) the response is the same 201 with an empty json.
3.- If I change the 201 response schema in the post route to:
{
type: 'object',
required: ['id'],
additionalProperties: false,
properties:{
id: idSchema
}
}
The output is the expected for the above cases ( returning { test: 'test'} or { id: 'id with wrong type'}) a 500 code with the message id is required. This makes sense since the previous schema does not tell Fastify that the response is an object and that the property id is required.
But if I change again the return line inside the handler to a simple string 'Wrong type' the output is a successful response code, This happens even in the get route with its dataSchema schema, when I change the return line to return 'Wrong type'
For GET:
[vagrant@localhost practices-nodejs-course-lfw212]$ node -e "http.get('http://localhost:3000/bicycle/1', (res)=> res.setEncoding('utf8').once('data', console.log.bind(null, res.statusCode)))"
The output: 200 Wrong type
For POST:
[vagrant@localhost practices-nodejs-course-lfw212]$ node -e "http.request('http://localhost:3000/bicycle', { method: 'post', headers: {'content-type': 'application/json'}}, (res) => res.setEncoding('utf8').once('data', console.log.bind(null, res.statusCode))).end(JSON.stringify({data: {brand: 'AnotherBrand', color: 'brown'}}))"
The output: 201 Wrong type
Any thoughts? Looks like Fastify does not contemplate this last case when a return is of a different type of the declared in the schema
Thanks in advance.
Gusmen.
Comments
-
@gusmenp I spoke with the lead maintainer of Fastify, this is a known issue. It's because of a rough edge in the implementation, this is one of those 99%/1% situations where in practice it's not ideal but generally quite rare and not a huge issue. However, in the next major release (v4) they are anticipating some of the more fundamental internal changes will enable this to be addressed.
0
Categories
- All Categories
- 176 LFX Mentorship
- 176 LFX Mentorship: Linux Kernel
- 750 Linux Foundation IT Professional Programs
- 373 Cloud Engineer IT Professional Program
- 169 Advanced Cloud Engineer IT Professional Program
- 74 DevOps IT Professional Program - Discontinued
- 4 DevOps & GitOps IT Professional Program
- 99 Cloud Native Developer IT Professional Program
- 7.6K Training Courses & Learning Paths
- 1 AI & ML Training
- 1 Blockchain & Decentralized Identity Training
- 3 Cloud & Containers Training
- 1 Cybersecurity Training
- 1 DevOps & Site-Reliability Training
- 1 Linux Kernel Development Training
- 1 Networking Training
- 1 Open Source Best Practice Training
- 1 System Administration Training
- 1 System Engineering Training
- 1 Web & Application Development Training
- 792 Hardware
- 202 Drivers
- 68 I/O Devices
- 37 Monitors
- 95 Multimedia
- 173 Networking
- 91 Printers & Scanners
- 87 Storage
- 768 Linux Distributions
- 81 Debian
- 67 Fedora
- 22 Linux Mint
- 13 Mageia
- 24 openSUSE
- 150 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 356 Ubuntu
- 465 Linux System Administration
- 31 Cloud Computing
- 73 Command Line/Scripting
- Github systems admin projects
- 98 Linux Security
- 78 Network Management
- 101 System Management
- 46 Web Management
- 106 Mobile Computing
- 18 Android
- 73 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 392 Off Topic
- 121 Introductions
- 181 Small Talk
- 29 Study Material
- 949 Programming and Development
- 310 Kernel Development
- 621 Software Development
- 982 Software
- 374 Applications
- 182 Command Line
- 5 Compiling/Installing
- 68 Games
- 317 Installation
- Archived
- 2 LFD140 Class Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)
