express-generator-typescript 2.5.17 → 2.5.18

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.
@@ -26,7 +26,7 @@
26
26
  "jet-env": "^1.1.4",
27
27
  "jet-logger": "^2.0.1",
28
28
  "jet-paths": "^1.1.0",
29
- "jet-validators": "^1.2.1",
29
+ "jet-validators": "^1.3.3",
30
30
  "jsonfile": "^6.1.0",
31
31
  "module-alias": "^2.2.2",
32
32
  "morgan": "^1.10.0"
@@ -1,5 +1,7 @@
1
1
  import { Response } from 'supertest';
2
2
  import UserRepo from '@src/repos/UserRepo';
3
+ import { IParseObjectError, parseJson } from 'jet-validators/utils';
4
+ import { isString } from 'jet-validators';
3
5
 
4
6
 
5
7
  /******************************************************************************
@@ -16,6 +18,11 @@ interface IErrObj {
16
18
  [key: string]: unknown;
17
19
  }
18
20
 
21
+ interface IValidationErr {
22
+ message: string;
23
+ errors: IParseObjectError[];
24
+ }
25
+
19
26
 
20
27
  /******************************************************************************
21
28
  Functions
@@ -29,3 +36,13 @@ export async function cleanDatabase(): Promise<void> {
29
36
  UserRepo.deleteAllUsers(),
30
37
  ]);
31
38
  }
39
+
40
+ /**
41
+ * JSON parse a validation error.
42
+ */
43
+ export function parseValidationErr(arg: unknown): IValidationErr {
44
+ if (!isString(arg)) {
45
+ throw new Error('Not a string');
46
+ }
47
+ return parseJson<IValidationErr>(arg);
48
+ }
@@ -1,18 +1,18 @@
1
1
  import supertest from 'supertest';
2
2
  import insertUrlParams from 'inserturlparams';
3
- import { isObject } from 'jet-validators';
4
- import { parseJson, customDeepCompare } from 'jet-validators/utils';
3
+ import { customDeepCompare } from 'jet-validators/utils';
5
4
 
6
5
  import app from '@src/server';
7
6
 
8
7
  import UserRepo from '@src/repos/UserRepo';
9
8
  import User, { IUser } from '@src/models/User';
10
9
  import { USER_NOT_FOUND_ERR } from '@src/services/UserService';
11
- import { IParseRequestError } from '@src/routes/common';
10
+
12
11
  import HttpStatusCodes from '@src/common/HttpStatusCodes';
12
+ import { ValidationError } from '@src/common/route-errors';
13
13
 
14
14
  import Paths from 'spec/support/Paths';
15
- import { cleanDatabase, TRes } from 'spec/support';
15
+ import { cleanDatabase, parseValidationErr, TRes } from 'spec/support';
16
16
 
17
17
 
