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:
[[email protected] 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:
[[email protected] 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:
[[email protected] 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:
[[email protected] 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
- 8.8K All Categories
- 13 LFX Mentorship
- 66 LFX Mentorship: Linux Kernel
- 355 Linux Foundation Boot Camps
- 228 Cloud Engineer Boot Camp
- 67 Advanced Cloud Engineer Boot Camp
- 23 DevOps Engineer Boot Camp
- 4 Cloud Native Developer Boot Camp
- 738 Training Courses
- 14 LFC110 Class Forum
- 16 LFD102 Class Forum
- 96 LFD103 Class Forum
- 2 LFD121 Class Forum
- 55 LFD201 Class Forum
- 1 LFD213 Class Forum - Discontinued
- 128 LFD232 Class Forum
- 14 LFD254 Class Forum
- 425 LFD259 Class Forum
- 78 LFD272 Class Forum
- 1 LFD272-JP クラス フォーラム
- 15 LFS200 Class Forum
- 686 LFS201 Class Forum
- LFS201-JP クラス フォーラム
- 271 LFS211 Class Forum
- 50 LFS216 Class Forum
- 23 LFS241 Class Forum
- 26 LFS242 Class Forum
- 18 LFS243 Class Forum
- 4 LFS244 Class Forum
- 7 LFS250 Class Forum
- LFS250-JP クラス フォーラム
- 105 LFS253 Class Forum
- 754 LFS258 Class Forum
- 7 LFS258-JP クラス フォーラム
- 48 LFS260 Class Forum
- 75 LFS261 Class Forum
- 6 LFS262 Class Forum
- 76 LFS263 Class Forum
- 14 LFS264 Class Forum
- 10 LFS266 Class Forum
- 8 LFS267 Class Forum
- 8 LFS268 Class Forum
- 5 LFS269 Class Forum
- 173 LFS272 Class Forum
- 1 LFS272-JP クラス フォーラム
- 184 LFW211 Class Forum
- 100 LFW212 Class Forum
- 876 Hardware
- 205 Drivers
- 74 I/O Devices
- 43 Monitors
- 115 Multimedia
- 204 Networking
- 98 Printers & Scanners
- 82 Storage
- 716 Linux Distributions
- 78 Debian
- 64 Fedora
- 12 Linux Mint
- 13 Mageia
- 22 openSUSE
- 125 Red Hat Enterprise
- 33 Slackware
- 13 SUSE Enterprise
- 344 Ubuntu
- 445 Linux System Administration
- 33 Cloud Computing
- 63 Command Line/Scripting
- Github systems admin projects
- 88 Linux Security
- 73 Network Management
- 105 System Management
- 45 Web Management
- 50 Mobile Computing
- 18 Android
- 19 Development
- 1.2K New to Linux
- 1.1K Getting Started with Linux
- 499 Off Topic
- 119 Introductions
- 193 Small Talk
- 19 Study Material
- 743 Programming and Development
- 237 Kernel Development
- 472 Software Development
- 899 Software
- 245 Applications
- 178 Command Line
- 2 Compiling/Installing
- 72 Games
- 313 Installation
- 19 All In Program
- 19 All In 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)