fastify-txstate 3.2.15 → 3.3.0

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.
package/lib/error.d.ts CHANGED
@@ -1,16 +1,24 @@
1
- import { type FastifySchemaValidationError } from 'fastify/types/schema';
1
+ import type { ValidationMessage } from '@txstate-mws/fastify-shared';
2
+ import type { FastifySchemaValidationError } from 'fastify/types/schema';
2
3
  export declare class HttpError extends Error {
3
4
  statusCode: number;
4
5
  constructor(statusCode: number, message?: string);
5
6
  }
6
- type ValidationErrors = Record<string, string[]>;
7
7
  export declare class FailedValidationError extends HttpError {
8
- errors: ValidationErrors;
9
- constructor(errors: ValidationErrors);
8
+ errors: Record<string, string[]>;
9
+ constructor(errors: Record<string, string[]>);
10
+ }
11
+ export declare class ValidationError extends HttpError {
12
+ path?: string | undefined;
13
+ type?: "error" | "success" | "warning" | "system" | undefined;
14
+ constructor(message: string, path?: string | undefined, type?: "error" | "success" | "warning" | "system" | undefined);
15
+ }
16
+ export declare class ValidationErrors extends HttpError {
17
+ errors: ValidationMessage[];
18
+ constructor(errors: ValidationMessage[]);
10
19
  }
11
20
  export declare function fstValidationToMessage(v: FastifySchemaValidationError): {
12
21
  message: string | undefined;
13
22
  path: string;
14
23
  type: string;
15
24
  };
16
- export {};
package/lib/error.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fstValidationToMessage = exports.FailedValidationError = exports.HttpError = void 0;
3
+ exports.fstValidationToMessage = exports.ValidationErrors = exports.ValidationError = exports.FailedValidationError = exports.HttpError = void 0;
4
4
  const http_status_codes_1 = require("http-status-codes");
5
5
  class HttpError extends Error {
6
6
  statusCode;
@@ -23,9 +23,28 @@ class FailedValidationError extends HttpError {
23
23
  constructor(errors) {
24
24
  super(422, 'Validation failure.');
25
25
  this.errors = errors;
26
+ this.errors = errors;
26
27
  }
27
28
  }
28
29
  exports.FailedValidationError = FailedValidationError;
30
+ class ValidationError extends HttpError {
31
+ path;
32
+ type;
33
+ constructor(message, path, type) {
34
+ super(422, message);
35
+ this.path = path;
36
+ this.type = type;
37
+ }
38
+ }
39
+ exports.ValidationError = ValidationError;
40
+ class ValidationErrors extends HttpError {
41
+ errors;
42
+ constructor(errors) {
43
+ super(422, errors[0]?.message);
44
+ this.errors = errors;
45
+ }
46
+ }
47
+ exports.ValidationErrors = ValidationErrors;
29
48
  function fstValidationToMessage(v) {
30
49
  const instancePath = v.keyword === 'required' ? v.instancePath + '/' + v.params.missingProperty : v.instancePath;
31
50
  return { message: v.message, path: instancePath.substring(1).replace(/\//g, '.'), type: 'error' };
package/lib/index.js CHANGED
@@ -147,7 +147,7 @@ class Server {
147
147
  * undefined before we validate.
148
148
  */
149
149
  if (schema != null)
150
- (0, txstate_utils_1.destroyNulls)(data);
150
+ (0, txstate_utils_1.destroyNulls)((0, txstate_utils_1.stringifyDates)(data));
151
151
  if (!validate(data))
152
152
  throw new Error('Output validation failed. ' + validate.errors?.[0].instancePath + ': ' + validate.errors?.[0].message);
153
153
  return JSON.stringify(data);
@@ -241,6 +241,12 @@ class Server {
241
241
  if (err instanceof error_1.FailedValidationError) {
242
242
  await res.status(err.statusCode).send(err.errors);
243
243
  }
244
+ else if (err instanceof error_1.ValidationError) {
245
+ await res.status(err.statusCode).send({ success: false, messages: [{ message: err.message, path: err.path, type: err.type ?? 'error' }] });
246
+ }
247
+ else if (err instanceof error_1.ValidationErrors) {
248
+ await res.status(err.statusCode).send({ success: false, messages: err.errors });
249
+ }
244
250
  else if (err instanceof error_1.HttpError) {
245
251
  await res.status(err.statusCode).send(err.message);
246
252
  }
package/lib-esm/index.js CHANGED
@@ -4,6 +4,8 @@ export const devLogger = ftxst.devLogger
4
4
  export const prodLogger = ftxst.prodLogger
5
5
  export const HttpError = ftxst.HttpError
6
6
  export const FailedValidationError = ftxst.FailedValidationError
7
+ export const ValidationError = ftxst.ValidationError
8
+ export const ValidationErrors = ftxst.ValidationErrors
7
9
  export const unifiedAuthenticate = ftxst.unifiedAuthenticate
8
10
  export const unifiedAuthenticateAll = ftxst.unifiedAuthenticateAll
9
11
  export const analyticsPlugin = ftxst.analyticsPlugin
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify-txstate",
3
- "version": "3.2.15",
3
+ "version": "3.3.0",
4
4
  "description": "A small wrapper for fastify providing a set of common conventions & utility functions we use.",
5
5
  "exports": {
6
6
  ".": {
@@ -30,7 +30,7 @@
30
30
  "fastify-plugin": "^4.5.1",
31
31
  "http-status-codes": "^2.1.4",
32
32
  "jose": "^5.2.3",
33
- "txstate-utils": "^1.9.1",
33
+ "txstate-utils": "^1.9.5",
34
34
  "ua-parser-js": "^1.0.37"
35
35
  },
36
36
  "devDependencies": {