18
18
  /******************************************************************************
@@ -92,10 +92,9 @@ describe('UserRouter', () => {
92
92
  .send({ user: null })
93
93
  .end((_, res: TRes) => {
94
94
  expect(res.status).toBe(HttpStatusCodes.BAD_REQUEST);
95
- const errorObj = parseJson<IParseRequestError>(res.body.error),
96
- errItem = errorObj.errors[0];
97
- expect(errorObj.message).toBe('Parse request validation failed');
98
- expect(isObject(errItem) && errItem.prop === 'user').toBeTruthy();
95
+ const errorObj = parseValidationErr(res.body.error);
96
+ expect(errorObj.message).toBe(ValidationError.MESSAGE);
97
+ expect(errorObj.errors[0].prop).toBe('user');
99
98
  done();
100
99
  });
101
100
  });
@@ -118,19 +117,21 @@ describe('UserRouter', () => {
118
117
  });
119
118
  });
120
119
 
121
- // Param missing
120
+ // Id is the wrong data type
122
121
  it('should return a JSON object with an error message and a status code ' +
123
122
  `of "${HttpStatusCodes.BAD_REQUEST}" if the user param was missing`,
124
123
  done => {
124
+ const user = User.new();
125
+ user.id = ('5' as unknown as number);
125
126
  agent
126
127
  .put(Paths.Users.Update)
127
- .send({ user: null })
128
+ .send({ user })
128
129
  .end((_, res: TRes) => {
129
130
  expect(res.status).toBe(HttpStatusCodes.BAD_REQUEST);
130
- const errorObj = parseJson<IParseRequestError>(res.body.error),
131
- errItem = errorObj.errors[0];
132
- expect(errorObj.message).toBe('Parse request validation failed');
133
- expect(isObject(errItem) && errItem.prop === 'user').toBeTruthy();
131
+ const errorObj = parseValidationErr(res.body.error);
132
+ expect(errorObj.message).toBe(ValidationError.MESSAGE);
133
+ expect(errorObj.errors[0].prop).toBe('user');
134
+ expect(errorObj.errors[0].children?.[0].prop).toBe('id');
134
135
  done();
135
136
  });
136
137
  });
@@ -1,8 +1,10 @@
1
+ import { IParseObjectError } from 'jet-validators/utils';
2
+
1
3
  import HttpStatusCodes from '@src/common/HttpStatusCodes';
2
4
 
3
5
 
4
6
  /******************************************************************************
5
- Classes
7
+ Classes
6
8
  ******************************************************************************/
7
9
 
8
10
  /**
@@ -17,3 +19,19 @@ export class RouteError extends Error {
17
19
  }
18
20
  }
19
21
 
22
+ /**
23
+ * Handle "parseObj" errors.
24
+ */
25
+ export class ValidationError extends RouteError {
26
+
27
+ public static MESSAGE = 'The parseObj() function discovered one or ' +
28
+ 'more errors.';
29
+
30
+ public constructor(errors: IParseObjectError[]) {
31
+ const msg = JSON.stringify({
32
+ message: ValidationError.MESSAGE,
33
+ errors,
34
+ });
35
+ super(HttpStatusCodes.BAD_REQUEST, msg);
36
+ }
37
+ }
@@ -1,7 +1,7 @@
1
1
  import { isString } from 'jet-validators';
2
- import { parseObjectPlus, TParser } from 'jet-validators/utils';
2
+ import { parseObject, TParseOnError } from 'jet-validators/utils';
3
3
 
4
- import { isRelationalKey, tranIsDate } from '@src/util/validators';
4
+ import { isRelationalKey, transIsDate } from '@src/util/validators';
5
5
 
6
6
 
7
7
  /******************************************************************************
@@ -35,26 +35,26 @@ export interface IUser {
35
35
  /**
36
36
  * New user object.
37
37
  */
