@sn1006/common 1.0.18 → 1.0.19

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.
@@ -5,5 +5,5 @@ export declare abstract class NormalizedError extends Error {
5
5
  }
6
6
  export interface SerializedError {
7
7
  message: string;
8
- field?: string;
8
+ path?: (string | number)[];
9
9
  }
@@ -1,7 +1,7 @@
1
1
  import { NormalizedError } from './normalized-error';
2
2
  interface NonSerializedValidationError {
3
- msg: string;
4
- path: string;
3
+ message: string;
4
+ path: (string | number)[];
5
5
  }
6
6
  declare class CustomValidationError extends NormalizedError {
7
7
  errors: NonSerializedValidationError[];
@@ -9,7 +9,7 @@ declare class CustomValidationError extends NormalizedError {
9
9
  constructor(errors: NonSerializedValidationError[]);
10
10
  serializeErrors(): {
11
11
  message: string;
12
- field: string;
12
+ path: (string | number)[];
13
13
  }[];
14
14
  }
15
15
  export default CustomValidationError;
@@ -10,7 +10,7 @@ class CustomValidationError extends normalized_error_1.NormalizedError {
10
10
  }
11
11
  serializeErrors() {
12
12
  return this.errors.map(err => {
13
- return { message: err.msg, field: err.path };
13
+ return { message: err.message, path: err.path };
14
14
  });
15
15
  }
16
16
  }
@@ -10,7 +10,12 @@ function joiValidateRequest(schema, options) {
10
10
  const { error } = schema.validate(req, options);
11
11
  if (error) {
12
12
  // normalized the error to be similar to express-validator error
13
- const errors = error.details.map((err) => (Object.assign(Object.assign({}, err), { msg: err.message, path: err.path.join(".") })));
13
+ // const errors = error.details.map((err) => ({
14
+ // ...err,
15
+ // msg: err.message,
16
+ // path: err.path.join("."),
17
+ // }));
18
+ const errors = error.details;
14
19
  return next(new validation_error_1.default(errors));
15
20
  }
16
21
  return next();
@@ -10,7 +10,11 @@ const validateRequest = (req, res, next) => {
10
10
  const error = (0, express_validator_1.validationResult)(req);
11
11
  if (!error.isEmpty()) {
12
12
  const errors = error.array();
13
- return next(new validation_error_1.default(errors));
13
+ const cleanedErrors = errors.map((err) => ({
14
+ path: err.path.split("."),
15
+ message: err.msg
16
+ }));
17
+ return next(new validation_error_1.default(cleanedErrors));
14
18
  }
15
19
  next();
16
20
  };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const validation_error_1 = __importDefault(require("../errors/validation-error"));
16
+ function zodValidateRequest(schema) {
17
+ return (req, res, next) => __awaiter(this, void 0, void 0, function* () {
18
+ const result = schema.safeParse(req.body);
19
+ if (!result.success) {
20
+ const errors = result.error.issues;
21
+ return next(new validation_error_1.default(errors));
22
+ }
23
+ return next();
24
+ });
25
+ }
@@ -0,0 +1,18 @@
1
+ import { z } from "zod";
2
+ declare const filterSchema: z.ZodObject<{
3
+ name: z.ZodString;
4
+ description: z.ZodString;
5
+ keyword: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
6
+ active: z.ZodBoolean;
7
+ }, "strip", z.ZodTypeAny, {
8
+ name: string;
9
+ description: string;
10
+ active: boolean;
11
+ keyword?: string[] | undefined;
12
+ }, {
13
+ name: string;
14
+ description: string;
15
+ active: boolean;
16
+ keyword?: string[] | undefined;
17
+ }>;
18
+ export { filterSchema };
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.filterSchema = void 0;
4
+ const zod_1 = require("zod");
5
+ const filterSchema = zod_1.z.object({
6
+ name: zod_1.z.string().min(3).max(30),
7
+ description: zod_1.z.string().min(10).max(320),
8
+ keyword: zod_1.z.string().array().optional(),
9
+ active: zod_1.z.boolean()
10
+ });
11
+ exports.filterSchema = filterSchema;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sn1006/common",
3
- "version": "1.0.18",
3
+ "version": "1.0.19",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -27,6 +27,7 @@
27
27
  "express": "^4.18.2",
28
28
  "express-validator": "^7.0.1",
29
29
  "joi": "^17.12.1",
30
- "jsonwebtoken": "^9.0.1"
30
+ "jsonwebtoken": "^9.0.1",
31
+ "zod": "^3.22.4"
31
32
  }
32
33
  }