pg-mvc-service 1.0.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/README.md +1 -0
- package/dist/PoolManager.js +57 -0
- package/dist/Service.js +257 -0
- package/dist/clients/AwsS3Client.js +249 -0
- package/dist/clients/Base64Client.js +153 -0
- package/dist/clients/EncryptClient.js +85 -0
- package/dist/clients/StringClient.js +13 -0
- package/dist/documents/Swagger.js +94 -0
- package/dist/exceptions/Exception.js +53 -0
- package/dist/index.js +16 -0
- package/dist/models/MigrateDatabase.js +138 -0
- package/dist/models/MigrateRollback.js +146 -0
- package/dist/models/MigrateTable.js +51 -0
- package/dist/models/SqlUtils/SelectExpression.js +92 -0
- package/dist/models/SqlUtils/ValidateValueUtil.js +250 -0
- package/dist/models/SqlUtils/WhereExpression.js +256 -0
- package/dist/models/TableDoc.js +353 -0
- package/dist/models/TableModel.js +636 -0
- package/dist/models/Type.js +2 -0
- package/dist/models/Utils/DateTimeUtil.js +134 -0
- package/dist/models/Utils/NumberUtil.js +28 -0
- package/dist/models/Utils/StringUtil.js +31 -0
- package/dist/models/ValidateClient.js +164 -0
- package/dist/models/index.js +14 -0
- package/dist/reqestResponse/ReqResType.js +196 -0
- package/dist/reqestResponse/RequestType.js +742 -0
- package/dist/reqestResponse/ResponseType.js +380 -0
- package/index.d.ts +306 -0
- package/package.json +36 -0
- package/src/PoolManager.ts +48 -0
- package/src/Service.ts +251 -0
- package/src/clients/AwsS3Client.ts +229 -0
- package/src/clients/Base64Client.ts +155 -0
- package/src/clients/EncryptClient.ts +100 -0
- package/src/clients/StringClient.ts +14 -0
- package/src/documents/Swagger.ts +111 -0
- package/src/exceptions/Exception.ts +54 -0
- package/src/index.ts +7 -0
- package/src/models/MigrateDatabase.ts +135 -0
- package/src/models/MigrateRollback.ts +151 -0
- package/src/models/MigrateTable.ts +56 -0
- package/src/models/SqlUtils/SelectExpression.ts +97 -0
- package/src/models/SqlUtils/ValidateValueUtil.ts +270 -0
- package/src/models/SqlUtils/WhereExpression.ts +286 -0
- package/src/models/TableDoc.ts +360 -0
- package/src/models/TableModel.ts +713 -0
- package/src/models/Type.ts +59 -0
- package/src/models/Utils/DateTimeUtil.ts +146 -0
- package/src/models/Utils/NumberUtil.ts +23 -0
- package/src/models/Utils/StringUtil.ts +33 -0
- package/src/models/ValidateClient.ts +182 -0
- package/src/models/index.ts +7 -0
- package/src/reqestResponse/ReqResType.ts +242 -0
- package/src/reqestResponse/RequestType.ts +851 -0
- package/src/reqestResponse/ResponseType.ts +418 -0
- package/tsconfig.json +14 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { TableModel } from "./TableModel";
|
|
2
|
+
|
|
3
|
+
export type TSqlValue = string | number | boolean | Date | null | Array<string | null> | Array<number | null> | Array<Date | null> | Array<Boolean | null>;
|
|
4
|
+
|
|
5
|
+
// column type
|
|
6
|
+
export type TColumnAttribute = "primary" | "nullable" | "hasDefault" | "noDefault";
|
|
7
|
+
export type TColumnType = "number" | "string" | "uuid" | "date" | "time" | "timestamp" | "bool";
|
|
8
|
+
export type TColumnArrayType = "number[]" | "string[]" | "uuid[]" | "date[]" | "time[]" | "timestamp[]" | "bool[]";
|
|
9
|
+
type TColumnBase = {
|
|
10
|
+
alias?: string,
|
|
11
|
+
type: TColumnType | TColumnArrayType,
|
|
12
|
+
attribute: TColumnAttribute,
|
|
13
|
+
default?: string,
|
|
14
|
+
comment?: string
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type TStringColumn = TColumnBase & {
|
|
18
|
+
type: "string",
|
|
19
|
+
length: number
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
type TNonStringColumn = TColumnBase & {
|
|
23
|
+
type: Exclude<TColumnType, "string">,
|
|
24
|
+
length?: undefined
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type TStringArrayColumn = TColumnBase & {
|
|
28
|
+
type: "string[]",
|
|
29
|
+
length: number,
|
|
30
|
+
attribute: Exclude<TColumnAttribute, "primary">
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
type TArrayColumn = TColumnBase & {
|
|
34
|
+
type: Exclude<TColumnArrayType, "string[]">,
|
|
35
|
+
length?: undefined,
|
|
36
|
+
attribute: Exclude<TColumnAttribute, "primary">
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type TColumn = TStringColumn | TNonStringColumn | TStringArrayColumn | TArrayColumn;
|
|
40
|
+
export type TColumnDetail = TColumn & {
|
|
41
|
+
columnName: string,
|
|
42
|
+
tableName: string,
|
|
43
|
+
expression: string
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type TOperator = "=" | "!=" | ">" | ">=" | "<" | "<=" | "like" | "ilike" | "h2f_like" | "h2f_ilike" | "in" | "not in";
|
|
47
|
+
export type TColumnInfo = { model: TableModel, name: string }
|
|
48
|
+
export type TQuery = {sql: string, vars?: Array<any>};
|
|
49
|
+
export type TSelectExpression = { expression: string, alias: string }
|
|
50
|
+
export type TAggregateFuncType = 'sum' | 'avg' | 'max' | 'min' | 'count';
|
|
51
|
+
export type TCondition = string | {
|
|
52
|
+
l: string | TColumnInfo,
|
|
53
|
+
o: TOperator,
|
|
54
|
+
r: TSqlValue | Array<TSqlValue> | TColumnInfo
|
|
55
|
+
};
|
|
56
|
+
export type TNestedCondition = TCondition | ['AND' | 'OR', ...TNestedCondition[]] | TNestedCondition[];
|
|
57
|
+
export type TSortKeyword = 'desc' | 'asc';
|
|
58
|
+
export type TKeyFormat = 'snake' | 'lowerCamel';
|
|
59
|
+
export type TOption = {[key: string]: TSqlValue};
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
export default class DateTimeUtil {
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks if the value is a valid date-time format
|
|
5
|
+
* 値が有効な日付時間形式かどうかを確認します
|
|
6
|
+
* @param value - 検証する値, The value to be validated
|
|
7
|
+
* @returns {boolean} - 値が有効な日付時間形式であるかどうか, Whether the value is a valid date-time format
|
|
8
|
+
*/
|
|
9
|
+
private static isErrorDateTime(value: string): boolean {
|
|
10
|
+
try {
|
|
11
|
+
const [datePart, timePart] = value.split(' ');
|
|
12
|
+
const [year, month, day] = datePart.split('-').map(Number);
|
|
13
|
+
let [hour, minute, sec] = [0, 0, 0];
|
|
14
|
+
if (timePart !== undefined) {
|
|
15
|
+
[hour, minute, sec] = timePart.split(':').map(Number);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const date = new Date(year, month - 1, day, hour, minute, sec);
|
|
19
|
+
return year !== date.getFullYear() ||
|
|
20
|
+
month !== date.getMonth() + 1 ||
|
|
21
|
+
day !== date.getDate() ||
|
|
22
|
+
hour !== date.getHours() ||
|
|
23
|
+
minute !== date.getMinutes() ||
|
|
24
|
+
sec !== date.getSeconds()
|
|
25
|
+
} catch (error) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Generates a Date object from a string.
|
|
32
|
+
* 文字列からDateオブジェクトを生成します。
|
|
33
|
+
* @param dateString A string representing the date and time (e.g., "2023-10-05 14:30:00")
|
|
34
|
+
* 日付と時間を表す文字列(例: "2023-10-05 14:30:00")
|
|
35
|
+
* @returns Date object
|
|
36
|
+
* Dateオブジェクト
|
|
37
|
+
*/
|
|
38
|
+
static toDateFromString(dateString: string): Date {
|
|
39
|
+
const [datePart, timePart] = dateString.split(' ');
|
|
40
|
+
const [year, month, day] = datePart.split('-').map(Number);
|
|
41
|
+
let [hours, minutes, seconds] = [0, 0, 0];
|
|
42
|
+
if (timePart !== undefined) {
|
|
43
|
+
[hours, minutes, seconds] = timePart.split(':').map(Number);
|
|
44
|
+
}
|
|
45
|
+
return new Date(year, month - 1, day, hours, minutes, seconds);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Formats the specified date.
|
|
50
|
+
* 指定された日付をフォーマットします。
|
|
51
|
+
* @param date The date object to be formatted.
|
|
52
|
+
* フォーマットする対象の日付オブジェクト
|
|
53
|
+
* @param type A string specifying the type of format.
|
|
54
|
+
* フォーマットの種類を指定する文字列
|
|
55
|
+
* @returns A formatted date string.
|
|
56
|
+
* フォーマットされた日付文字列
|
|
57
|
+
*/
|
|
58
|
+
static toStringFromDate(date: Date, type: 'datetime' | 'date' | 'time'): string {
|
|
59
|
+
|
|
60
|
+
const year = date.getFullYear().toString().padStart(4, '0');
|
|
61
|
+
const month = (date.getMonth() + 1).toString().padStart(2, '0');
|
|
62
|
+
const day = date.getDate().toString().padStart(2, '0');
|
|
63
|
+
const hour = date.getHours().toString().padStart(2, '0');
|
|
64
|
+
const minute = date.getMinutes().toString().padStart(2, '0');
|
|
65
|
+
const second = date.getSeconds().toString().padStart(2, '0');
|
|
66
|
+
|
|
67
|
+
switch (type) {
|
|
68
|
+
case 'datetime':
|
|
69
|
+
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
|
|
70
|
+
case 'date':
|
|
71
|
+
return `${year}-${month}-${day}`;
|
|
72
|
+
case 'time':
|
|
73
|
+
return `${hour}:${minute}:${second}`;
|
|
74
|
+
default:
|
|
75
|
+
throw new Error('Invalid type');
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Validates if the given value is in the format YYYY-MM-DD
|
|
81
|
+
* 与えられた値がYYYY-MM-DD形式であるかどうかを検証します
|
|
82
|
+
* @param value - The value to be validated, 検証する値
|
|
83
|
+
* @returns {boolean} - Whether the value is in the format YYYY-MM-DD, 値がYYYY-MM-DD形式であるかどうか
|
|
84
|
+
*/
|
|
85
|
+
static isYYYYMMDD(value: any) {
|
|
86
|
+
if (typeof value !== 'string') {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const pattern = new RegExp('^\\d{4}-\\d{2}-\\d{2}$');
|
|
91
|
+
if (pattern.test(value) === false) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return this.isErrorDateTime(value) === false;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Validates if the given value is in the format YYYY-MM-DD hh:mm:ss
|
|
100
|
+
* 与えられた値がYYYY-MM-DD hh:mm:ss形式であるかどうかを検証します
|
|
101
|
+
* @param value - The value to be validated, 検証する値
|
|
102
|
+
* @returns {boolean} - Whether the value is in the format YYYY-MM-DD hh:mm:ss, 値がYYYY-MM-DD hh:mm:ss形式であるかどうか
|
|
103
|
+
*/
|
|
104
|
+
static isYYYYMMDDhhmiss(value: any) {
|
|
105
|
+
if (typeof value !== 'string') {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const pattern = new RegExp('^\\d{4}-\\d{2}-\\d{2}[ T]\\d{2}:\\d{2}:\\d{2}$');
|
|
110
|
+
if (pattern.test(value) === false) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return this.isErrorDateTime(value) === false;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Validates if the given value is in the format YYYY-MM-DD hh:mm:ss
|
|
119
|
+
* 与えられた値がYYYY-MM-DD hh:mm:ss形式であるかどうかを検証します
|
|
120
|
+
* @param value - The value to be validated, 検証する値
|
|
121
|
+
* @returns {boolean} - Whether the value is in the format YYYY-MM-DD hh:mm:ss, 値がYYYY-MM-DD hh:mm:ss形式であるかどうか
|
|
122
|
+
*/
|
|
123
|
+
static isHHMM(value: any) {
|
|
124
|
+
if (typeof value !== 'string') {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const pattern = new RegExp('^(?:[01]\\d|2[0-3]):[0-5]\\d$');
|
|
129
|
+
return pattern.test(value);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Validates if the given value is in the format HH:MM:SS
|
|
134
|
+
* 与えられた値がHH:MM:SS形式であるかどうかを検証します
|
|
135
|
+
* @param value - The value to be validated, 検証する値
|
|
136
|
+
* @returns {boolean} - Whether the value is in the format HH:MM:SS, 値がHH:MM:SS形式であるかどうか
|
|
137
|
+
*/
|
|
138
|
+
static isHHMMSS(value: any) {
|
|
139
|
+
if (typeof value !== 'string') {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const pattern = new RegExp('^(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$');
|
|
144
|
+
return pattern.test(value);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export default class NumberUtil {
|
|
2
|
+
/**
|
|
3
|
+
* Checks if the given value is a number.
|
|
4
|
+
* 与えられた値が数値であるかどうかを確認します。
|
|
5
|
+
* @param value - The value to be checked. ���認する値。
|
|
6
|
+
* @returns {boolean} - Whether the value is a number. ��が数値であるかどうか。
|
|
7
|
+
*/
|
|
8
|
+
static isNumber(value: any) {
|
|
9
|
+
if (value == null) {
|
|
10
|
+
return false;
|
|
11
|
+
} else if (value instanceof Date) {
|
|
12
|
+
return false;
|
|
13
|
+
} else if (value instanceof Array) {
|
|
14
|
+
return false;
|
|
15
|
+
} else if (typeof(value) == 'string') {
|
|
16
|
+
if (value == "") {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return isNaN(Number(value)) == false;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export default class StringUtil {
|
|
2
|
+
/**
|
|
3
|
+
* Validates if the given value is a valid UUID
|
|
4
|
+
* 与えられた値が有効なUUIDであるかどうかを検証します
|
|
5
|
+
* @param value - The value to be validated, 検証する値
|
|
6
|
+
* @returns {boolean} - Whether the value is a valid UUID, 値が有効なUUIDであるかどうか
|
|
7
|
+
*/
|
|
8
|
+
static isUUID(value: any) {
|
|
9
|
+
if (typeof value !== 'string') {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const pattern = new RegExp('^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$');
|
|
14
|
+
return pattern.test(value);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 小文字スネークからキャピタルケースに変換
|
|
19
|
+
* @param {string} value スネーク文字列
|
|
20
|
+
* @returns キャピタル文字列
|
|
21
|
+
*/
|
|
22
|
+
static formatFromSnakeToCamel(value: string) {
|
|
23
|
+
const regex = /_[a-z]/g;
|
|
24
|
+
let capital = value.replace(regex,
|
|
25
|
+
function(matchChar) {
|
|
26
|
+
return String.fromCharCode(matchChar.charCodeAt(1) - 0x20);
|
|
27
|
+
}
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
// "_1"や"_@"のパターンを考慮して、_を省く(この使い方はあまりないと思いますが...)
|
|
31
|
+
return capital.replaceAll("_", "");
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { TableModel } from "./TableModel";
|
|
2
|
+
import { TOption, TSqlValue } from "./Type";
|
|
3
|
+
|
|
4
|
+
type TError = {code?: string; message?: string;};
|
|
5
|
+
|
|
6
|
+
export default class ValidateClient {
|
|
7
|
+
private model: TableModel;
|
|
8
|
+
constructor(model: TableModel) {
|
|
9
|
+
this.model = model;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public tryDate(value: any, isExcludeTime: boolean = false): Date | false {
|
|
13
|
+
if (value instanceof Date) {
|
|
14
|
+
return value;
|
|
15
|
+
} else if (typeof value !== 'string') {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
const [datePart, timePart] = value.split(' ');
|
|
21
|
+
const [year, month, day] = datePart.split('-').map(Number);
|
|
22
|
+
let [hours, minutes, seconds] = [0, 0, 0];
|
|
23
|
+
if (timePart !== undefined && isExcludeTime === false) {
|
|
24
|
+
[hours, minutes, seconds] = timePart.split(':').map(Number);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// 日付の整合性チェック
|
|
28
|
+
const date = new Date(year, month - 1, day, hours, minutes, seconds);
|
|
29
|
+
if (date.getFullYear() !== year ||
|
|
30
|
+
date.getMonth() + 1 !== month ||
|
|
31
|
+
date.getDate() !== day ||
|
|
32
|
+
date.getHours() !== hours ||
|
|
33
|
+
date.getMinutes() !== minutes ||
|
|
34
|
+
date.getSeconds() !== seconds
|
|
35
|
+
) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return date;
|
|
40
|
+
} catch (ex) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public validateInList(option: TOption, key: string, list: Array<TSqlValue>, error?: TError) {
|
|
46
|
+
const column = this.model.getColumn(key);
|
|
47
|
+
const value = option[key];
|
|
48
|
+
if (value === undefined || value === null || value === "") {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (list.includes(value) === false) {
|
|
53
|
+
const code = error?.code ?? "000";
|
|
54
|
+
let message = error?.message;
|
|
55
|
+
if (message === undefined) {
|
|
56
|
+
message = `{column} must be one of the items in the {list}. ({value})`;
|
|
57
|
+
}
|
|
58
|
+
message = message.replace('{column}', column.alias ?? column.columnName);
|
|
59
|
+
message = message.replace('{value}', value.toString());
|
|
60
|
+
message = message.replace('{list}', list.join(', '));
|
|
61
|
+
this.model.throwValidationError(code, message);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public validateUnderNow(option: TOption, key: string, error?: TError) {
|
|
66
|
+
const column = this.model.getColumn(key);
|
|
67
|
+
const value = option[key];
|
|
68
|
+
if (value === undefined || value === null || value === "") {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const date = this.tryDate(value);
|
|
73
|
+
if (date === false) {
|
|
74
|
+
throw new Error("The value must be a Date or a valid date string when using validateUnderNow.");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const now = new Date();
|
|
78
|
+
if (date > now) {
|
|
79
|
+
const code = error?.code ?? "000";
|
|
80
|
+
let message = error?.message;
|
|
81
|
+
if (message === undefined) {
|
|
82
|
+
message = `{column} should be entered on or before now. ({value})`;
|
|
83
|
+
}
|
|
84
|
+
message = message.replace('{column}', column.alias ?? column.columnName);
|
|
85
|
+
message = message.replace('{value}', value.toString());
|
|
86
|
+
this.model.throwValidationError(code, message);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
public validateUnderToday(option: TOption, key: string, isErrorToday: boolean, error?: TError): void {
|
|
91
|
+
const column = this.model.getColumn(key);
|
|
92
|
+
const value = option[key];
|
|
93
|
+
if (value === undefined || value === null || value === "") {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const date = this.tryDate(value);
|
|
98
|
+
if (date === false) {
|
|
99
|
+
throw new Error("The value must be a Date or a valid date string when using validateUnderToday.");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
date.setHours(0);
|
|
103
|
+
date.setMinutes(0);
|
|
104
|
+
date.setSeconds(0);
|
|
105
|
+
date.setMilliseconds(0);
|
|
106
|
+
|
|
107
|
+
const today = new Date();
|
|
108
|
+
today.setHours(0);
|
|
109
|
+
today.setMinutes(0);
|
|
110
|
+
today.setSeconds(0);
|
|
111
|
+
today.setMilliseconds(isErrorToday ? 0 : 1);
|
|
112
|
+
|
|
113
|
+
if (date >= today) {
|
|
114
|
+
const code = error?.code ?? "000";
|
|
115
|
+
let message = error?.message;
|
|
116
|
+
if (message === undefined) {
|
|
117
|
+
if (isErrorToday) {
|
|
118
|
+
message = `{column} should be entered before today. ({value})`;
|
|
119
|
+
} else {
|
|
120
|
+
message = `{column} should be entered on or before today. ({value})`;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
message = message.replace('{column}', column.alias ?? column.columnName);
|
|
124
|
+
message = message.replace('{value}', value.toString());
|
|
125
|
+
this.model.throwValidationError(code, message);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
public validateRegExp(option: TOption, key: string, regExp: RegExp | string, error?: TError): void {
|
|
130
|
+
const column = this.model.getColumn(key);
|
|
131
|
+
const value = option[key];
|
|
132
|
+
if (value === undefined || value === null || value === "") {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (typeof value !== 'string') {
|
|
137
|
+
throw new Error("The value must be a string when using validateStringRegExp.");
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (typeof regExp === 'string') {
|
|
141
|
+
regExp = new RegExp(regExp);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (regExp.test(value) === false) {
|
|
145
|
+
const code = error?.code ?? "000";
|
|
146
|
+
let message = error?.message;
|
|
147
|
+
if (message === undefined) {
|
|
148
|
+
message = `{column} is invalid. ({value})`;
|
|
149
|
+
}
|
|
150
|
+
message = message.replace('{column}', column.alias ?? column.columnName);
|
|
151
|
+
message = message.replace('{value}', value.toString());
|
|
152
|
+
this.model.throwValidationError(code, message);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
public validatePositiveNumber(option: TOption, key: string, error?: TError) {
|
|
157
|
+
const column = this.model.getColumn(key);
|
|
158
|
+
const value = option[key];
|
|
159
|
+
if (value === undefined || value === null || value === "") {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (typeof value !== 'number' && typeof value !== 'string') {
|
|
164
|
+
throw new Error("The value must be a valid number string or number when using validatePositiveNumber.");
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (isNaN(Number(value))) {
|
|
168
|
+
throw new Error("The value must be a valid number string or number when using validatePositiveNumber.");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (Number(value) <= 0) {
|
|
172
|
+
const code = error?.code ?? "000";
|
|
173
|
+
let message = error?.message;
|
|
174
|
+
if (message === undefined) {
|
|
175
|
+
message = `Please enter a value greater than 0 for {column}. ({value})`;
|
|
176
|
+
}
|
|
177
|
+
message = message.replace('{column}', column.alias ?? column.columnName);
|
|
178
|
+
message = message.replace('{value}', value.toString());
|
|
179
|
+
this.model.throwValidationError(code, message);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|