Skip to content

Commit 71d880d

Browse files
[FIX] - fix getErrorMessage naming, regex lint error, Test no options when registering
1 parent 55e5bae commit 71d880d

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

index.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
const fp = require('fastify-plugin')
22
const {
3-
getErrrorMessage,
3+
getErrorMessage,
44
hasProperties,
55
initialExcludes,
66
isSchemaTypeExcluded,
77
SCHEMA_TYPES
88
} = require('./utils')
99

10-
function FastifyEnforceSchema (fastify, opts, done) {
11-
if (!opts) {
12-
opts = {}
13-
}
14-
10+
function FastifyEnforceSchema (fastify, opts = {}, done) {
1511
if (!Object.prototype.hasOwnProperty.call(opts, 'required')) {
1612
opts.required = []
1713
}
@@ -59,7 +55,7 @@ function FastifyEnforceSchema (fastify, opts, done) {
5955
const schema = Object.keys(routeOptions?.schema?.response || [])
6056

6157
if (!routeOptions?.schema?.response) {
62-
throw new Error(getErrrorMessage(SCHEMA_TYPES.response, routeOptions))
58+
throw new Error(getErrorMessage(SCHEMA_TYPES.response, routeOptions))
6359
}
6460

6561
if (
@@ -88,17 +84,17 @@ function FastifyEnforceSchema (fastify, opts, done) {
8884
required.indexOf(SCHEMA_TYPES.body) !== -1 &&
8985
!hasProperties(routeOptions, SCHEMA_TYPES.body)
9086
) {
91-
throw new Error(getErrrorMessage(SCHEMA_TYPES.body, routeOptions))
87+
throw new Error(getErrorMessage(SCHEMA_TYPES.body, routeOptions))
9288
}
9389

9490
if (
9591
routeOptions?.schema?.params !== false &&
96-
new RegExp(/:\w+/).test(routeOptions.url) &&
92+
/:\w+/.test(routeOptions.url) &&
9793
!isSchemaTypeExcluded(excludedEntity, SCHEMA_TYPES.params) &&
9894
required.indexOf(SCHEMA_TYPES.params) !== -1 &&
9995
!hasProperties(routeOptions, SCHEMA_TYPES.params)
10096
) {
101-
throw new Error(getErrrorMessage(SCHEMA_TYPES.params, routeOptions))
97+
throw new Error(getErrorMessage(SCHEMA_TYPES.params, routeOptions))
10298
}
10399
})
104100

test/enforce-schema.test.js

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,47 @@ const { test } = require('tap')
44
const Fastify = require('fastify')
55
const enforceSchema = require('../index.js')
66
const {
7-
getErrrorMessage,
7+
getErrorMessage,
88
hasProperties,
99
isSchemaTypeExcluded
1010
} = require('../utils.js')
1111

12+
test('Should fail if no options passed: every endpoint should have a schema', async (t) => {
13+
t.plan(1)
14+
15+
const fastify = Fastify()
16+
17+
await fastify.register(enforceSchema)
18+
19+
try {
20+
fastify.post('/foo', {}, (req, reply) => {
21+
reply.code(201).send('ok')
22+
})
23+
} catch (error) {
24+
t.equal(error.message, 'schema missing at the path POST: "/foo"')
25+
}
26+
})
27+
28+
test('Should pass if no options passed and schema validation explicitly disabled', async (t) => {
29+
t.plan(2)
30+
31+
const fastify = Fastify()
32+
33+
await fastify.register(enforceSchema)
34+
35+
fastify.post('/foo', { schema: false }, (req, reply) => {
36+
reply.code(201).send('ok')
37+
})
38+
39+
const res = await fastify.inject({
40+
method: 'POST',
41+
url: '/foo'
42+
})
43+
44+
t.equal(res.statusCode, 201)
45+
t.equal(res.payload, 'ok')
46+
})
47+
1248
test('Should fail if schema is missing', async (t) => {
1349
t.plan(1)
1450

@@ -132,7 +168,6 @@ test('Should NOT fail if params schema is missing', async (t) => {
132168

133169
await fastify.register(enforceSchema, { required: ['params'] })
134170

135-
136171
fastify.get('/foo', { schema: {} }, (req, reply) => {
137172
reply.code(200).send('ok')
138173
})
@@ -166,15 +201,15 @@ test('getErrorMessage should return a proper message', async (t) => {
166201
t.plan(3)
167202

168203
t.equal(
169-
getErrrorMessage('body', { path: '/bar', method: 'PUT' }),
204+
getErrorMessage('body', { path: '/bar', method: 'PUT' }),
170205
'PUT: /bar is missing a body schema'
171206
)
172207
t.equal(
173-
getErrrorMessage('response', { path: '/bar', method: 'PUT' }),
208+
getErrorMessage('response', { path: '/bar', method: 'PUT' }),
174209
'PUT: /bar is missing a response schema'
175210
)
176211
t.equal(
177-
getErrrorMessage('params', { path: '/bar', method: 'PUT' }),
212+
getErrorMessage('params', { path: '/bar', method: 'PUT' }),
178213
'PUT: /bar is missing a params schema'
179214
)
180215
})

utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const getErrrorMessage = (schemaType, routeOptions) => {
1+
const getErrorMessage = (schemaType, routeOptions) => {
22
const { path, method } = routeOptions
33
return `${method}: ${path} is missing a ${schemaType} schema`
44
}
55

6-
exports.getErrrorMessage = getErrrorMessage
6+
exports.getErrorMessage = getErrorMessage
77

88
exports.hasProperties = (routeOptions, name) => {
99
return !!Object.keys(routeOptions?.schema?.[name]?.properties || []).length

0 commit comments

Comments
 (0)