|
1 | | -const fp = require("fastify-plugin"); |
| 1 | +const fp = require('fastify-plugin') |
2 | 2 | const { |
3 | 3 | getErrrorMessage, |
4 | 4 | hasProperties, |
5 | 5 | initialExcludes, |
6 | 6 | isSchemaTypeExcluded, |
7 | | - SCHEMA_TYPES, |
8 | | - isHTTPVerbExcluded, |
9 | | -} = require("./utils"); |
| 7 | + SCHEMA_TYPES |
| 8 | +} = require('./utils') |
10 | 9 |
|
11 | | -function FastifyEnforceSchema(fastify, opts, done) { |
| 10 | +function FastifyEnforceSchema (fastify, opts, done) { |
12 | 11 | if (!opts) { |
13 | | - opts = {}; |
| 12 | + opts = {} |
14 | 13 | } |
15 | 14 |
|
16 | | - if (!Object.prototype.hasOwnProperty.call(opts, "required")) { |
17 | | - opts.required = []; |
| 15 | + if (!Object.prototype.hasOwnProperty.call(opts, 'required')) { |
| 16 | + opts.required = [] |
18 | 17 | } |
19 | 18 |
|
20 | | - if (!Object.prototype.hasOwnProperty.call(opts, "exclude")) { |
21 | | - opts.exclude = []; |
| 19 | + if (!Object.prototype.hasOwnProperty.call(opts, 'exclude')) { |
| 20 | + opts.exclude = [] |
22 | 21 | } |
23 | 22 |
|
24 | | - if (!Object.prototype.hasOwnProperty.call(opts, 'excludeOnFalseSchema')) { |
25 | | - opts.excludeOnFalseSchema = false; |
26 | | - } |
27 | | - |
28 | | - const { required, exclude, excludeOnFalseSchema } = opts; |
| 23 | + const { required, exclude } = opts |
29 | 24 |
|
30 | | - fastify.addHook("onRoute", (routeOptions) => { |
| 25 | + fastify.addHook('onRoute', (routeOptions) => { |
31 | 26 | if ( |
32 | | - routeOptions.path === "*" || |
| 27 | + routeOptions.path === '*' || |
33 | 28 | !routeOptions.path || |
34 | | - (excludeOnFalseSchema && routeOptions.schema === false) |
| 29 | + routeOptions.schema === false |
35 | 30 | ) { |
36 | | - done(); |
37 | | - return; |
38 | | - } |
39 | | - |
40 | | - if (excludeOnFalseSchema && typeof routeOptions.schema === "object") { |
41 | | - const excludedEntity = exclude.find( |
42 | | - ({ url }) => url === routeOptions.path |
43 | | - ); |
44 | | - const excludedSchemas = []; |
45 | | - |
46 | | - Object.entries(routeOptions.schema).forEach(([key, value]) => { |
47 | | - if (value === false) { |
48 | | - excludedSchemas.push(key); |
49 | | - } |
50 | | - }); |
51 | | - |
52 | | - if (excludedEntity) { |
53 | | - excludedEntity.excludedSchemas = [ |
54 | | - ...new Set([...excludedEntity.excludedSchemas, ...excludedSchemas]), |
55 | | - ]; |
56 | | - } else { |
57 | | - exclude.push({ url: routeOptions.path, excludedSchemas }); |
58 | | - } |
| 31 | + done() |
| 32 | + return |
59 | 33 | } |
60 | 34 |
|
61 | 35 | const excludedEntity = [...initialExcludes, ...exclude].find( |
62 | 36 | ({ url }) => url === routeOptions.path |
63 | | - ); |
| 37 | + ) |
64 | 38 |
|
65 | 39 | const hasSchemas = |
66 | | - typeof excludedEntity === "object" && |
67 | | - Object.prototype.hasOwnProperty.call(excludedEntity, "excludedSchemas"); |
| 40 | + typeof excludedEntity === 'object' && |
| 41 | + Object.prototype.hasOwnProperty.call(excludedEntity, 'excludedSchemas') |
68 | 42 |
|
69 | 43 | if (excludedEntity && !hasSchemas) { |
70 | | - done(); |
71 | | - return; |
| 44 | + done() |
| 45 | + return |
72 | 46 | } |
73 | 47 |
|
74 | 48 | if (!routeOptions?.schema) { |
75 | 49 | throw new Error( |
76 | 50 | `schema missing at the path ${routeOptions.method}: "${routeOptions.path}"` |
77 | | - ); |
| 51 | + ) |
78 | 52 | } |
79 | 53 |
|
80 | 54 | if ( |
| 55 | + routeOptions?.schema?.response !== false && |
81 | 56 | !isSchemaTypeExcluded(excludedEntity, SCHEMA_TYPES.response) && |
82 | 57 | required.indexOf(SCHEMA_TYPES.response) !== -1 |
83 | 58 | ) { |
84 | | - const schema = Object.keys(routeOptions?.schema?.response || []); |
| 59 | + const schema = Object.keys(routeOptions?.schema?.response || []) |
85 | 60 |
|
86 | 61 | if (!routeOptions?.schema?.response) { |
87 | | - throw new Error(getErrrorMessage(SCHEMA_TYPES.response, routeOptions)); |
| 62 | + throw new Error(getErrrorMessage(SCHEMA_TYPES.response, routeOptions)) |
88 | 63 | } |
89 | 64 |
|
90 | 65 | if ( |
91 | 66 | routeOptions?.schema?.response && |
92 | 67 | !Object.keys(routeOptions?.schema?.response || []).length |
93 | 68 | ) { |
94 | | - throw new Error(`No HTTP status codes provided in the response schema`); |
| 69 | + throw new Error('No HTTP status codes provided in the response schema') |
95 | 70 | } |
96 | 71 |
|
97 | | - schema.forEach(value => { |
| 72 | + schema.forEach((value) => { |
98 | 73 | if (!Number.isInteger(parseInt(value, 10))) { |
99 | 74 | throw new Error( |
100 | 75 | `"${value}" is not a number. HTTP status codes from 100 - 599 supported` |
101 | | - ); |
| 76 | + ) |
102 | 77 | } |
103 | 78 |
|
104 | 79 | if (value < 100 || value > 599) { |
105 | | - throw new Error(`HTTP status codes from 100 - 599 supported`); |
| 80 | + throw new Error('HTTP status codes from 100 - 599 supported') |
106 | 81 | } |
107 | | - }); |
| 82 | + }) |
108 | 83 | } |
109 | 84 | if ( |
| 85 | + routeOptions?.schema?.body !== false && |
110 | 86 | !isSchemaTypeExcluded(excludedEntity, SCHEMA_TYPES.body) && |
111 | | - ["POST", "PUT", "PATCH"].includes(routeOptions.method) && |
| 87 | + ['POST', 'PUT', 'PATCH'].includes(routeOptions.method) && |
112 | 88 | required.indexOf(SCHEMA_TYPES.body) !== -1 && |
113 | 89 | !hasProperties(routeOptions, SCHEMA_TYPES.body) |
114 | 90 | ) { |
115 | | - throw new Error(getErrrorMessage(SCHEMA_TYPES.body, routeOptions)); |
| 91 | + throw new Error(getErrrorMessage(SCHEMA_TYPES.body, routeOptions)) |
116 | 92 | } |
117 | 93 |
|
118 | 94 | if ( |
| 95 | + routeOptions?.schema?.params !== false && |
| 96 | + new RegExp(/:\w+/).test(routeOptions.url) && |
119 | 97 | !isSchemaTypeExcluded(excludedEntity, SCHEMA_TYPES.params) && |
120 | 98 | required.indexOf(SCHEMA_TYPES.params) !== -1 && |
121 | 99 | !hasProperties(routeOptions, SCHEMA_TYPES.params) |
122 | 100 | ) { |
123 | | - throw new Error(getErrrorMessage(SCHEMA_TYPES.params, routeOptions)); |
| 101 | + throw new Error(getErrrorMessage(SCHEMA_TYPES.params, routeOptions)) |
124 | 102 | } |
125 | | - }); |
| 103 | + }) |
126 | 104 |
|
127 | | - done(); |
| 105 | + done() |
128 | 106 | } |
129 | 107 |
|
130 | 108 | const _fastifyEnforceSchema = fp(FastifyEnforceSchema, { |
131 | | - fastify: "4.x", |
132 | | - name: "fastify-enforce-schema", |
133 | | -}); |
| 109 | + fastify: '4.x', |
| 110 | + name: 'fastify-enforce-schema' |
| 111 | +}) |
134 | 112 |
|
135 | | -module.exports = _fastifyEnforceSchema; |
136 | | -module.exports.FastifyEnforceSchema = _fastifyEnforceSchema; |
137 | | -module.exports.default = _fastifyEnforceSchema; |
| 113 | +module.exports = _fastifyEnforceSchema |
| 114 | +module.exports.FastifyEnforceSchema = _fastifyEnforceSchema |
| 115 | +module.exports.default = _fastifyEnforceSchema |
0 commit comments