koatty_validation 1.3.1 → 1.3.4

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/dist/README.md CHANGED
@@ -1,116 +1,116 @@
1
- # koatty_validation
2
- Validation Util for Koatty. Based on class-validator, extended parameter type checking and restricted attribute functions.
3
-
4
-
5
- # User Decorators
6
-
7
- * @IsDefined
8
- * @IsCnName
9
- * @IsIdNumber
10
- * @IsZipCode
11
- * @IsMobile
12
- * @IsPlateNumber
13
- * @IsEmail
14
- * @IsIP
15
- * @IsPhoneNumber
16
- * @IsUrl
17
- * @IsHash
18
- * @IsNotEmpty
19
- * @Equals
20
- * @NotEquals
21
- * @Contains
22
- * @IsIn
23
- * @IsNotIn
24
- * @IsDate
25
- * @Gt
26
- * @Gte
27
- * @Lt
28
- * @Lte
29
-
30
- * @Valid
31
- * @Validated
32
-
33
-
34
-
35
- ```js
36
-
37
- export class Controller {
38
-
39
- Test(@Valid("IsNotEmpty", "can not be empty!!") id: number){
40
- //todo
41
- }
42
-
43
- @Validated() // use dto validation
44
- TestDto(user: UserDTO) {
45
-
46
- }
47
- }
48
-
49
- export class UserDTO {
50
- @IsNotEmpty({ message: "can not be empty!!" })
51
- phoneNum: string;
52
-
53
- @IsCnName({ message: "must be cn name"})
54
- userName: string;
55
- }
56
-
57
- ```
58
-
59
- # Validator for manual
60
-
61
- ## FunctionValidator
62
-
63
- * IsCnName
64
- * IsIdNumber
65
- * IsZipCode
66
- * IsMobile
67
- * IsPlateNumber
68
- * IsEmail
69
- * IsIP
70
- * IsPhoneNumber
71
- * IsUrl
72
- * IsHash
73
- * IsNotEmpty
74
- * Equals
75
- * NotEquals
76
- * Contains
77
- * IsIn
78
- * IsNotIn
79
- * IsDate
80
- * Gt
81
- * Gte
82
- * Lt
83
- * Lte
84
-
85
- exp:
86
-
87
- ```js
88
- const str = "";
89
- // throw Error
90
- FunctionValidator.IsNotEmpty(str, "cannot be empty");
91
- FunctionValidator.Contains(str, {message: "must contain s", value: "s"});
92
- //
93
- if (!ValidFuncs.IsNotEmpty(str)) {
94
- console.log("empty");
95
- }
96
- ```
97
-
98
- ## ClassValidator
99
-
100
- exp:
101
-
102
- ```js
103
- class SchemaClass {
104
- @IsDefined
105
- id: number;
106
-
107
- @IsNotEmpty
108
- name: string;
109
- }
110
-
111
-
112
- ClassValidator.valid(SchemaClass, {name: ''}).catch(err => {
113
- console.log(err);
114
- })
115
- ```
116
-
1
+ # koatty_validation
2
+ Validation Util for Koatty. Based on class-validator, extended parameter type checking and restricted attribute functions.
3
+
4
+
5
+ # User Decorators
6
+
7
+ * @IsDefined
8
+ * @IsCnName
9
+ * @IsIdNumber
10
+ * @IsZipCode
11
+ * @IsMobile
12
+ * @IsPlateNumber
13
+ * @IsEmail
14
+ * @IsIP
15
+ * @IsPhoneNumber
16
+ * @IsUrl
17
+ * @IsHash
18
+ * @IsNotEmpty
19
+ * @Equals
20
+ * @NotEquals
21
+ * @Contains
22
+ * @IsIn
23
+ * @IsNotIn
24
+ * @IsDate
25
+ * @Gt
26
+ * @Gte
27
+ * @Lt
28
+ * @Lte
29
+
30
+ * @Valid
31
+ * @Validated
32
+
33
+
34
+
35
+ ```js
36
+
37
+ export class Controller {
38
+
39
+ Test(@Valid("IsNotEmpty", "can not be empty!!") id: number){
40
+ //todo
41
+ }
42
+
43
+ @Validated() // use dto validation
44
+ TestDto(user: UserDTO) {
45
+
46
+ }
47
+ }
48
+
49
+ export class UserDTO {
50
+ @IsNotEmpty({ message: "can not be empty!!" })
51
+ phoneNum: string;
52
+
53
+ @IsCnName({ message: "must be cn name"})
54
+ userName: string;
55
+ }
56
+
57
+ ```
58
+
59
+ # Validator for manual
60
+
61
+ ## FunctionValidator
62
+
63
+ * IsCnName
64
+ * IsIdNumber
65
+ * IsZipCode
66
+ * IsMobile
67
+ * IsPlateNumber
68
+ * IsEmail
69
+ * IsIP
70
+ * IsPhoneNumber
71
+ * IsUrl
72
+ * IsHash
73
+ * IsNotEmpty
74
+ * Equals
75
+ * NotEquals
76
+ * Contains
77
+ * IsIn
78
+ * IsNotIn
79
+ * IsDate
80
+ * Gt
81
+ * Gte
82
+ * Lt
83
+ * Lte
84
+
85
+ exp:
86
+
87
+ ```js
88
+ const str = "";
89
+ // throw Error
90
+ FunctionValidator.IsNotEmpty(str, "cannot be empty");
91
+ FunctionValidator.Contains(str, {message: "must contain s", value: "s"});
92
+ //
93
+ if (!ValidFuncs.IsNotEmpty(str)) {
94
+ console.log("empty");
95
+ }
96
+ ```
97
+
98
+ ## ClassValidator
99
+
100
+ exp:
101
+
102
+ ```js
103
+ class SchemaClass {
104
+ @IsDefined
105
+ id: number;
106
+
107
+ @IsNotEmpty
108
+ name: string;
109
+ }
110
+
111
+
112
+ ClassValidator.valid(SchemaClass, {name: ''}).catch(err => {
113
+ console.log(err);
114
+ })
115
+ ```
116
+
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2024-01-08 07:08:42
3
+ * @Date: 2024-10-31 17:03:02
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
package/dist/index.js CHANGED
@@ -1,16 +1,16 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2024-01-08 07:08:31
3
+ * @Date: 2024-10-31 17:02:42
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
7
7
  */
