pg-mvc-service 2.0.88 → 2.0.90
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.
|
@@ -4,30 +4,6 @@ class ReqResType {
|
|
|
4
4
|
constructor() {
|
|
5
5
|
this.properties = {};
|
|
6
6
|
}
|
|
7
|
-
/**
|
|
8
|
-
* Retrieve the property definition corresponding to the specified key path.
|
|
9
|
-
* 指定されたキーパスに対応するプロパティ定義を取得します。
|
|
10
|
-
* @param {Array<string | number>} keys - Access path to the property (array of strings or index numbers)
|
|
11
|
-
* プロパティへのアクセスパス(文字列またはインデックス番号の配列)
|
|
12
|
-
* @returns {BaseType} Property definition object
|
|
13
|
-
* プロパティ定義オブジェクト
|
|
14
|
-
*/
|
|
15
|
-
// protected getProperty(keys: Array<string | number>) {
|
|
16
|
-
// let property: any = this.properties;
|
|
17
|
-
// for (let i = 0;i < keys.length;i++) {
|
|
18
|
-
// const key = keys[i];
|
|
19
|
-
// if (typeof key === 'number') {
|
|
20
|
-
// property = property.properties;
|
|
21
|
-
// continue;
|
|
22
|
-
// }
|
|
23
|
-
// if (i === 0) {
|
|
24
|
-
// property = property[key];
|
|
25
|
-
// } else {
|
|
26
|
-
// property = property.properties[key];
|
|
27
|
-
// }
|
|
28
|
-
// }
|
|
29
|
-
// return property;
|
|
30
|
-
// }
|
|
31
7
|
/**
|
|
32
8
|
* Retrieve property type data
|
|
33
9
|
* プロパティ型のデータを取得
|
|
@@ -23,8 +23,11 @@ class RequestType extends ReqResType_1.default {
|
|
|
23
23
|
INVALID_OBJECT: '{property} must be of type Object. ({value})',
|
|
24
24
|
INVALID_ARRAY: '{property} must be of type Array. ({value})',
|
|
25
25
|
INVALID_NUMBER: '{property} must be of type number. ({value})',
|
|
26
|
+
INVALID_NUMBER_MAX: '{property} must be less than or equal to {max}. ({value})',
|
|
27
|
+
INVALID_NUMBER_MIN: '{property} must be greater than or equal to {min}. ({value})',
|
|
26
28
|
INVALID_BOOL: '{property} must be of type bool or a string with true, false, or a number with 0, 1. ({value})',
|
|
27
29
|
INVALID_STRING: '{property} must be of type string. ({value})',
|
|
30
|
+
INVALID_STRING_MAX_LENGTH: '{property} must be less than or equal to {maxLength} characters. ({value})',
|
|
28
31
|
INVALID_UUID: '{property} must be a UUID. ({value})',
|
|
29
32
|
INVALID_MAIL: '{property} must be an email. ({value})',
|
|
30
33
|
INVALID_HTTPS: '{property} must be an https or http URL. ({value})',
|
|
@@ -43,8 +46,11 @@ class RequestType extends ReqResType_1.default {
|
|
|
43
46
|
INVALID_OBJECT: '{property}はobject型で入力してください。({value})',
|
|
44
47
|
INVALID_ARRAY: '{property}はarray型で入力してください。({value})',
|
|
45
48
|
INVALID_NUMBER: '{property}はnumber型または半角数値のstring型で入力してください。({value})',
|
|
49
|
+
INVALID_NUMBER_MAX: '{property}は{max}以下の値で入力してください。({value})',
|
|
50
|
+
INVALID_NUMBER_MIN: '{property}は{min}以上の値で入力してください。({value})',
|
|
46
51
|
INVALID_BOOL: '{property}はboolean型またはtrue、falseのstring型または0、1のnumber型で入力してください。({value})',
|
|
47
52
|
INVALID_STRING: '{property}はstring型で入力してください。({value})',
|
|
53
|
+
INVALID_STRING_MAX_LENGTH: '{property}は{maxLength}文字以内で入力してください。({value})',
|
|
48
54
|
INVALID_UUID: '{property}はUUID形式のstring型で入力してください。({value})',
|
|
49
55
|
INVALID_MAIL: '{property}はメールアドレス形式のstring型で入力してください。({value})',
|
|
50
56
|
INVALID_HTTPS: '{property}はhttpsまたはhttpのURL形式のstring型で入力してください。({value})',
|
|
@@ -124,7 +130,7 @@ class RequestType extends ReqResType_1.default {
|
|
|
124
130
|
throw new Error(`${key} is not set in paramProperties.`);
|
|
125
131
|
}
|
|
126
132
|
const property = this.paramProperties[index];
|
|
127
|
-
this.params[key] = this.convertValue(property
|
|
133
|
+
this.params[key] = this.convertValue(property, value, [key, `(pathIndex: ${index})`], false);
|
|
128
134
|
}
|
|
129
135
|
}
|
|
130
136
|
this.params = (_a = request.params) !== null && _a !== void 0 ? _a : {};
|
|
@@ -140,7 +146,7 @@ class RequestType extends ReqResType_1.default {
|
|
|
140
146
|
* @returns {string} The generated error message. 生成されたエラーメッセージ
|
|
141
147
|
*/
|
|
142
148
|
throwInputError(code, keys, value) {
|
|
143
|
-
var _a;
|
|
149
|
+
var _a, _b, _c, _d;
|
|
144
150
|
const list = {
|
|
145
151
|
"REQUIRE_00": this.ERROR_MESSAGE.REQUIRED,
|
|
146
152
|
"REQUIRE_01": this.ERROR_MESSAGE.REQUIRED,
|
|
@@ -152,10 +158,13 @@ class RequestType extends ReqResType_1.default {
|
|
|
152
158
|
"ARRAY_11": this.ERROR_MESSAGE.INVALID_ARRAY,
|
|
153
159
|
"UNNECESSARY_11": this.ERROR_MESSAGE.UNNECESSARY,
|
|
154
160
|
"NUMBER_21": this.ERROR_MESSAGE.INVALID_NUMBER,
|
|
161
|
+
"NUMBER_22": this.ERROR_MESSAGE.INVALID_NUMBER_MIN,
|
|
162
|
+
"NUMBER_23": this.ERROR_MESSAGE.INVALID_NUMBER_MAX,
|
|
155
163
|
"BOOL_21": this.ERROR_MESSAGE.INVALID_BOOL,
|
|
156
164
|
"BOOL_22": this.ERROR_MESSAGE.INVALID_BOOL,
|
|
157
165
|
"BOOL_23": this.ERROR_MESSAGE.INVALID_BOOL,
|
|
158
166
|
"STRING_21": this.ERROR_MESSAGE.INVALID_STRING,
|
|
167
|
+
"STRING_22": this.ERROR_MESSAGE.INVALID_STRING_MAX_LENGTH,
|
|
159
168
|
"UUID_21": this.ERROR_MESSAGE.INVALID_UUID,
|
|
160
169
|
"MAIL_21": this.ERROR_MESSAGE.INVALID_MAIL,
|
|
161
170
|
"DATE_21": this.ERROR_MESSAGE.INVALID_DATE,
|
|
@@ -166,8 +175,6 @@ class RequestType extends ReqResType_1.default {
|
|
|
166
175
|
"HTTPS_21": this.ERROR_MESSAGE.INVALID_HTTPS,
|
|
167
176
|
"BASE64_21": this.ERROR_MESSAGE.INVALID_BASE64,
|
|
168
177
|
"REQUIRE_31": this.ERROR_MESSAGE.REQUIRED,
|
|
169
|
-
"NUMBER_31": this.ERROR_MESSAGE.INVALID_NUMBER,
|
|
170
|
-
"STRING_31": this.ERROR_MESSAGE.INVALID_STRING,
|
|
171
178
|
"ENUM_32": this.ERROR_MESSAGE.INVALID_ENUM,
|
|
172
179
|
"NUMBER_41": this.ERROR_MESSAGE.INVALID_NUMBER,
|
|
173
180
|
"STRING_41": this.ERROR_MESSAGE.INVALID_STRING,
|
|
@@ -189,10 +196,13 @@ class RequestType extends ReqResType_1.default {
|
|
|
189
196
|
"MAP_34": this.ERROR_MESSAGE.INVALID_MAP_BOOL,
|
|
190
197
|
"MAP_35": this.ERROR_MESSAGE.INVALID_MAP_BOOL,
|
|
191
198
|
"NUMBER_91": this.ERROR_MESSAGE.INVALID_NUMBER,
|
|
199
|
+
"NUMBER_92": this.ERROR_MESSAGE.INVALID_NUMBER_MIN,
|
|
200
|
+
"NUMBER_93": this.ERROR_MESSAGE.INVALID_NUMBER_MAX,
|
|
192
201
|
"BOOL_91": this.ERROR_MESSAGE.INVALID_BOOL,
|
|
193
202
|
"BOOL_92": this.ERROR_MESSAGE.INVALID_BOOL,
|
|
194
203
|
"BOOL_93": this.ERROR_MESSAGE.INVALID_BOOL,
|
|
195
204
|
"STRING_91": this.ERROR_MESSAGE.INVALID_STRING,
|
|
205
|
+
"STRING_92": this.ERROR_MESSAGE.INVALID_STRING_MAX_LENGTH,
|
|
196
206
|
"UUID_91": this.ERROR_MESSAGE.INVALID_UUID,
|
|
197
207
|
"MAIL_91": this.ERROR_MESSAGE.INVALID_MAIL,
|
|
198
208
|
"DATE_91": this.ERROR_MESSAGE.INVALID_DATE,
|
|
@@ -204,9 +214,21 @@ class RequestType extends ReqResType_1.default {
|
|
|
204
214
|
"BASE64_91": this.ERROR_MESSAGE.INVALID_BASE64,
|
|
205
215
|
};
|
|
206
216
|
let errorMessage = list[code];
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
217
|
+
const property = this.getProperty(keys);
|
|
218
|
+
switch (property.type) {
|
|
219
|
+
case 'enum':
|
|
220
|
+
case 'enum?':
|
|
221
|
+
errorMessage = errorMessage.replace('{enums}', Object.keys((_a = property.enums) !== null && _a !== void 0 ? _a : '').join(','));
|
|
222
|
+
break;
|
|
223
|
+
case 'string':
|
|
224
|
+
case 'string?':
|
|
225
|
+
errorMessage = errorMessage.replace('{maxLength}', Object.keys((_b = property.maxLength) !== null && _b !== void 0 ? _b : '').join(','));
|
|
226
|
+
break;
|
|
227
|
+
case 'number':
|
|
228
|
+
case 'number?':
|
|
229
|
+
errorMessage = errorMessage.replace('{max}', Object.keys((_c = property.max) !== null && _c !== void 0 ? _c : '').join(','));
|
|
230
|
+
errorMessage = errorMessage.replace('{min}', Object.keys((_d = property.min) !== null && _d !== void 0 ? _d : '').join(','));
|
|
231
|
+
break;
|
|
210
232
|
}
|
|
211
233
|
errorMessage = errorMessage.replace("{property}", keys.join('.')).replace("{value}", value);
|
|
212
234
|
throw new Exception_1.InputErrorException(code, errorMessage);
|
|
@@ -291,7 +313,43 @@ class RequestType extends ReqResType_1.default {
|
|
|
291
313
|
if (request.method === 'GET' || request.method === 'DELETE') {
|
|
292
314
|
// GET,DELETEメソッドの場合、?array=1&array=2で配列となるが、
|
|
293
315
|
// ?array=1のみで終わる場合は配列にならないため、直接配列にしている
|
|
294
|
-
|
|
316
|
+
const type = this.properties[key].item.type;
|
|
317
|
+
if (type === 'object' || type === 'object?' || type === 'array' || type === 'array?' || type === 'map' || type === 'map?') {
|
|
318
|
+
throw new Error("GETまたはDELETEメソッドでは配列型にobject, array, mapを使用することはできません。");
|
|
319
|
+
}
|
|
320
|
+
if (type === 'enum' || type === 'enum?') {
|
|
321
|
+
const tempProp = {
|
|
322
|
+
type: type,
|
|
323
|
+
description: this.properties[key].item.description,
|
|
324
|
+
enumType: this.properties[key].item.enumType,
|
|
325
|
+
enums: this.properties[key].item.enums,
|
|
326
|
+
};
|
|
327
|
+
this.data[key] = [this.convertValue(tempProp, value, [key, 0], true)];
|
|
328
|
+
}
|
|
329
|
+
else if (type === 'string' || type === 'string?') {
|
|
330
|
+
const tempProp = {
|
|
331
|
+
type: type,
|
|
332
|
+
description: this.properties[key].item.description,
|
|
333
|
+
maxLength: this.properties[key].item.maxLength,
|
|
334
|
+
};
|
|
335
|
+
this.data[key] = [this.convertValue(tempProp, value, [key, 0], true)];
|
|
336
|
+
}
|
|
337
|
+
else if (type === 'number' || type === 'number?') {
|
|
338
|
+
const tempProp = {
|
|
339
|
+
type: type,
|
|
340
|
+
description: this.properties[key].item.description,
|
|
341
|
+
max: this.properties[key].item.max,
|
|
342
|
+
min: this.properties[key].item.min,
|
|
343
|
+
};
|
|
344
|
+
this.data[key] = [this.convertValue(tempProp, value, [key, 0], true)];
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
const tempProp = {
|
|
348
|
+
type: type,
|
|
349
|
+
description: this.properties[key].item.description
|
|
350
|
+
};
|
|
351
|
+
this.data[key] = [this.convertValue(tempProp, value, [key, 0], true)];
|
|
352
|
+
}
|
|
295
353
|
}
|
|
296
354
|
else {
|
|
297
355
|
this.throwInputError("ARRAY_01", [key], value);
|
|
@@ -455,37 +513,6 @@ class RequestType extends ReqResType_1.default {
|
|
|
455
513
|
case 'enum':
|
|
456
514
|
case 'enum?':
|
|
457
515
|
this.setEnum([...keys, i], values[i]);
|
|
458
|
-
// const toEnumValue = [];
|
|
459
|
-
// for (const value of [...values]) {
|
|
460
|
-
// console.log(values, value)
|
|
461
|
-
// switch (property.item.enumType) {
|
|
462
|
-
// case 'number':
|
|
463
|
-
// case 'number?':
|
|
464
|
-
// console.log(this.isNumber(value));
|
|
465
|
-
// if (this.isNumber(value) === false) {
|
|
466
|
-
// this.throwInputError("NUMBER_31", [...keys, i], value);
|
|
467
|
-
// }
|
|
468
|
-
// toEnumValue.push(Number(value));
|
|
469
|
-
// break;
|
|
470
|
-
// case 'string':
|
|
471
|
-
// case 'string?':
|
|
472
|
-
// switch (typeof value) {
|
|
473
|
-
// case 'number':
|
|
474
|
-
// toEnumValue.push(value.toString());
|
|
475
|
-
// break;
|
|
476
|
-
// case 'string':
|
|
477
|
-
// toEnumValue.push(value);
|
|
478
|
-
// break;
|
|
479
|
-
// default:
|
|
480
|
-
// this.throwInputError("STRING_31", [...keys, i], value);
|
|
481
|
-
// }
|
|
482
|
-
// break;
|
|
483
|
-
// }
|
|
484
|
-
// if (Object.keys(property.item.enums).includes(value.toString()) === false) {
|
|
485
|
-
// this.throwInputError("ENUM_32", [...keys, i], value);
|
|
486
|
-
// }
|
|
487
|
-
// }
|
|
488
|
-
// this.changeBody([...keys, i], toEnumValue);
|
|
489
516
|
break;
|
|
490
517
|
case 'map':
|
|
491
518
|
case 'map?':
|
|
@@ -684,7 +711,7 @@ class RequestType extends ReqResType_1.default {
|
|
|
684
711
|
* 指定された型に基づいて入力値を変換します。
|
|
685
712
|
* 型変換に失敗した場合は例外をスローします。
|
|
686
713
|
*
|
|
687
|
-
* @param {string}
|
|
714
|
+
* @param {string} property - The type to convert to (e.g., 'number', 'boolean', 'string', 'date', 'time', 'datetime')
|
|
688
715
|
* 変換する型(例: 'number', 'boolean', 'string', 'date', 'time', 'datetime')
|
|
689
716
|
* @param {any} value - The value to convert
|
|
690
717
|
* 変換する値
|
|
@@ -693,14 +720,21 @@ class RequestType extends ReqResType_1.default {
|
|
|
693
720
|
* @returns {any} The converted value, 変換された値
|
|
694
721
|
* @throws {InputErrorException} Thrown if type conversion fails, 型変換に失敗した場合にスローされます
|
|
695
722
|
*/
|
|
696
|
-
convertValue(
|
|
697
|
-
switch (type) {
|
|
723
|
+
convertValue(property, value, keys, isRequestBody) {
|
|
724
|
+
switch (property.type) {
|
|
698
725
|
case 'number':
|
|
699
726
|
case 'number?':
|
|
700
727
|
if (this.isNumber(value) === false) {
|
|
701
728
|
this.throwInputError(isRequestBody ? "NUMBER_21" : "NUMBER_91", keys, value);
|
|
702
729
|
}
|
|
703
|
-
|
|
730
|
+
const numberValue = Number(value);
|
|
731
|
+
if (property.min !== undefined && numberValue < property.min) {
|
|
732
|
+
this.throwInputError(isRequestBody ? "NUMBER_21" : "NUMBER_91", keys, value);
|
|
733
|
+
}
|
|
734
|
+
if (property.max !== undefined && numberValue > property.max) {
|
|
735
|
+
this.throwInputError(isRequestBody ? "NUMBER_21" : "NUMBER_91", keys, value);
|
|
736
|
+
}
|
|
737
|
+
return numberValue;
|
|
704
738
|
case 'boolean':
|
|
705
739
|
case 'boolean?':
|
|
706
740
|
switch (typeof value) {
|
|
@@ -721,14 +755,21 @@ class RequestType extends ReqResType_1.default {
|
|
|
721
755
|
}
|
|
722
756
|
case 'string':
|
|
723
757
|
case 'string?':
|
|
758
|
+
let stringValue = "";
|
|
724
759
|
switch (typeof value) {
|
|
725
760
|
case 'number':
|
|
726
|
-
|
|
761
|
+
stringValue = value.toString();
|
|
762
|
+
break;
|
|
727
763
|
case 'string':
|
|
728
|
-
|
|
764
|
+
stringValue = value;
|
|
765
|
+
break;
|
|
729
766
|
default:
|
|
730
767
|
this.throwInputError(isRequestBody ? "STRING_21" : "STRING_91", keys, value);
|
|
731
768
|
}
|
|
769
|
+
if (property.maxLength !== undefined && stringValue.length > property.maxLength) {
|
|
770
|
+
this.throwInputError(isRequestBody ? "STRING_22" : "STRING_92", keys, value);
|
|
771
|
+
}
|
|
772
|
+
return stringValue;
|
|
732
773
|
case 'uuid':
|
|
733
774
|
case 'uuid?':
|
|
734
775
|
if (StringUtil_1.default.isUUID(value)) {
|
|
@@ -797,7 +838,7 @@ class RequestType extends ReqResType_1.default {
|
|
|
797
838
|
*/
|
|
798
839
|
convertInput(keys, value) {
|
|
799
840
|
const property = this.getProperty(keys);
|
|
800
|
-
this.changeBody(keys, this.convertValue(property
|
|
841
|
+
this.changeBody(keys, this.convertValue(property, value, keys, true));
|
|
801
842
|
}
|
|
802
843
|
// ****************************************************************************
|
|
803
844
|
// for create swagger
|
package/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { AxiosResponse } from "axios";
|
|
|
2
2
|
import { Request, Response } from 'express';
|
|
3
3
|
import { Pool, PoolClient } from 'pg';
|
|
4
4
|
import { ErrorMessageType, IncomingHttpHeaders } from './src/reqestResponse/RequestType';
|
|
5
|
-
import { ArrayType, EnumType, MapType, ObjectType, PrimitiveType } from './src/reqestResponse/ReqResType';
|
|
5
|
+
import { ArrayType, EnumType, MapType, NumberType, ObjectType, PrimitiveType, StringType } from './src/reqestResponse/ReqResType';
|
|
6
6
|
|
|
7
7
|
import { MethodType } from './src/Service';
|
|
8
8
|
export { MethodType } from './src/Service';
|
|
@@ -90,13 +90,13 @@ declare module 'pg-mvc-service' {
|
|
|
90
90
|
}
|
|
91
91
|
export function createSwagger(services: Service[], name: string, url: string, params: Array<IParams>): string;
|
|
92
92
|
|
|
93
|
-
export type PropertyType = PrimitiveType | ObjectType | ArrayType | EnumType | MapType;
|
|
93
|
+
export type PropertyType = PrimitiveType | StringType | NumberType | ObjectType | ArrayType | EnumType | MapType;
|
|
94
94
|
|
|
95
95
|
export class RequestType {
|
|
96
96
|
constructor();
|
|
97
97
|
|
|
98
98
|
protected properties: { [key: string]: PropertyType; };
|
|
99
|
-
protected paramProperties: Array<(PrimitiveType | EnumType) & { key: string }>;
|
|
99
|
+
protected paramProperties: Array<(PrimitiveType | StringType | NumberType | EnumType) & { key: string }>;
|
|
100
100
|
protected readonly ERROR_MESSAGE: ErrorMessageType;
|
|
101
101
|
|
|
102
102
|
protected throwException(code: string, message: string): never;
|
package/package.json
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
1
1
|
type PrimitiveKeyType =
|
|
2
|
-
'
|
|
3
|
-
'
|
|
2
|
+
'boolean' | 'date' | 'datetime' | 'time' | 'uuid' | 'mail' | 'https' | 'base64' |
|
|
3
|
+
'boolean?' | 'date?' | 'datetime?' | 'time?' | 'uuid?' | 'mail?' | 'https?' | 'base64?';
|
|
4
4
|
|
|
5
5
|
export type PrimitiveType = {
|
|
6
6
|
type: PrimitiveKeyType;
|
|
7
7
|
description?: string;
|
|
8
8
|
};
|
|
9
|
+
export type StringType = {
|
|
10
|
+
type: 'string' | 'string?';
|
|
11
|
+
description?: string;
|
|
12
|
+
maxLength?: number;
|
|
13
|
+
};
|
|
14
|
+
export type NumberType = {
|
|
15
|
+
type: 'number' | 'number?';
|
|
16
|
+
description?: string;
|
|
17
|
+
max?: number;
|
|
18
|
+
min?: number;
|
|
19
|
+
};
|
|
9
20
|
export type ObjectType = {
|
|
10
21
|
type: 'object' | 'object?';
|
|
11
22
|
description?: string;
|
|
@@ -30,39 +41,12 @@ export type EnumType = {
|
|
|
30
41
|
enums: {[key: string | number]: string};
|
|
31
42
|
};
|
|
32
43
|
|
|
33
|
-
export type PropertyType = PrimitiveType | ObjectType | ArrayType | EnumType | MapType;
|
|
44
|
+
export type PropertyType = PrimitiveType | StringType | NumberType | ObjectType | ArrayType | EnumType | MapType;
|
|
34
45
|
|
|
35
46
|
export default class ReqResType {
|
|
36
47
|
|
|
37
48
|
protected properties: { [key: string]: PropertyType; } = {};
|
|
38
49
|
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Retrieve the property definition corresponding to the specified key path.
|
|
42
|
-
* 指定されたキーパスに対応するプロパティ定義を取得します。
|
|
43
|
-
* @param {Array<string | number>} keys - Access path to the property (array of strings or index numbers)
|
|
44
|
-
* プロパティへのアクセスパス(文字列またはインデックス番号の配列)
|
|
45
|
-
* @returns {BaseType} Property definition object
|
|
46
|
-
* プロパティ定義オブジェクト
|
|
47
|
-
*/
|
|
48
|
-
// protected getProperty(keys: Array<string | number>) {
|
|
49
|
-
// let property: any = this.properties;
|
|
50
|
-
// for (let i = 0;i < keys.length;i++) {
|
|
51
|
-
// const key = keys[i];
|
|
52
|
-
// if (typeof key === 'number') {
|
|
53
|
-
// property = property.properties;
|
|
54
|
-
// continue;
|
|
55
|
-
// }
|
|
56
|
-
|
|
57
|
-
// if (i === 0) {
|
|
58
|
-
// property = property[key];
|
|
59
|
-
// } else {
|
|
60
|
-
// property = property.properties[key];
|
|
61
|
-
// }
|
|
62
|
-
// }
|
|
63
|
-
|
|
64
|
-
// return property;
|
|
65
|
-
// }
|
|
66
50
|
/**
|
|
67
51
|
* Retrieve property type data
|
|
68
52
|
* プロパティ型のデータを取得
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Request } from 'express';
|
|
2
|
-
import ReqResType, { EnumType, PrimitiveType } from "./ReqResType";
|
|
2
|
+
import ReqResType, { EnumType, NumberType, PrimitiveType, PropertyType, StringType } from "./ReqResType";
|
|
3
3
|
import { InputErrorException } from '../exceptions/Exception';
|
|
4
4
|
import StringUtil from '../Utils/StringUtil';
|
|
5
5
|
import { ValidateStringUtil } from 'type-utils-n-daira';
|
|
@@ -11,8 +11,11 @@ export interface ErrorMessageType {
|
|
|
11
11
|
INVALID_OBJECT: string;
|
|
12
12
|
INVALID_ARRAY: string;
|
|
13
13
|
INVALID_NUMBER: string;
|
|
14
|
+
INVALID_NUMBER_MIN: string;
|
|
15
|
+
INVALID_NUMBER_MAX: string;
|
|
14
16
|
INVALID_BOOL: string;
|
|
15
17
|
INVALID_STRING: string;
|
|
18
|
+
INVALID_STRING_MAX_LENGTH: string;
|
|
16
19
|
INVALID_UUID: string;
|
|
17
20
|
INVALID_MAIL: string;
|
|
18
21
|
INVALID_HTTPS: string;
|
|
@@ -40,8 +43,11 @@ export class RequestType extends ReqResType {
|
|
|
40
43
|
INVALID_OBJECT: '{property} must be of type Object. ({value})',
|
|
41
44
|
INVALID_ARRAY: '{property} must be of type Array. ({value})',
|
|
42
45
|
INVALID_NUMBER: '{property} must be of type number. ({value})',
|
|
46
|
+
INVALID_NUMBER_MAX: '{property} must be less than or equal to {max}. ({value})',
|
|
47
|
+
INVALID_NUMBER_MIN: '{property} must be greater than or equal to {min}. ({value})',
|
|
43
48
|
INVALID_BOOL: '{property} must be of type bool or a string with true, false, or a number with 0, 1. ({value})',
|
|
44
49
|
INVALID_STRING: '{property} must be of type string. ({value})',
|
|
50
|
+
INVALID_STRING_MAX_LENGTH: '{property} must be less than or equal to {maxLength} characters. ({value})',
|
|
45
51
|
INVALID_UUID: '{property} must be a UUID. ({value})',
|
|
46
52
|
INVALID_MAIL: '{property} must be an email. ({value})',
|
|
47
53
|
INVALID_HTTPS: '{property} must be an https or http URL. ({value})',
|
|
@@ -60,8 +66,11 @@ export class RequestType extends ReqResType {
|
|
|
60
66
|
INVALID_OBJECT: '{property}はobject型で入力してください。({value})',
|
|
61
67
|
INVALID_ARRAY: '{property}はarray型で入力してください。({value})',
|
|
62
68
|
INVALID_NUMBER: '{property}はnumber型または半角数値のstring型で入力してください。({value})',
|
|
69
|
+
INVALID_NUMBER_MAX: '{property}は{max}以下の値で入力してください。({value})',
|
|
70
|
+
INVALID_NUMBER_MIN: '{property}は{min}以上の値で入力してください。({value})',
|
|
63
71
|
INVALID_BOOL: '{property}はboolean型またはtrue、falseのstring型または0、1のnumber型で入力してください。({value})',
|
|
64
72
|
INVALID_STRING: '{property}はstring型で入力してください。({value})',
|
|
73
|
+
INVALID_STRING_MAX_LENGTH: '{property}は{maxLength}文字以内で入力してください。({value})',
|
|
65
74
|
INVALID_UUID: '{property}はUUID形式のstring型で入力してください。({value})',
|
|
66
75
|
INVALID_MAIL: '{property}はメールアドレス形式のstring型で入力してください。({value})',
|
|
67
76
|
INVALID_HTTPS: '{property}はhttpsまたはhttpのURL形式のstring型で入力してください。({value})',
|
|
@@ -76,7 +85,7 @@ export class RequestType extends ReqResType {
|
|
|
76
85
|
}
|
|
77
86
|
protected readonly ERROR_MESSAGE: ErrorMessageType = process.env.TZ === 'Asia/Tokyo' ? this.ERROR_MESSAGE_JAPAN : this.ERROR_MESSAGE_ENGLISH;
|
|
78
87
|
|
|
79
|
-
protected paramProperties: Array<(PrimitiveType | EnumType) & { key: string }> = []
|
|
88
|
+
protected paramProperties: Array<(PrimitiveType | StringType | NumberType | EnumType) & { key: string }> = []
|
|
80
89
|
get paramPath(): string {
|
|
81
90
|
return this.paramProperties.map(property => `/{${property.key}}`).join("");
|
|
82
91
|
}
|
|
@@ -123,7 +132,7 @@ export class RequestType extends ReqResType {
|
|
|
123
132
|
throw new Error(`${key} is not set in paramProperties.`);
|
|
124
133
|
}
|
|
125
134
|
const property = this.paramProperties[index];
|
|
126
|
-
this.params[key] = this.convertValue(property
|
|
135
|
+
this.params[key] = this.convertValue(property, value, [key, `(pathIndex: ${index})`], false);
|
|
127
136
|
}
|
|
128
137
|
}
|
|
129
138
|
this.params = request.params ?? {};
|
|
@@ -143,13 +152,13 @@ export class RequestType extends ReqResType {
|
|
|
143
152
|
private throwInputError(code:
|
|
144
153
|
"REQUIRE_00" | "REQUIRE_01" | "OBJECT_01" | "ARRAY_01" | "UNNECESSARY_01" |
|
|
145
154
|
"REQUIRE_11" | "OBJECT_11" | "ARRAY_11" | "UNNECESSARY_11" |
|
|
146
|
-
"NUMBER_21" | "BOOL_21" | "BOOL_22" | "BOOL_23" | "STRING_21" | "UUID_21" | "MAIL_21" | "DATE_21" | "DATE_22" |
|
|
155
|
+
"NUMBER_21" | "NUMBER_22" | "NUMBER_23" | "BOOL_21" | "BOOL_22" | "BOOL_23" | "STRING_21" | "STRING_22" | "UUID_21" | "MAIL_21" | "DATE_21" | "DATE_22" |
|
|
147
156
|
"TIME_21" | "DATETIME_21" | "DATETIME_22" | "HTTPS_21" | "BASE64_21" |
|
|
148
157
|
"REQUIRE_31" |
|
|
149
|
-
"ENUM_32" | "
|
|
158
|
+
"ENUM_32" | "ENUM_41" | "ENUM_42" | "NUMBER_41" | "STRING_41" |
|
|
150
159
|
"MAP_01" | "MAP_02" | "MAP_03" | "MAP_04" | "MAP_05" | "MAP_11" | "MAP_12" | "MAP_13" | "MAP_14" | "MAP_15" |
|
|
151
160
|
"MAP_31" | "MAP_32" | "MAP_33" | "MAP_34" | "MAP_35" |
|
|
152
|
-
"NUMBER_91" | "BOOL_91" | "BOOL_92" | "BOOL_93" | "STRING_91" | "UUID_91" | "MAIL_91" | "DATE_91" | "DATE_92" |
|
|
161
|
+
"NUMBER_91" | "NUMBER_92" | "NUMBER_93" | "BOOL_91" | "BOOL_92" | "BOOL_93" | "STRING_91" | "STRING_92" | "UUID_91" | "MAIL_91" | "DATE_91" | "DATE_92" |
|
|
153
162
|
"TIME_91" | "DATETIME_91" | "DATETIME_92" | "HTTPS_91" | "BASE64_91"
|
|
154
163
|
, keys: Array<string | number>, value: any): never {
|
|
155
164
|
const list = {
|
|
@@ -163,10 +172,13 @@ export class RequestType extends ReqResType {
|
|
|
163
172
|
"ARRAY_11": this.ERROR_MESSAGE.INVALID_ARRAY,
|
|
164
173
|
"UNNECESSARY_11": this.ERROR_MESSAGE.UNNECESSARY,
|
|
165
174
|
"NUMBER_21": this.ERROR_MESSAGE.INVALID_NUMBER,
|
|
175
|
+
"NUMBER_22": this.ERROR_MESSAGE.INVALID_NUMBER_MIN,
|
|
176
|
+
"NUMBER_23": this.ERROR_MESSAGE.INVALID_NUMBER_MAX,
|
|
166
177
|
"BOOL_21": this.ERROR_MESSAGE.INVALID_BOOL,
|
|
167
178
|
"BOOL_22": this.ERROR_MESSAGE.INVALID_BOOL,
|
|
168
179
|
"BOOL_23": this.ERROR_MESSAGE.INVALID_BOOL,
|
|
169
180
|
"STRING_21": this.ERROR_MESSAGE.INVALID_STRING,
|
|
181
|
+
"STRING_22": this.ERROR_MESSAGE.INVALID_STRING_MAX_LENGTH,
|
|
170
182
|
"UUID_21": this.ERROR_MESSAGE.INVALID_UUID,
|
|
171
183
|
"MAIL_21": this.ERROR_MESSAGE.INVALID_MAIL,
|
|
172
184
|
"DATE_21": this.ERROR_MESSAGE.INVALID_DATE,
|
|
@@ -177,8 +189,6 @@ export class RequestType extends ReqResType {
|
|
|
177
189
|
"HTTPS_21": this.ERROR_MESSAGE.INVALID_HTTPS,
|
|
178
190
|
"BASE64_21": this.ERROR_MESSAGE.INVALID_BASE64,
|
|
179
191
|
"REQUIRE_31": this.ERROR_MESSAGE.REQUIRED,
|
|
180
|
-
"NUMBER_31": this.ERROR_MESSAGE.INVALID_NUMBER,
|
|
181
|
-
"STRING_31": this.ERROR_MESSAGE.INVALID_STRING,
|
|
182
192
|
"ENUM_32": this.ERROR_MESSAGE.INVALID_ENUM,
|
|
183
193
|
"NUMBER_41": this.ERROR_MESSAGE.INVALID_NUMBER,
|
|
184
194
|
"STRING_41": this.ERROR_MESSAGE.INVALID_STRING,
|
|
@@ -200,10 +210,13 @@ export class RequestType extends ReqResType {
|
|
|
200
210
|
"MAP_34": this.ERROR_MESSAGE.INVALID_MAP_BOOL,
|
|
201
211
|
"MAP_35": this.ERROR_MESSAGE.INVALID_MAP_BOOL,
|
|
202
212
|
"NUMBER_91": this.ERROR_MESSAGE.INVALID_NUMBER,
|
|
213
|
+
"NUMBER_92": this.ERROR_MESSAGE.INVALID_NUMBER_MIN,
|
|
214
|
+
"NUMBER_93": this.ERROR_MESSAGE.INVALID_NUMBER_MAX,
|
|
203
215
|
"BOOL_91": this.ERROR_MESSAGE.INVALID_BOOL,
|
|
204
216
|
"BOOL_92": this.ERROR_MESSAGE.INVALID_BOOL,
|
|
205
217
|
"BOOL_93": this.ERROR_MESSAGE.INVALID_BOOL,
|
|
206
218
|
"STRING_91": this.ERROR_MESSAGE.INVALID_STRING,
|
|
219
|
+
"STRING_92": this.ERROR_MESSAGE.INVALID_STRING_MAX_LENGTH,
|
|
207
220
|
"UUID_91": this.ERROR_MESSAGE.INVALID_UUID,
|
|
208
221
|
"MAIL_91": this.ERROR_MESSAGE.INVALID_MAIL,
|
|
209
222
|
"DATE_91": this.ERROR_MESSAGE.INVALID_DATE,
|
|
@@ -216,9 +229,21 @@ export class RequestType extends ReqResType {
|
|
|
216
229
|
}
|
|
217
230
|
|
|
218
231
|
let errorMessage = list[code];
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
232
|
+
const property = this.getProperty(keys);
|
|
233
|
+
switch (property.type) {
|
|
234
|
+
case 'enum':
|
|
235
|
+
case 'enum?':
|
|
236
|
+
errorMessage = errorMessage.replace('{enums}', Object.keys(property.enums ?? '').join(','));
|
|
237
|
+
break;
|
|
238
|
+
case 'string':
|
|
239
|
+
case 'string?':
|
|
240
|
+
errorMessage = errorMessage.replace('{maxLength}', Object.keys(property.maxLength ?? '').join(','));
|
|
241
|
+
break;
|
|
242
|
+
case 'number':
|
|
243
|
+
case 'number?':
|
|
244
|
+
errorMessage = errorMessage.replace('{max}', Object.keys(property.max ?? '').join(','));
|
|
245
|
+
errorMessage = errorMessage.replace('{min}', Object.keys(property.min ?? '').join(','));
|
|
246
|
+
break;
|
|
222
247
|
}
|
|
223
248
|
|
|
224
249
|
errorMessage = errorMessage.replace("{property}", keys.join('.')).replace("{value}", value);
|
|
@@ -303,7 +328,42 @@ export class RequestType extends ReqResType {
|
|
|
303
328
|
if (request.method === 'GET' || request.method === 'DELETE') {
|
|
304
329
|
// GET,DELETEメソッドの場合、?array=1&array=2で配列となるが、
|
|
305
330
|
// ?array=1のみで終わる場合は配列にならないため、直接配列にしている
|
|
306
|
-
|
|
331
|
+
const type = this.properties[key].item.type;
|
|
332
|
+
if (type === 'object' || type === 'object?' || type === 'array' || type === 'array?' || type === 'map' || type === 'map?') {
|
|
333
|
+
throw new Error("GETまたはDELETEメソッドでは配列型にobject, array, mapを使用することはできません。");
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (type === 'enum' || type === 'enum?') {
|
|
337
|
+
const tempProp = {
|
|
338
|
+
type: type,
|
|
339
|
+
description: this.properties[key].item.description,
|
|
340
|
+
enumType: this.properties[key].item.enumType,
|
|
341
|
+
enums: this.properties[key].item.enums,
|
|
342
|
+
};
|
|
343
|
+
this.data[key] = [this.convertValue(tempProp, value, [key, 0], true)];
|
|
344
|
+
} else if (type === 'string' || type === 'string?') {
|
|
345
|
+
const tempProp = {
|
|
346
|
+
type: type,
|
|
347
|
+
description: this.properties[key].item.description,
|
|
348
|
+
maxLength: this.properties[key].item.maxLength,
|
|
349
|
+
};
|
|
350
|
+
this.data[key] = [this.convertValue(tempProp, value, [key, 0], true)];
|
|
351
|
+
} else if (type === 'number' || type === 'number?') {
|
|
352
|
+
const tempProp = {
|
|
353
|
+
type: type,
|
|
354
|
+
description: this.properties[key].item.description,
|
|
355
|
+
max: this.properties[key].item.max,
|
|
356
|
+
min: this.properties[key].item.min,
|
|
357
|
+
};
|
|
358
|
+
this.data[key] = [this.convertValue(tempProp, value, [key, 0], true)];
|
|
359
|
+
} else {
|
|
360
|
+
const tempProp = {
|
|
361
|
+
type: type,
|
|
362
|
+
description: this.properties[key].item.description
|
|
363
|
+
};
|
|
364
|
+
this.data[key] = [this.convertValue(tempProp, value, [key, 0], true)];
|
|
365
|
+
}
|
|
366
|
+
|
|
307
367
|
} else {
|
|
308
368
|
this.throwInputError("ARRAY_01", [key], value);
|
|
309
369
|
}
|
|
@@ -475,38 +535,6 @@ export class RequestType extends ReqResType {
|
|
|
475
535
|
case 'enum':
|
|
476
536
|
case 'enum?':
|
|
477
537
|
this.setEnum([...keys, i], values[i]);
|
|
478
|
-
// const toEnumValue = [];
|
|
479
|
-
// for (const value of [...values]) {
|
|
480
|
-
// console.log(values, value)
|
|
481
|
-
// switch (property.item.enumType) {
|
|
482
|
-
// case 'number':
|
|
483
|
-
// case 'number?':
|
|
484
|
-
// console.log(this.isNumber(value));
|
|
485
|
-
// if (this.isNumber(value) === false) {
|
|
486
|
-
// this.throwInputError("NUMBER_31", [...keys, i], value);
|
|
487
|
-
// }
|
|
488
|
-
// toEnumValue.push(Number(value));
|
|
489
|
-
// break;
|
|
490
|
-
// case 'string':
|
|
491
|
-
// case 'string?':
|
|
492
|
-
// switch (typeof value) {
|
|
493
|
-
// case 'number':
|
|
494
|
-
// toEnumValue.push(value.toString());
|
|
495
|
-
// break;
|
|
496
|
-
// case 'string':
|
|
497
|
-
// toEnumValue.push(value);
|
|
498
|
-
// break;
|
|
499
|
-
// default:
|
|
500
|
-
// this.throwInputError("STRING_31", [...keys, i], value);
|
|
501
|
-
// }
|
|
502
|
-
// break;
|
|
503
|
-
// }
|
|
504
|
-
|
|
505
|
-
// if (Object.keys(property.item.enums).includes(value.toString()) === false) {
|
|
506
|
-
// this.throwInputError("ENUM_32", [...keys, i], value);
|
|
507
|
-
// }
|
|
508
|
-
// }
|
|
509
|
-
// this.changeBody([...keys, i], toEnumValue);
|
|
510
538
|
break;
|
|
511
539
|
case 'map':
|
|
512
540
|
case 'map?':
|
|
@@ -712,7 +740,7 @@ export class RequestType extends ReqResType {
|
|
|
712
740
|
* 指定された型に基づいて入力値を変換します。
|
|
713
741
|
* 型変換に失敗した場合は例外をスローします。
|
|
714
742
|
*
|
|
715
|
-
* @param {string}
|
|
743
|
+
* @param {string} property - The type to convert to (e.g., 'number', 'boolean', 'string', 'date', 'time', 'datetime')
|
|
716
744
|
* 変換する型(例: 'number', 'boolean', 'string', 'date', 'time', 'datetime')
|
|
717
745
|
* @param {any} value - The value to convert
|
|
718
746
|
* 変換する値
|
|
@@ -721,15 +749,23 @@ export class RequestType extends ReqResType {
|
|
|
721
749
|
* @returns {any} The converted value, 変換された値
|
|
722
750
|
* @throws {InputErrorException} Thrown if type conversion fails, 型変換に失敗した場合にスローされます
|
|
723
751
|
*/
|
|
724
|
-
private convertValue(
|
|
752
|
+
private convertValue(property: PropertyType, value: any, keys: Array<string | number>, isRequestBody: boolean) {
|
|
725
753
|
|
|
726
|
-
switch (type) {
|
|
754
|
+
switch (property.type) {
|
|
727
755
|
case 'number':
|
|
728
756
|
case 'number?':
|
|
729
757
|
if (this.isNumber(value) === false) {
|
|
730
758
|
this.throwInputError(isRequestBody ? "NUMBER_21" : "NUMBER_91", keys, value);
|
|
731
759
|
}
|
|
732
|
-
|
|
760
|
+
const numberValue = Number(value);
|
|
761
|
+
if (property.min !== undefined && numberValue < property.min) {
|
|
762
|
+
this.throwInputError(isRequestBody ? "NUMBER_21" : "NUMBER_91", keys, value);
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
if (property.max !== undefined && numberValue > property.max) {
|
|
766
|
+
this.throwInputError(isRequestBody ? "NUMBER_21" : "NUMBER_91", keys, value);
|
|
767
|
+
}
|
|
768
|
+
return numberValue;
|
|
733
769
|
case 'boolean':
|
|
734
770
|
case 'boolean?':
|
|
735
771
|
switch (typeof value) {
|
|
@@ -750,14 +786,22 @@ export class RequestType extends ReqResType {
|
|
|
750
786
|
}
|
|
751
787
|
case 'string':
|
|
752
788
|
case 'string?':
|
|
789
|
+
let stringValue: string = "";
|
|
753
790
|
switch (typeof value) {
|
|
754
791
|
case 'number':
|
|
755
|
-
|
|
792
|
+
stringValue = value.toString();
|
|
793
|
+
break;
|
|
756
794
|
case 'string':
|
|
757
|
-
|
|
795
|
+
stringValue = value;
|
|
796
|
+
break;
|
|
758
797
|
default:
|
|
759
798
|
this.throwInputError(isRequestBody ? "STRING_21" : "STRING_91", keys, value);
|
|
760
799
|
}
|
|
800
|
+
|
|
801
|
+
if (property.maxLength !== undefined && stringValue.length > property.maxLength) {
|
|
802
|
+
this.throwInputError(isRequestBody ? "STRING_22" : "STRING_92", keys, value);
|
|
803
|
+
}
|
|
804
|
+
return stringValue;
|
|
761
805
|
case 'uuid':
|
|
762
806
|
case 'uuid?':
|
|
763
807
|
if (StringUtil.isUUID(value)) {
|
|
@@ -831,7 +875,7 @@ export class RequestType extends ReqResType {
|
|
|
831
875
|
*/
|
|
832
876
|
private convertInput(keys: Array<string | number>, value: any) {
|
|
833
877
|
const property = this.getProperty(keys);
|
|
834
|
-
this.changeBody(keys, this.convertValue(property
|
|
878
|
+
this.changeBody(keys, this.convertValue(property, value, keys, true));
|
|
835
879
|
}
|
|
836
880
|
|
|
837
881
|
|