38
- function newUser(user: Partial<IUser>): IUser {
38
+ function newUser(user?: Partial<IUser>): IUser {
39
39
  const retVal = { ...DEFAULT_USER_VALS(), ...user };
40
40
  return parseUser(retVal);
41
41
  }
42
42
 
43
43
  /**
44
- * Validate user object but return type predicate.
44
+ * Check is a user object.
45
45
  */
46
- function testIsUser(arg: unknown): arg is IUser {
47
- return !!parseUser(arg);
46
+ function testUser(arg: unknown, errCb?: TParseOnError): arg is IUser {
47
+ return !!parseUser(arg, errCb);
48
48
  }
49
49
 
50
50
  /**
51
51
  * Validate a user object.
52
52
  */
53
- const parseUser: TParser<IUser> = parseObjectPlus({
53
+ const parseUser = parseObject<IUser>({
54
54
  id: isRelationalKey,
55
55
  name: isString,
56
56
  email: isString,
57
- created: tranIsDate,
57
+ created: transIsDate,
58
58
  });
59
59
 
60
60
 
@@ -64,6 +64,5 @@ const parseUser: TParser<IUser> = parseObjectPlus({
64
64
 
65
65
  export default {
66
66
  new: newUser,
67
- parse: parseUser,
68
- test: testIsUser,
67
+ test: testUser,
69
68
  } as const;
@@ -1 +1 @@
1
- {"users":[{"id":39386845760,"name":"Bill","created":"2025-03-09T02:38:03.021Z","email":"sean.maxwell@gmail.com"},{"id":232226940074,"name":"John Smith","created":"2025-03-09T02:38:03.021Z","email":"john.smith@gmail.com"},{"id":847602476180,"name":"Gordan Freeman","created":"2025-03-09T02:38:03.021Z","email":"gordan.freeman@gmail.com"}]}
1
+ {"users":[{"id":937353914582,"name":"Bill","created":"2025-03-10T08:02:45.482Z","email":"sean.maxwell@gmail.com"},{"id":564064331076,"name":"John Smith","created":"2025-03-10T08:02:45.482Z","email":"john.smith@gmail.com"},{"id":491773368070,"name":"Gordan Freeman","created":"2025-03-10T08:02:45.482Z","email":"gordan.freeman@gmail.com"}]}
@@ -1,54 +1,28 @@
1
1
  import { Response, Request } from 'express';
2
+ import { parseObject, TSchema } from 'jet-validators/utils';
2
3
 
3
- import {
4
- ParseObjectError,
5
- TParseErrorItem,
6
- TSchema,
7
- parseObjectPlus,
8
- } from 'jet-validators/utils';
9
-
10
- import HttpStatusCodes from '@src/common/HttpStatusCodes';
11
- import { RouteError } from '@src/common/route-errors';
4
+ import { ValidationError } from '@src/common/route-errors';
12
5
 
13
6
 
14
7
  /******************************************************************************
15
- Types
8
+ Functions
16
9
  ******************************************************************************/
17
10
 
18
- type TRecord = Record<string, unknown>;
19
- export type IReq = Request<TRecord, void, TRecord, TRecord>;
20
- export type IRes = Response<unknown, TRecord>;
21
-
22
- export interface IParseRequestError {
23
- message: 'Parse request validation failed';
24
- errors: TParseErrorItem[];
11
+ /**
12
+ * Throw a "ParseObjError" when "parseObject" fails. Also extract a nested
13
+ * "ParseObjError" and add it to the nestedErrors array.
14
+ */
15
+ export function parseReq<U extends TSchema>(schema: U) {
16
+ return parseObject(schema, errors => {
17
+ throw new ValidationError(errors);
18
+ });
25
19
  }
26
20
 
27
21
 
28
22
  /******************************************************************************
29
- Functions
23
+ Types
30
24
  ******************************************************************************/
31
25
 
32
- /**
33
- * Parse an incoming route request object.
34
- */
35
- export function parseReq<U extends TSchema>(schema: U) {
36
- const parse = parseObjectPlus(schema);
37
- return (arg: unknown) => {
38
- let retVal;
39
- try {
40
- retVal = parse(arg);
41
- } catch (err) {
42
- if (err instanceof ParseObjectError) {
43
- const errObj: IParseRequestError = {
44
- message: 'Parse request validation failed',
45
- errors: err.getErrors(),
46
- };
47
- const msg = JSON.stringify(errObj);
48
- throw new RouteError(HttpStatusCodes.BAD_REQUEST, msg);
49
- }
50
- throw err;
51
- }
52
- return retVal;
53
- };
54
- }
26
+ type TRecord = Record<string, unknown>;
27
+ export type IReq = Request<TRecord, void, TRecord, TRecord>;
28
+ export type IRes = Response<unknown, TRecord>;
@@ -16,7 +16,7 @@ export function isRelationalKey(arg: unknown): arg is number {
16
16
  /**
17
17
  * Convert to date object then check is a validate date.
18
18
  */
19
- export const tranIsDate = transform(
19
+ export const transIsDate = transform(
20
20
  arg => new Date(arg as string),
21
21
  arg => isDate(arg),
22
22
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-generator-typescript",
3
- "version": "2.5.17",
3
+ "version": "2.5.18",
4
4
  "description": "Generate new Express applications similar to express-generate which but sets it up to use TypeScript instead",
5
5
  "scripts": {
6
6
  "express-generator-typescript": "node bin/cli.js",