8
8
  'use strict';
9
9
 
10
+ var classValidator = require('class-validator');
10
11
  var helper = require('koatty_lib');
11
- require('reflect-metadata');
12
12
  var koatty_container = require('koatty_container');
13
- var classValidator = require('class-validator');
13
+ require('reflect-metadata');
14
14
 
15
15
  function _interopNamespaceDefault(e) {
16
16
  var n = Object.create(null);
@@ -204,6 +204,7 @@ function convertParamsType(param, type) {
204
204
  default: //any
205
205
  return param;
206
206
  }
207
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
207
208
  }
208
209
  catch (err) {
209
210
  return param;
@@ -353,7 +354,7 @@ function plateNumber(value) {
353
354
  * @Usage:
354
355
  * @Author: richen
355
356
  * @Date: 2021-11-25 10:47:04
356
- * @LastEditTime: 2024-01-03 14:32:49
357
+ * @LastEditTime: 2024-10-31 16:57:28
357
358
  */
358
359
  // constant
359
360
  const PARAM_TYPE_KEY = 'PARAM_TYPE_KEY';
@@ -412,20 +413,10 @@ class ValidateClass {
412
413
  * @memberof ValidateClass
413
414
  */
414
415
  async valid(Clazz, data, convert = false) {
415
- let obj = {};
416
- if (data instanceof Clazz) {
417
- obj = data;
418
- }
419
- else {
420
- obj = plainToClass(Clazz, data, convert);
421
- }
422
- let errors = [];
423
- if (convert) {
424
- errors = await classValidator.validate(obj);
425
- }
426
- else {
427
- errors = await classValidator.validate(obj, { skipMissingProperties: true });
428
- }
416
+ const obj = data instanceof Clazz ? data : plainToClass(Clazz, data, convert);
417
+ const errors = convert
418
+ ? await classValidator.validate(obj)
419
+ : await classValidator.validate(obj, { skipMissingProperties: true });
429
420
  if (errors.length > 0) {
430
421
  throw new Error(Object.values(errors[0].constraints)[0]);
431
422
  }
@@ -598,71 +589,7 @@ const ValidFuncs = {
598
589
  * @param {(string | ValidOtpions)} [options]
599
590
  * @returns {*}
600
591
  */
601
- const FunctionValidator = {
602
- IsNotEmpty: function (value, options) {
603
- throw new Error("Function not implemented.");
604
- },
605
- IsDate: function (value, options) {
606
- throw new Error("Function not implemented.");
607
- },
608
- IsEmail: function (value, options) {
609
- throw new Error("Function not implemented.");
610
- },
611
- IsIP: function (value, options) {
612
- throw new Error("Function not implemented.");
613
- },
614
- IsPhoneNumber: function (value, options) {
615
- throw new Error("Function not implemented.");
616
- },
617
- IsUrl: function (value, options) {
618
- throw new Error("Function not implemented.");
619
- },
620
- IsHash: function (value, options) {
621
- throw new Error("Function not implemented.");
622
- },
623
- IsCnName: function (value, options) {
624
- throw new Error("Function not implemented.");
625
- },
626
- IsIdNumber: function (value, options) {
627
- throw new Error("Function not implemented.");
628
- },
629
- IsZipCode: function (value, options) {
630
- throw new Error("Function not implemented.");
631
- },
632
- IsMobile: function (value, options) {
633
- throw new Error("Function not implemented.");
634
- },
635
- IsPlateNumber: function (value, options) {
636
- throw new Error("Function not implemented.");
637
- },
638
- Equals: function (value, options) {
639
- throw new Error("Function not implemented.");
640
- },
641
- NotEquals: function (value, options) {
642
- throw new Error("Function not implemented.");
643
- },
644
- Contains: function (value, options) {
645
- throw new Error("Function not implemented.");
646
- },
647
- IsIn: function (value, options) {
648
- throw new Error("Function not implemented.");
649
- },
650
- IsNotIn: function (value, options) {
651
- throw new Error("Function not implemented.");
652
- },
653
- Gt: function (value, options) {
654
- throw new Error("Function not implemented.");
655
- },
656
- Lt: function (value, options) {
657
- throw new Error("Function not implemented.");
658
- },
659
- Gte: function (value, options) {
660
- throw new Error("Function not implemented.");
661
- },
662
- Lte: function (value, options) {
663
- throw new Error("Function not implemented.");
664
- }
665
- };
592
+ const FunctionValidator = {};
666
593
  Object.keys(ValidFuncs).forEach((key) => {
667
594
  FunctionValidator[key] = (value, options) => {
668
595
  if (helper__namespace.isString(options)) {
@@ -679,7 +606,7 @@ Object.keys(ValidFuncs).forEach((key) => {
679
606
  * @Usage:
680
607
  * @Author: richen
681
608
  * @Date: 2021-11-25 10:46:57
682
- * @LastEditTime: 2024-01-03 11:30:55
609
+ * @LastEditTime: 2024-10-31 16:49:26
683
610
  */
684
611
  /**
685
612
  * Validation parameter's type and values.
@@ -722,7 +649,7 @@ function Valid(rule, options) {
722
649
  * @returns {MethodDecorator}
723
650
  */
724
651
  function Validated() {
725
- return (target, propertyKey, descriptor) => {
652
+ return (target, propertyKey, _descriptor) => {
726
653
  //
727
654
  koatty_container.IOCContainer.savePropertyData(PARAM_CHECK_KEY, {
728
655
  dtoCheck: 1
@@ -794,10 +721,10 @@ function IsCnName(validationOptions) {
794
721
  propertyName,
795
722
  options: validationOptions,
796
723
  validator: {
797
- validate(value, args) {
724
+ validate(value, _args) {
798
725
  return cnName(value);
799
726
  },
800
- defaultMessage(args) {
727
+ defaultMessage(_args) {
801
728
  return "invalid parameter ($property).";
802
729
  }
803
730
  }
@@ -821,10 +748,10 @@ function IsIdNumber(validationOptions) {
821
748
  propertyName,
822
749
  options: validationOptions,
823
750
  validator: {
824
- validate(value, args) {
751
+ validate(value, _args) {
825
752
  return idNumber(value);
826
753
  },
827
- defaultMessage(args) {
754
+ defaultMessage(_args) {
828
755
  return "invalid parameter ($property).";
829
756
  }
830
757
  }
@@ -848,10 +775,10 @@ function IsZipCode(validationOptions) {
848
775
  propertyName,
849
776
  options: validationOptions,
850
777
  validator: {
851
- validate(value, args) {
778
+ validate(value, _args) {
852
779
  return zipCode(value);
853
780
  },
854
- defaultMessage(args) {
781
+ defaultMessage(_args) {
855
782
  return "invalid parameter ($property).";
856
783
  }
857
784
  }
@@ -875,10 +802,10 @@ function IsMobile(validationOptions) {
875
802
  propertyName,
876
803
  options: validationOptions,
877
804
  validator: {
878
- validate(value, args) {
805
+ validate(value, _args) {
879
806
  return mobile(value);
880
807
  },
881
- defaultMessage(args) {
808
+ defaultMessage(_args) {
882
809
  return "invalid parameter ($property).";
883
810
  }
884
811
  }
@@ -902,10 +829,10 @@ function IsPlateNumber(validationOptions) {
902
829
  propertyName,
903
830
  options: validationOptions,
904
831
  validator: {
905
- validate(value, args) {
832
+ validate(value, _args) {
906
833
  return plateNumber(value);
907
834
  },
908
- defaultMessage(args) {
835
+ defaultMessage(_args) {
909
836
  return "invalid parameter ($property).";
910
837
  }
911
838
  }
@@ -928,10 +855,10 @@ function IsNotEmpty(validationOptions) {
928
855
  propertyName,
929
856
  options: validationOptions,
930
857
  validator: {
931
- validate(value, args) {
858
+ validate(value, _args) {
932
859
  return !helper__namespace.isEmpty(value);
933
860
  },
934
- defaultMessage(args) {
861
+ defaultMessage(_args) {
935
862
  return "invalid parameter ($property).";
936
863
  }
937
864
  }
@@ -955,10 +882,10 @@ function Equals(comparison, validationOptions) {
955
882
  propertyName,
956
883
  options: validationOptions,
957
884
  validator: {
958
- validate(value, args) {
885
+ validate(value, _args) {
959
886
  return classValidator.equals(value, comparison);
960
887
  },
961
- defaultMessage(args) {
888
+ defaultMessage(_args) {
962
889
  return `invalid parameter, ($property) must be equals ${comparison}.`;
963
890
  }
964
891
  }
@@ -982,10 +909,10 @@ function NotEquals(comparison, validationOptions) {
982
909
  propertyName,
983
910
  options: validationOptions,
984
911
  validator: {
985
- validate(value, args) {
912
+ validate(value, _args) {
986
913
  return classValidator.notEquals(value, comparison);
987
914
  },
988
- defaultMessage(args) {
915
+ defaultMessage(_args) {
989
916
  return `invalid parameter, ($property) must be not equals ${comparison}.`;
990
917
  }
991
918
  }
@@ -1009,11 +936,11 @@ function Contains(seed, validationOptions) {
1009
936
  propertyName,
1010
937
  options: validationOptions,
1011
938
  validator: {
1012
- validate(value, args) {
939
+ validate(value, _args) {
1013
940
  return classValidator.contains(value, seed);
1014
941
  // return typeof value === "string" && (value.indexOf(seed) > -1);
1015
942
  },
1016
- defaultMessage(args) {
943
+ defaultMessage(_args) {
1017
944
  return `invalid parameter, ($property) must be contains ${seed}.`;
1018
945
  }
1019
946
  }
@@ -1037,10 +964,10 @@ function IsIn(possibleValues, validationOptions) {
1037
964
  propertyName,
1038
965
  options: validationOptions,
1039
966
  validator: {
1040
- validate(value, args) {
967
+ validate(value, _args) {
1041
968
  return classValidator.isIn(value, possibleValues);
1042
969
  },
1043
- defaultMessage(args) {
970
+ defaultMessage(_args) {
1044
971
  return `invalid parameter ($property).`;
1045
972
  }
1046
973
  }
@@ -1064,10 +991,10 @@ function IsNotIn(possibleValues, validationOptions) {
1064
991
  propertyName,
1065
992
  options: validationOptions,
1066
993
  validator: {
1067
- validate(value, args) {
994
+ validate(value, _args) {
1068
995
  return classValidator.isNotIn(value, possibleValues);
1069
996
  },
1070
- defaultMessage(args) {
997
+ defaultMessage(_args) {
1071
998
  return `invalid parameter ($property).`;
1072
999
  }
1073
1000
  }
@@ -1090,10 +1017,10 @@ function IsDate(validationOptions) {
1090
1017
  propertyName,
1091
1018
  options: validationOptions,
1092
1019
  validator: {
1093
- validate(value, args) {
1020
+ validate(value, _args) {
1094
1021
  return classValidator.isDate(value);
1095
1022
  },
1096
- defaultMessage(args) {
1023
+ defaultMessage(_args) {
1097
1024
  return `invalid parameter ($property).`;
1098
1025
  }
1099
1026
  }
@@ -1117,10 +1044,10 @@ function Gt(min, validationOptions) {
1117
1044
  propertyName,
1118
1045
  options: validationOptions,
1119
1046
  validator: {
1120
- validate(value, args) {
1047
+ validate(value, _args) {
1121
1048
  return helper__namespace.toNumber(value) > min;
1122
1049
  },
1123
- defaultMessage(args) {
1050
+ defaultMessage(_args) {
1124
1051
  return `invalid parameter ($property).`;
1125
1052
  }
1126
1053
  }
@@ -1144,10 +1071,10 @@ function Lt(max, validationOptions) {
1144
1071
  propertyName,
1145
1072
  options: validationOptions,
1146
1073
  validator: {
1147
- validate(value, args) {
1074
+ validate(value, _args) {
1148
1075
  return helper__namespace.toNumber(value) < max;
1149
1076
  },
1150
- defaultMessage(args) {
1077
+ defaultMessage(_args) {
1151
1078
  return `invalid parameter ($property).`;
1152
1079
  }
1153
1080
  }
@@ -1171,10 +1098,10 @@ function Gte(min, validationOptions) {
1171
1098
  propertyName,
1172
1099
  options: validationOptions,
1173
1100
  validator: {
1174
- validate(value, args) {
1101
+ validate(value, _args) {
1175
1102
  return helper__namespace.toNumber(value) >= min;
1176
1103
  },
1177
- defaultMessage(args) {
1104
+ defaultMessage(_args) {
1178
1105
  return `invalid parameter ($property).`;
1179
1106
  }
1180
1107
  }
@@ -1198,10 +1125,10 @@ function Lte(max, validationOptions) {
1198
1125
  propertyName,
1199
1126
  options: validationOptions,
1200
1127
  validator: {
1201
- validate(value, args) {
1128
+ validate(value, _args) {
1202
1129
  return helper__namespace.toNumber(value) <= max;
1203
1130
  },
1204
- defaultMessage(args) {
1131
+ defaultMessage(_args) {
1205
1132
  return `invalid parameter ($property).`;
1206
1133
  }
1207
1134
  }
@@ -1227,10 +1154,10 @@ function Length(min, max, validationOptions) {
1227
1154
  propertyName,
1228
1155
  options: validationOptions,
1229
1156
  validator: {
1230
- validate(value, args) {
1157
+ validate(value, _args) {
1231
1158
  return classValidator.length(value, min, max);
1232
1159
  },
1233
- defaultMessage(args) {
1160
+ defaultMessage(_args) {
1234
1161
  return `invalid parameter ($property).`;
1235
1162
  }
1236
1163
  }
@@ -1254,10 +1181,10 @@ function IsEmail(options, validationOptions) {
1254
1181
  propertyName,
1255
1182
  options: validationOptions,
1256
1183
  validator: {
1257
- validate(value, args) {
1184
+ validate(value, _args) {
1258
1185
  return classValidator.isEmail(value);
1259
1186
  },
1260
- defaultMessage(args) {
1187
+ defaultMessage(_args) {
1261
1188
  return `invalid parameter ($property).`;
1262
1189
  }
1263
1190
  }
@@ -1281,10 +1208,10 @@ function IsIP(version, validationOptions) {
1281
1208
  propertyName,
1282
1209
  options: validationOptions,
1283
1210
  validator: {
1284
- validate(value, args) {
1211
+ validate(value, _args) {
1285
1212
  return classValidator.isIP(value, version);
1286
1213
  },
1287
- defaultMessage(args) {
1214
+ defaultMessage(_args) {
1288
1215
  return `invalid parameter ($property).`;
1289
1216
  }
1290
1217
  }
@@ -1311,10 +1238,10 @@ function IsPhoneNumber(region, validationOptions) {
1311
1238
  propertyName,
1312
1239
  options: validationOptions,
1313
1240
  validator: {
1314
- validate(value, args) {
1241
+ validate(value, _args) {
1315
1242
  return classValidator.isPhoneNumber(value, region);
1316
1243
  },
1317
- defaultMessage(args) {
1244
+ defaultMessage(_args) {
1318
1245
  return `invalid parameter ($property).`;
1319
1246
  }
1320
1247
  }
@@ -1338,10 +1265,10 @@ function IsUrl(options, validationOptions) {
1338
1265
  propertyName,
1339
1266
  options: validationOptions,
1340
1267
  validator: {
1341
- validate(value, args) {
1268
+ validate(value, _args) {
1342
1269
  return classValidator.isURL(value, options);
1343
1270
  },
1344
- defaultMessage(args) {
1271
+ defaultMessage(_args) {
1345
1272
  return `invalid parameter ($property).`;
1346
1273
  }
1347
1274
  }
@@ -1366,10 +1293,10 @@ function IsHash(algorithm, validationOptions) {
1366
1293
  propertyName,
1367
1294
  options: validationOptions,
1368
1295
  validator: {
1369
- validate(value, args) {
1296
+ validate(value, _args) {
1370
1297
  return classValidator.isHash(value, algorithm);
1371
1298
  },
1372
- defaultMessage(args) {
1299
+ defaultMessage(_args) {
1373
1300
  return `invalid parameter, ($property) must be is an ${algorithm} Hash string.`;
1374
1301
  }
1375
1302
  }
@@ -1391,10 +1318,10 @@ function CheckFunc(func, validationOptions) {
1391
1318
  propertyName,
1392
1319
  options: validationOptions,
1393
1320
  validator: {
1394
- validate(value, args) {
1321
+ validate(value, _args) {
1395
1322
  return func(value);
1396
1323
  },
1397
- defaultMessage(args) {
1324
+ defaultMessage(_args) {
1398
1325
  return `invalid parameter ($property).`;
1399
1326
  }
1400
1327
  }