From 37b0e4e4172e09e4d612a894805c2636addd0f68 Mon Sep 17 00:00:00 2001 From: Manuel Spigolon Date: Mon, 4 Jul 2022 07:45:01 +0200 Subject: [PATCH] fix: no store compiled schema ids --- package.json | 2 +- test/schema-validation.test.js | 71 ++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index e9cb0edb13..3416cef37d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/schema-validation.test.js b/test/schema-validation.test.js index 2980e24a1e..05ce41be5a 100644 --- a/test/schema-validation.test.js +++ b/test/schema-validation.test.js @@ -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({ @@ -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') + }) +})