fastify-txstate 3.2.13 → 3.2.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/index.js +31 -11
  2. package/package.json +2 -2
package/lib/index.js CHANGED
@@ -18,6 +18,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
20
  exports.prodLogger = exports.devLogger = void 0;
21
+ const ajv_1 = __importDefault(require("ajv"));
21
22
  const swagger_1 = __importDefault(require("@fastify/swagger"));
22
23
  const swagger_ui_1 = __importDefault(require("@fastify/swagger-ui"));
23
24
  const fastify_shared_1 = require("@txstate-mws/fastify-shared");
@@ -117,21 +118,40 @@ class Server {
117
118
  if (missingResponse) {
118
119
  newSchema.response['200'] = {
119
120
  description: 'Success. Return type has not been specified.',
120
- type: 'null'
121
+ type: 'object'
121
122
  };
122
123
  }
123
124
  route.schema = newSchema;
124
125
  });
125
- /**
126
- * Fastify validates response schema while serializing the response
127
- *
128
- * This is great, but because Ajv treats optional properties as non-nullable, optional
129
- * properties that are set to `null` instead of `undefined` will fail validation (or with `coerceTypes`,
130
- * be converted to empty string or 0 or false). This is silly behavior, so we're adding a hook to
131
- * convert all nulls to undefined before fastify validates.
132
- */
133
- this.app.addHook('preSerialization', async (req, res, payload) => {
134
- return req.routeOptions.schema?.response ? (0, txstate_utils_1.destroyNulls)(payload) : payload;
126
+ this.app.addHook('preValidation', (req, res, done) => {
127
+ if (req.body != null && req.routeOptions.schema?.body)
128
+ (0, txstate_utils_1.destroyNulls)(req.body);
129
+ done();
130
+ });
131
+ // use Ajv to validate responses instead of @fastify/json-fast-stringify since ajv does
132
+ // a better job with recursive types and we don't want to have different behavior between
133
+ // input and output validation
134
+ const ajv = new ajv_1.default(config.ajv.customOptions);
135
+ for (const pluginConfig of config.ajv.plugins ?? []) {
136
+ const [plugin, opts] = (0, txstate_utils_1.toArray)(pluginConfig);
137
+ plugin(ajv, opts);
138
+ }
139
+ this.app.setSerializerCompiler((route) => {
140
+ const schema = route.schema;
141
+ const validate = schema == null ? ajv.compile({ type: 'object' }) : ajv.compile(schema);
142
+ return data => {
143
+ /**
144
+ * Ajv unfortunately treats optional properties as non-nullable, so they're allowed to
145
+ * be undefined but not allowed to be null. Worse, with `coerceTypes`, null will be converted
146
+ * to empty string or 0 or false. This is silly behavior, so we're converting all nulls to
147
+ * undefined before we validate.
148
+ */
149
+ if (schema != null)
150
+ (0, txstate_utils_1.destroyNulls)(data);
151
+ if (!validate(data))
152
+ throw new Error('Output validation failed: ' + validate.errors?.[0].message);
153
+ return JSON.stringify(data);
154
+ };
135
155
  });
136
156
  if (!config.skipOriginCheck && !process.env.SKIP_ORIGIN_CHECK) {
137
157
  this.setValidOrigins([...(config.validOrigins ?? []), ...(process.env.VALID_ORIGINS?.split(',') ?? [])]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify-txstate",
3
- "version": "3.2.13",
3
+ "version": "3.2.14",
4
4
  "description": "A small wrapper for fastify providing a set of common conventions & utility functions we use.",
5
5
  "exports": {
6
6
  ".": {
@@ -18,7 +18,7 @@
18
18
  "testserver": "node -r ts-node/register --no-warnings testserver/index.ts"
19
19
  },
20
20
  "dependencies": {
21
- "@elastic/elasticsearch": "^8.12.2",
21
+ "@elastic/elasticsearch": "^8.13.0",
22
22
  "@fastify/swagger": "^8.14.0",
23
23
  "@fastify/swagger-ui": "^3.0.0",
24
24
  "@fastify/type-provider-json-schema-to-ts": "^3.0.0",