fastify-txstate 3.2.7 → 3.2.9

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 +11 -0
  2. package/package.json +2 -2
package/lib/index.js CHANGED
@@ -121,6 +121,17 @@ class Server {
121
121
  }
122
122
  route.schema = newSchema;
123
123
  });
124
+ /**
125
+ * Fastify validates response schema while serializing the response
126
+ *
127
+ * This is great, but because Ajv treats optional properties as non-nullable, optional
128
+ * properties that are set to `null` instead of `undefined` will fail validation (or with `coerceTypes`,
129
+ * be converted to empty string or 0 or false). This is silly behavior, so we're adding a hook to
130
+ * convert all nulls to undefined before fastify validates.
131
+ */
132
+ this.app.addHook('preSerialization', async (req, res, payload) => {
133
+ return req.routeSchema.response ? (0, txstate_utils_1.destroyNulls)(payload) : payload;
134
+ });
124
135
  if (!config.skipOriginCheck && !process.env.SKIP_ORIGIN_CHECK) {
125
136
  this.setValidOrigins([...((_d = config.validOrigins) !== null && _d !== void 0 ? _d : []), ...((_f = (_e = process.env.VALID_ORIGINS) === null || _e === void 0 ? void 0 : _e.split(',')) !== null && _f !== void 0 ? _f : [])]);
126
137
  this.setValidOriginHosts([...((_g = config.validOriginHosts) !== null && _g !== void 0 ? _g : []), ...((_j = (_h = process.env.VALID_ORIGIN_HOSTS) === null || _h === void 0 ? void 0 : _h.split(',')) !== null && _j !== void 0 ? _j : [])]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify-txstate",
3
- "version": "3.2.7",
3
+ "version": "3.2.9",
4
4
  "description": "A small wrapper for fastify providing a set of common conventions & utility functions we use.",
5
5
  "exports": {
6
6
  ".": {
@@ -29,7 +29,7 @@
29
29
  "fastify-plugin": "^4.5.1",
30
30
  "http-status-codes": "^2.1.4",
31
31
  "jose": "^5.2.3",
32
- "txstate-utils": "^1.8.15",
32
+ "txstate-utils": "^1.9.1",
33
33
  "ua-parser-js": "^1.0.37"
34
34
  },
35
35
  "devDependencies": {