koatty_validation 1.2.10 → 1.3.1

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/CHANGELOG.md CHANGED
@@ -2,6 +2,26 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [1.3.1](https://github.com/koatty/koatty_validation/compare/v1.3.0...v1.3.1) (2024-01-07)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * comment ([d898cf6](https://github.com/koatty/koatty_validation/commit/d898cf68bc8693f78155b7208b419dc4b11dffb4))
11
+ * dto赋值类型 ([5a1d68d](https://github.com/koatty/koatty_validation/commit/5a1d68dea0b67e988427c54abae9bfe76a6eaf48))
12
+
13
+ ## [1.3.0](https://github.com/koatty/koatty_validation/compare/v1.2.10...v1.3.0) (2024-01-03)
14
+
15
+
16
+ ### Features
17
+
18
+ * 增加@CheckFunc ([d5bdc2d](https://github.com/koatty/koatty_validation/commit/d5bdc2d51cdbf6d558e376bfda51e126158d4e75))
19
+
20
+
21
+ ### Bug Fixes
22
+
23
+ * export ValidFuncs ([7d42023](https://github.com/koatty/koatty_validation/commit/7d42023138e9577c9646423c4c38faffba46fb10))
24
+
5
25
  ### [1.2.10](https://github.com/koatty/koatty_validation/compare/v1.2.9...v1.2.10) (2023-12-16)
6
26
 
7
27
 
package/README.md CHANGED
@@ -89,6 +89,10 @@ const str = "";
89
89
  // throw Error
90
90
  FunctionValidator.IsNotEmpty(str, "cannot be empty");
91
91
  FunctionValidator.Contains(str, {message: "must contain s", value: "s"});
92
+ //
93
+ if (!ValidFuncs.IsNotEmpty(str)) {
94
+ console.log("empty");
95
+ }
92
96
  ```
93
97
 
94
98
  ## ClassValidator
package/dist/README.md CHANGED
@@ -89,6 +89,10 @@ const str = "";
89
89
  // throw Error
90
90
  FunctionValidator.IsNotEmpty(str, "cannot be empty");
91
91
  FunctionValidator.Contains(str, {message: "must contain s", value: "s"});
92
+ //
93
+ if (!ValidFuncs.IsNotEmpty(str)) {
94
+ console.log("empty");
95
+ }
92
96
  ```
93
97
 
94
98
  ## ClassValidator
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2023-12-16 14:43:06
3
+ * @Date: 2024-01-08 07:08:42
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -9,6 +9,14 @@ import { CountryCode } from 'libphonenumber-js';
9
9
  import { IsIpVersion } from 'class-validator';
10
10
  import { ValidationOptions } from 'class-validator';
11
11
 
12
+ /**
13
+ * Use a custom function for validation
14
+ * @param func
15
+ * @param validationOptions
16
+ * @returns
17
+ */
18
+ export declare function CheckFunc(func: (value: unknown) => boolean, validationOptions?: ValidationOptions): PropertyDecorator;
19
+
12
20
  /**
13
21
  * Check the base types.
14
22
  *
@@ -126,7 +134,7 @@ export declare function IsCnName(validationOptions?: ValidationOptions): Propert
126
134
  export declare function IsDate(validationOptions?: ValidationOptions): PropertyDecorator;
127
135
 
128
136
  /**
129
- * Identifies that the field needs to be defined
137
+ * Alias of Expose
130
138
  *
131
139
  * @export
132
140
  * @returns {PropertyDecorator}
@@ -405,6 +413,103 @@ declare class ValidateClass {
405
413
  */
406
414
  export declare function Validated(): MethodDecorator;
407
415
 
416
+ /**
417
+ * Validator Functions
418
+ */
419
+ export declare const ValidFuncs: {
420
+ /**
421
+ * Checks value is not empty, undefined, null, '', NaN, [], {} and any empty string(including spaces,
422
+ * tabs, formfeeds, etc.), returns false
423
+ */
424
+ IsNotEmpty: (value: unknown) => boolean;
425
+ /**
426
+ * Checks if a given value is a real date.
427
+ */
428
+ IsDate: (value: unknown) => boolean;
429
+ /**
430
+ * Checks if the string is an email. If given value is not a string, then it returns false.
431
+ */
432
+ IsEmail: (value: unknown, options?: IsEmailOptions) => boolean;
433
+ /**
434
+ * Checks if the string is an IP (version 4 or 6). If given value is not a string, then it returns false.
435
+ */
436
+ IsIP: (value: unknown, version?: any) => boolean;
437
+ /**
438
+ * Checks if the string is a valid phone number.
439
+ * @param value — the potential phone number string to test
440
+ * @param region 2 characters uppercase country code (e.g. DE, US, CH). If users must enter the intl.
441
+ * prefix (e.g. +41), then you may pass "ZZ" or null as region.
442
+ * See [google-libphonenumber, metadata.js:countryCodeToRegionCodeMap on github]
443
+ * {@link https://github.com/ruimarinho/google-libphonenumber/blob/1e46138878cff479aafe2ce62175c6c49cb58720/src/metadata.js#L33}
444
+ */
445
+ IsPhoneNumber: (value: string, region?: CountryCode) => boolean;
446
+ /**
447
+ * Checks if the string is an url. If given value is not a string, then it returns false.
448
+ */
449
+ IsUrl: (value: string, options?: IsURLOptions) => boolean;
450
+ /**
451
+ * check if the string is a hash of type algorithm. Algorithm is one of
452
+ * ['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']
453
+ */
454
+ IsHash: (value: unknown, algorithm: HashAlgorithm) => boolean;
455
+ /**
456
+ * Checks if value is a chinese name.
457
+ */
458
+ IsCnName: (value: any) => boolean;
459
+ /**
460
+ * Checks if value is a idcard number.
461
+ */
462
+ IsIdNumber: (value: any) => boolean;
463
+ /**
464
+ * Checks if value is a zipCode.
465
+ */
466
+ IsZipCode: (value: any) => boolean;
467
+ /**
468
+ * Checks if value is a mobile phone number.
469
+ */
470
+ IsMobile: (value: any) => boolean;
471
+ /**
472
+ * Checks if value is a plateNumber.
473
+ */
474
+ IsPlateNumber: (value: any) => boolean;
475
+ /**
476
+ * Checks if value matches ("===") the comparison.
477
+ */
478
+ Equals: (value: unknown, comparison: unknown) => boolean;
479
+ /**
480
+ * Checks if value does not match ("!==") the comparison.
481
+ */
482
+ NotEquals: (value: unknown, comparison: unknown) => boolean;
483
+ /**
484
+ * Checks if the string contains the seed. If given value is not a string, then it returns false.
485
+ */
486
+ Contains: (value: unknown, seed: string) => boolean;
487
+ /**
488
+ * Checks if given value is in a array of allowed values.
489
+ */
490
+ IsIn: (value: unknown, possibleValues: unknown[]) => boolean;
491
+ /**
492
+ * Checks if given value not in a array of allowed values.
493
+ */
494
+ IsNotIn: (value: unknown, possibleValues: unknown[]) => boolean;
495
+ /**
496
+ * Checks if the first number is greater than or equal to the second.
497
+ */
498
+ Gt: (num: unknown, min: number) => boolean;
499
+ /**
500
+ * Checks if the first number is less than or equal to the second.
501
+ */
502
+ Lt: (num: unknown, max: number) => boolean;
503
+ /**
504
+ * Checks if the first number is greater than or equal to the second.
505
+ */
506
+ Gte: (num: unknown, min: number) => boolean;
507
+ /**
508
+ * Checks if the first number is less than or equal to the second.
509
+ */
510
+ Lte: (num: unknown, max: number) => boolean;
511
+ };
512
+
408
513
  export declare type ValidOtpions = {
409
514
  message: string;
410
515
  value: any;
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2023-12-16 14:42:53
3
+ * @Date: 2024-01-08 07:08:31
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -83,7 +83,12 @@ function plainToClass(clazz, data, convert = false) {
83
83
  function assignDtoParams(clazz, data, convert = false) {
84
84
  const cls = Reflect.construct(clazz, []);
85
85
  if (convert) {
86
- return convertAssignDtoParams(clazz, cls, data);
86
+ const metaData = getDtoParamsMeta(clazz, cls);
87
+ for (const [key, type] of metaData) {
88
+ if (key && data[key] !== undefined) {
89
+ cls[key] = convertParamsType(data[key], type);
90
+ }
91
+ }
87
92
  }
88
93
  else {
89
94
  for (const key in cls) {
@@ -92,34 +97,30 @@ function assignDtoParams(clazz, data, convert = false) {
92
97
  cls[key] = data[key];
93
98
  }
94
99
  }
95
- return cls;
96
100
  }
101
+ return cls;
97
102
  }
98
103
  /**
99
- * convert type and assign dto params
104
+ * get class prototype type def.
100
105
  * @param clazz
101
106
  * @param cls
102
- * @param data
103
107
  * @returns
104
108
  */
105
- function convertAssignDtoParams(clazz, cls, data) {
106
- if (Object.prototype.hasOwnProperty.call(cls, "_typeDef")) {
107
- for (const key in cls) {
108
- if (Object.prototype.hasOwnProperty.call(cls._typeDef, key) &&
109
- data[key] !== undefined) {
110
- cls[key] = convertParamsType(data[key], cls._typeDef[key]);
111
- }
112
- }
109
+ function getDtoParamsMeta(clazz, cls) {
110
+ // Non-own properties are inherited from the prototype chain,
111
+ // ensure that properties are not polluted
112
+ if (!Object.prototype.hasOwnProperty.call(cls, "_typeDef") &&
113
+ ("_typeDef" in cls)) {
114
+ return cls._typeDef;
113
115
  }
114
- else {
115
- const originMap = koatty_container.getOriginMetadata(PARAM_TYPE_KEY, clazz);
116
- for (const [key, type] of originMap) {
117
- if (key && data[key] !== undefined) {
118
- cls[key] = convertParamsType(data[key], type);
119
- }
120
- }
121
- }
122
- return cls;
116
+ const typeDef = koatty_container.getOriginMetadata(PARAM_TYPE_KEY, clazz);
117
+ Reflect.defineProperty(clazz.prototype, "_typeDef", {
118
+ enumerable: true,
119
+ configurable: false,
120
+ writable: false,
121
+ value: typeDef,
122
+ });
123
+ return typeDef;
123
124
  }
124
125
  /**
125
126
  * convertDtoParamsType
@@ -352,7 +353,7 @@ function plateNumber(value) {
352
353
  * @Usage:
353
354
  * @Author: richen
354
355
  * @Date: 2021-11-25 10:47:04
355
- * @LastEditTime: 2022-08-19 14:21:20
356
+ * @LastEditTime: 2024-01-03 14:32:49
356
357
  */
357
358
  // constant
358
359
  const PARAM_TYPE_KEY = 'PARAM_TYPE_KEY';
@@ -678,7 +679,7 @@ Object.keys(ValidFuncs).forEach((key) => {
678
679
  * @Usage:
679
680
  * @Author: richen
680
681
  * @Date: 2021-11-25 10:46:57
681
- * @LastEditTime: 2023-12-16 14:23:12
682
+ * @LastEditTime: 2024-01-03 11:30:55
682
683
  */
683
684
  /**
684
685
  * Validation parameter's type and values.
@@ -762,15 +763,11 @@ function Validated() {
762
763
  */
763
764
  function Expose() {
764
765
  return function (object, propertyName) {
765
- const types = Reflect.getMetadata("design:type", object, propertyName);
766
- if (types) {
767
- const originMap = koatty_container.getOriginMetadata(PARAM_TYPE_KEY, object);
768
- originMap.set(propertyName, types.name);
769
- }
766
+ setExpose(object, propertyName);
770
767
  };
771
768
  }
772
769
  /**
773
- * Identifies that the field needs to be defined
770
+ * Alias of Expose
774
771
  *
775
772
  * @export
776
773
  * @returns {PropertyDecorator}
@@ -1379,7 +1376,33 @@ function IsHash(algorithm, validationOptions) {
1379
1376
  });
1380
1377
  };
1381
1378
  }
1379
+ /**
1380
+ * Use a custom function for validation
1381
+ * @param func
1382
+ * @param validationOptions
1383
+ * @returns
1384
+ */
1385
+ function CheckFunc(func, validationOptions) {
1386
+ return function (object, propertyName) {
1387
+ setExpose(object, propertyName);
1388
+ classValidator.registerDecorator({
1389
+ name: "vCheckFunc",
1390
+ target: object.constructor,
1391
+ propertyName,
1392
+ options: validationOptions,
1393
+ validator: {
1394
+ validate(value, args) {
1395
+ return func(value);
1396
+ },
1397
+ defaultMessage(args) {
1398
+ return `invalid parameter ($property).`;
1399
+ }
1400
+ }
1401
+ });
1402
+ };
1403
+ }
1382
1404
 
1405
+ exports.CheckFunc = CheckFunc;
1383
1406
  exports.ClassValidator = ClassValidator;
1384
1407
  exports.Contains = Contains;
1385
1408
  exports.ENABLE_VALIDATED = ENABLE_VALIDATED;
@@ -1411,6 +1434,7 @@ exports.PARAM_CHECK_KEY = PARAM_CHECK_KEY;
1411
1434
  exports.PARAM_RULE_KEY = PARAM_RULE_KEY;
1412
1435
  exports.PARAM_TYPE_KEY = PARAM_TYPE_KEY;
1413
1436
  exports.Valid = Valid;
1437
+ exports.ValidFuncs = ValidFuncs;
1414
1438
  exports.Validated = Validated;
1415
1439
  exports.checkParamsType = checkParamsType;
1416
1440
  exports.convertDtoParamsType = convertDtoParamsType;
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2023-12-16 14:42:53
3
+ * @Date: 2024-01-08 07:08:31
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -62,7 +62,12 @@ function plainToClass(clazz, data, convert = false) {
62
62
  function assignDtoParams(clazz, data, convert = false) {
63
63
  const cls = Reflect.construct(clazz, []);
64
64
  if (convert) {
65
- return convertAssignDtoParams(clazz, cls, data);
65
+ const metaData = getDtoParamsMeta(clazz, cls);
66
+ for (const [key, type] of metaData) {
67
+ if (key && data[key] !== undefined) {
68
+ cls[key] = convertParamsType(data[key], type);
69
+ }
70
+ }
66
71
  }
67
72
  else {
68
73
  for (const key in cls) {
@@ -71,34 +76,30 @@ function assignDtoParams(clazz, data, convert = false) {
71
76
  cls[key] = data[key];
72
77
  }
73
78
  }
74
- return cls;
75
79
  }
80
+ return cls;
76
81
  }
77
82
  /**
78
- * convert type and assign dto params
83
+ * get class prototype type def.
79
84
  * @param clazz
80
85
  * @param cls
81
- * @param data
82
86
  * @returns
83
87
  */
84
- function convertAssignDtoParams(clazz, cls, data) {
85
- if (Object.prototype.hasOwnProperty.call(cls, "_typeDef")) {
86
- for (const key in cls) {
87
- if (Object.prototype.hasOwnProperty.call(cls._typeDef, key) &&
88
- data[key] !== undefined) {
89
- cls[key] = convertParamsType(data[key], cls._typeDef[key]);
90
- }
91
- }
92
- }
93
- else {
94
- const originMap = getOriginMetadata(PARAM_TYPE_KEY, clazz);
95
- for (const [key, type] of originMap) {
96
- if (key && data[key] !== undefined) {
97
- cls[key] = convertParamsType(data[key], type);
98
- }
99
- }
88
+ function getDtoParamsMeta(clazz, cls) {
89
+ // Non-own properties are inherited from the prototype chain,
90
+ // ensure that properties are not polluted
91
+ if (!Object.prototype.hasOwnProperty.call(cls, "_typeDef") &&
92
+ ("_typeDef" in cls)) {
93
+ return cls._typeDef;
100
94
  }
101
- return cls;
95
+ const typeDef = getOriginMetadata(PARAM_TYPE_KEY, clazz);
96
+ Reflect.defineProperty(clazz.prototype, "_typeDef", {
97
+ enumerable: true,
98
+ configurable: false,
99
+ writable: false,
100
+ value: typeDef,
101
+ });
102
+ return typeDef;
102
103
  }
103
104
  /**
104
105
  * convertDtoParamsType
@@ -331,7 +332,7 @@ function plateNumber(value) {
331
332
  * @Usage:
332
333
  * @Author: richen
333
334
  * @Date: 2021-11-25 10:47:04
334
- * @LastEditTime: 2022-08-19 14:21:20
335
+ * @LastEditTime: 2024-01-03 14:32:49
335
336
  */
336
337
  // constant
337
338
  const PARAM_TYPE_KEY = 'PARAM_TYPE_KEY';
@@ -657,7 +658,7 @@ Object.keys(ValidFuncs).forEach((key) => {
657
658
  * @Usage:
658
659
  * @Author: richen
659
660
  * @Date: 2021-11-25 10:46:57
660
- * @LastEditTime: 2023-12-16 14:23:12
661
+ * @LastEditTime: 2024-01-03 11:30:55
661
662
  */
662
663
  /**
663
664
  * Validation parameter's type and values.
@@ -741,15 +742,11 @@ function Validated() {
741
742
  */
742
743
  function Expose() {
743
744
  return function (object, propertyName) {
744
- const types = Reflect.getMetadata("design:type", object, propertyName);
745
- if (types) {
746
- const originMap = getOriginMetadata(PARAM_TYPE_KEY, object);
747
- originMap.set(propertyName, types.name);
748
- }
745
+ setExpose(object, propertyName);
749
746
  };
750
747
  }
751
748
  /**
752
- * Identifies that the field needs to be defined
749
+ * Alias of Expose
753
750
  *
754
751
  * @export
755
752
  * @returns {PropertyDecorator}
@@ -1358,5 +1355,30 @@ function IsHash(algorithm, validationOptions) {
1358
1355
  });
1359
1356
  };
1360
1357
  }
1358
+ /**
1359
+ * Use a custom function for validation
1360
+ * @param func
1361
+ * @param validationOptions
1362
+ * @returns
1363
+ */
1364
+ function CheckFunc(func, validationOptions) {
1365
+ return function (object, propertyName) {
1366
+ setExpose(object, propertyName);
1367
+ registerDecorator({
1368
+ name: "vCheckFunc",
1369
+ target: object.constructor,
1370
+ propertyName,
1371
+ options: validationOptions,
1372
+ validator: {
1373
+ validate(value, args) {
1374
+ return func(value);
1375
+ },
1376
+ defaultMessage(args) {
1377
+ return `invalid parameter ($property).`;
1378
+ }
1379
+ }
1380
+ });
1381
+ };
1382
+ }
1361
1383
 
1362
- export { ClassValidator, Contains, ENABLE_VALIDATED, Equals, Expose, FunctionValidator, Gt, Gte, IsCnName, IsDate, IsDefined, IsEmail, IsHash, IsIP, IsIdNumber, IsIn, IsMobile, IsNotEmpty, IsNotIn, IsPhoneNumber, IsPlateNumber, IsUrl, IsZipCode, Length, Lt, Lte, NotEquals, PARAM_CHECK_KEY, PARAM_RULE_KEY, PARAM_TYPE_KEY, Valid, Validated, checkParamsType, convertDtoParamsType, convertParamsType, paramterTypes, plainToClass };
1384
+ export { CheckFunc, ClassValidator, Contains, ENABLE_VALIDATED, Equals, Expose, FunctionValidator, Gt, Gte, IsCnName, IsDate, IsDefined, IsEmail, IsHash, IsIP, IsIdNumber, IsIn, IsMobile, IsNotEmpty, IsNotIn, IsPhoneNumber, IsPlateNumber, IsUrl, IsZipCode, Length, Lt, Lte, NotEquals, PARAM_CHECK_KEY, PARAM_RULE_KEY, PARAM_TYPE_KEY, Valid, ValidFuncs, Validated, checkParamsType, convertDtoParamsType, convertParamsType, paramterTypes, plainToClass };
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koatty_validation",
3
- "version": "1.2.10",
3
+ "version": "1.3.1",
4
4
  "description": "Validation Util for Koatty and ThinkORM.",
5
5
  "scripts": {
6
6
  "build": "npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp",
@@ -10,10 +10,12 @@
10
10
  "build:dts": "del-cli --force temp && npx tsc && npx api-extractor run --local --verbose",
11
11
  "eslint": "eslint --ext .ts,.js ./",
12
12
  "lock": "npm i --package-lock-only",
13
- "prepublishOnly": "npm test && npm run build",
13
+ "prepublishOnly": "npm test && npm run build && git push --follow-tags origin",
14
14
  "prerelease": "npm test && npm run build",
15
- "pub": "git push --follow-tags origin && npm publish",
16
15
  "release": "standard-version",
16
+ "release:pre": "npm run release -- --prerelease",
17
+ "release:major": "npm run release -- --release-as major",
18
+ "release:minor": "npm run release -- --release-as minor",
17
19
  "test": "npm run eslint && jest --passWithNoTests"
18
20
  },
19
21
  "main": "./dist/index.js",
@@ -84,4 +86,4 @@
84
86
  "koatty_lib": "^1.x.x",
85
87
  "koatty_logger": "^2.x.x"
86
88
  }
87
- }
89
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koatty_validation",
3
- "version": "1.2.10",
3
+ "version": "1.3.1",
4
4
  "description": "Validation Util for Koatty and ThinkORM.",
5
5
  "scripts": {
6
6
  "build": "npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp",
@@ -10,10 +10,12 @@
10
10
  "build:dts": "del-cli --force temp && npx tsc && npx api-extractor run --local --verbose",
11
11
  "eslint": "eslint --ext .ts,.js ./",
12
12
  "lock": "npm i --package-lock-only",
13
- "prepublishOnly": "npm test && npm run build",
13
+ "prepublishOnly": "npm test && npm run build && git push --follow-tags origin",
14
14
  "prerelease": "npm test && npm run build",
15
- "pub": "git push --follow-tags origin && npm publish",
16
15
  "release": "standard-version",
16
+ "release:pre": "npm run release -- --prerelease",
17
+ "release:major": "npm run release -- --release-as major",
18
+ "release:minor": "npm run release -- --release-as minor",
17
19
  "test": "npm run eslint && jest --passWithNoTests"
18
20
  },
19
21
  "main": "./dist/index.js",
@@ -84,4 +86,4 @@
84
86
  "koatty_lib": "^1.x.x",
85
87
  "koatty_logger": "^2.x.x"
86
88
  }
87
- }
89
+ }