valgen 4.0.0 → 4.1.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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # v4.1.0
2
+ [2023-10-31]
3
+
4
+ ### Changes
5
+
6
+ * Updated validation messages. ([`066fc4e`](https://github.com/panates/valgen/commit/066fc4e0f9fc7a0c0543572cf543c66084611135))
7
+
8
+ # v4.0.1
9
+ [2023-10-24]
10
+
11
+ ### Changes
12
+
13
+ * Added "coerce" option to isNull and isUndefined rules ([`8c02040`](https://github.com/panates/valgen/commit/8c02040264174e26c840fce351da8b69e664d626))
14
+
1
15
  # v4.0.0
2
16
  [2023-08-17]
3
17
 
@@ -15,6 +15,8 @@ class Context {
15
15
  this.onFail = options.onFail;
16
16
  if (options?.coerce != null)
17
17
  this.coerce = options?.coerce;
18
+ if (options?.label != null)
19
+ this.label = options?.label;
18
20
  }
19
21
  fail(rule, message, value, details) {
20
22
  const issue = (0, omit_undefined_js_1.omitUndefined)({
@@ -12,7 +12,7 @@ function isArray(itemValidator, options) {
12
12
  if (input != null && context.coerce && !Array.isArray(input))
13
13
  input = [input];
14
14
  if (!Array.isArray(input)) {
15
- context.fail(_this, `{{label}} is not an array`, input);
15
+ context.fail(_this, `{{label}} must be an array`, input);
16
16
  return;
17
17
  }
18
18
  if (!itemValidator)
@@ -9,13 +9,12 @@ const index_js_1 = require("../core/index.js");
9
9
  */
10
10
  function isBigint(options) {
11
11
  return (0, index_js_1.validator)('isBigint', function (input, context, _this) {
12
- if (input != null && typeof input !== 'bigint' && context.coerce) {
13
- if (typeof input === 'string' || typeof input === 'number')
14
- input = BigInt(input);
15
- }
16
12
  if (typeof input === 'bigint')
17
13
  return input;
18
- context.fail(_this, `{{label}} is not a valid BigInt`, input);
14
+ if ((typeof input === 'number' && !isNaN(input)) ||
15
+ (typeof input === 'string' && context.coerce))
16
+ return BigInt(input);
17
+ context.fail(_this, `{{label}} must be an integer number`, input);
19
18
  }, options);
20
19
  }
21
20
  exports.isBigint = isBigint;
@@ -24,7 +24,7 @@ function isBoolean(options) {
24
24
  }
25
25
  if (typeof input === 'boolean')
26
26
  return input;
27
- context.fail(_this, `{{label}} is not a valid boolean`, input);
27
+ context.fail(_this, `{{label}} must be a boolean`, input);
28
28
  }, options);
29
29
  }
30
30
  exports.isBoolean = isBoolean;
@@ -27,9 +27,9 @@ function isDate(options) {
27
27
  }
28
28
  if (input && input instanceof Date && !isNaN(input.getTime()))
29
29
  return input;
30
- context.fail(_this, `{{label}} is not a valid Date instance` +
30
+ context.fail(_this, `{{label}} must be a Date instance` +
31
31
  (context.coerce
32
- ? ` or a date formatted${options?.format ? " (" + options.format + ")" : ''} string`
32
+ ? ` or a date string${options?.format ? " (" + options.format + ")" : ''}`
33
33
  : ''), input, { format: options?.format });
34
34
  }, (0, object_utils_js_1.omitKeys)(options || {}, ['format']));
35
35
  }
@@ -8,29 +8,29 @@ const index_js_1 = require("../core/index.js");
8
8
  */
9
9
  function isEmpty(options) {
10
10
  return (0, index_js_1.validator)('isEmpty', function (input, context, _this) {
11
- if (input != null) {
12
- if (typeof input === 'string') {
13
- if (!input)
14
- return input;
15
- }
16
- else if (Array.isArray(input)) {
17
- if (!input.length)
18
- return input;
19
- }
20
- else if (input instanceof Set) {
21
- if (!input.size)
22
- return input;
23
- }
24
- else if (input instanceof Map) {
25
- if (!input.size)
26
- return input;
27
- }
28
- else if (typeof input === 'object') {
29
- if (!Object.keys(input).length)
30
- return input;
31
- }
11
+ if (input == null)
12
+ return input;
13
+ if (typeof input === 'string') {
14
+ if (!input)
15
+ return input;
16
+ }
17
+ else if (Array.isArray(input)) {
18
+ if (!input.length)
19
+ return input;
20
+ }
21
+ else if (input instanceof Set) {
22
+ if (!input.size)
23
+ return input;
24
+ }
25
+ else if (input instanceof Map) {
26
+ if (!input.size)
27
+ return input;
28
+ }
29
+ else if (typeof input === 'object') {
30
+ if (!Object.keys(input).length)
31
+ return input;
32
32
  }
33
- context.fail(_this, `{{label}} is not empty`, input);
33
+ context.fail(_this, `{{label}} must be empty`, input);
34
34
  }, options);
35
35
  }
36
36
  exports.isEmpty = isEmpty;
@@ -62,7 +62,7 @@ function isNotEmpty(options) {
62
62
  return input;
63
63
  }
64
64
  }
65
- context.fail(_this, `{{label}} is empty`, input);
65
+ context.fail(_this, `{{label}} mustn't be empty`, input);
66
66
  }, options);
67
67
  }
68
68
  exports.isNotEmpty = isNotEmpty;
@@ -20,7 +20,7 @@ function isInteger(options) {
20
20
  }
21
21
  if (typeof input === 'number' && !isNaN(input) && Number.isInteger(input))
22
22
  return input;
23
- context.fail(_this, `{{label}} is not a valid integer number`, input);
23
+ context.fail(_this, `{{label}} must be an integer number`, input);
24
24
  }, options);
25
25
  }
26
26
  exports.isInteger = isInteger;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isUndefined = exports.isDefined = exports.isNull = void 0;
3
+ exports.isUndefined = exports.isDefined = exports.isNullish = exports.isNull = void 0;
4
4
  const index_js_1 = require("../core/index.js");
5
5
  /**
6
6
  * Validates if value is "null".
@@ -10,10 +10,26 @@ function isNull(options) {
10
10
  return (0, index_js_1.validator)('isNull', function (input, context, _this) {
11
11
  if (input === null)
12
12
  return input;
13
- context.fail(_this, `{{label}} is not null`, input);
13
+ if (context.coerce)
14
+ return null;
15
+ context.fail(_this, `{{label}} must be null`, input);
14
16
  }, options);
15
17
  }
16
18
  exports.isNull = isNull;
19
+ /**
20
+ * Validates if value is "null" or "undefined".
21
+ * @validator isNull
22
+ */
23
+ function isNullish(options) {
24
+ return (0, index_js_1.validator)('isNullish', function (input, context, _this) {
25
+ if (input == null)
26
+ return input;
27
+ if (context.coerce)
28
+ return null;
29
+ context.fail(_this, `{{label}} must be null or undefined`, input);
30
+ }, options);
31
+ }
32
+ exports.isNullish = isNullish;
17
33
  /**
18
34
  * Validates if value is not "undefined" nor "null"
19
35
  * @validator isDefined
@@ -22,19 +38,21 @@ function isDefined(options) {
22
38
  return (0, index_js_1.validator)('is-defined', function (input, context, _this) {
23
39
  if (input !== undefined)
24
40
  return input;
25
- context.fail(_this, `{{label}} is not defined`, input);
41
+ context.fail(_this, `{{label}} must be defined`, input);
26
42
  }, options);
27
43
  }
28
44
  exports.isDefined = isDefined;
29
45
  /**
30
46
  * Validates if value is "undefined"
31
- * @validator isNotDefined
47
+ * @validator isUndefined
32
48
  */
33
49
  function isUndefined(options) {
34
50
  return (0, index_js_1.validator)('isUndefined', function (input, context, _this) {
51
+ if (context.coerce)
52
+ return undefined;
35
53
  if (input === undefined)
36
54
  return;
37
- context.fail(_this, `{{label}} defined`, input);
55
+ context.fail(_this, `{{label}} mustn\'t be defined`, input);
38
56
  }, options);
39
57
  }
40
58
  exports.isUndefined = isUndefined;
@@ -20,7 +20,7 @@ function isNumber(options) {
20
20
  }
21
21
  if (typeof input === 'number' && !isNaN(input))
22
22
  return input;
23
- context.fail(_this, `{{label}} is not a valid number`, input);
23
+ context.fail(_this, `{{label}} must be a number`, input);
24
24
  }, options);
25
25
  }
26
26
  exports.isNumber = isNumber;
@@ -18,7 +18,7 @@ function isObjectId(options) {
18
18
  typeof input.toHexString === 'function' &&
19
19
  _isObjectIdValue(input.toHexString()))))
20
20
  return input;
21
- context.fail(_this, `{{label}} is not a valid ObjectId`, input);
21
+ context.fail(_this, `{{label}} must be an ObjectId`, input);
22
22
  }, options);
23
23
  }
24
24
  exports.isObjectId = isObjectId;
@@ -33,7 +33,7 @@ function isObject(schema, options) {
33
33
  });
34
34
  const _rule = (0, index_js_1.validator)('isObject', function (input, context, _this) {
35
35
  if (!(input && typeof input === 'object')) {
36
- context.fail(_this, `{{label}} is not an object`, input);
36
+ context.fail(_this, `{{label}} must be an object`, input);
37
37
  return;
38
38
  }
39
39
  const keys = [...Object.keys(input), ...Object.keys(schema)];
@@ -10,7 +10,7 @@ const index_js_1 = require("../core/index.js");
10
10
  function isRecord(keyRule, valueRule, options) {
11
11
  return (0, index_js_1.validator)('isRecord', function (input, context, _this) {
12
12
  if (!(input && typeof input === 'object')) {
13
- context.fail(_this, `{{label}} is not an object`, input);
13
+ context.fail(_this, `{{label}} must be an object`, input);
14
14
  return;
15
15
  }
16
16
  const keyContext = context.extend();
@@ -21,7 +21,7 @@ function isString(options) {
21
21
  }
22
22
  if (typeof input === 'string')
23
23
  return input;
24
- context.fail(_this, `{{label}} is not a string`, input);
24
+ context.fail(_this, `{{label}} must be a string`, input);
25
25
  }, options);
26
26
  }
27
27
  exports.isString = isString;
@@ -7,7 +7,7 @@ function isTuple(items, options) {
7
7
  if (input != null && context.coerce && !Array.isArray(input))
8
8
  input = [input];
9
9
  if (!Array.isArray(input)) {
10
- context.fail(_this, `{{label}} is not a tuple`, input);
10
+ context.fail(_this, `{{label}} must be a tuple`, input);
11
11
  return;
12
12
  }
13
13
  const location = context.location || '';
@@ -12,6 +12,8 @@ export class Context {
12
12
  this.onFail = options.onFail;
13
13
  if (options?.coerce != null)
14
14
  this.coerce = options?.coerce;
15
+ if (options?.label != null)
16
+ this.label = options?.label;
15
17
  }
16
18
  fail(rule, message, value, details) {
17
19
  const issue = omitUndefined({
@@ -9,7 +9,7 @@ export function isArray(itemValidator, options) {
9
9
  if (input != null && context.coerce && !Array.isArray(input))
10
10
  input = [input];
11
11
  if (!Array.isArray(input)) {
12
- context.fail(_this, `{{label}} is not an array`, input);
12
+ context.fail(_this, `{{label}} must be an array`, input);
13
13
  return;
14
14
  }
15
15
  if (!itemValidator)
@@ -6,12 +6,11 @@ import { validator } from '../core/index.js';
6
6
  */
7
7
  export function isBigint(options) {
8
8
  return validator('isBigint', function (input, context, _this) {
9
- if (input != null && typeof input !== 'bigint' && context.coerce) {
10
- if (typeof input === 'string' || typeof input === 'number')
11
- input = BigInt(input);
12
- }
13
9
  if (typeof input === 'bigint')
14
10
  return input;
15
- context.fail(_this, `{{label}} is not a valid BigInt`, input);
11
+ if ((typeof input === 'number' && !isNaN(input)) ||
12
+ (typeof input === 'string' && context.coerce))
13
+ return BigInt(input);
14
+ context.fail(_this, `{{label}} must be an integer number`, input);
16
15
  }, options);
17
16
  }
@@ -21,6 +21,6 @@ export function isBoolean(options) {
21
21
  }
22
22
  if (typeof input === 'boolean')
23
23
  return input;
24
- context.fail(_this, `{{label}} is not a valid boolean`, input);
24
+ context.fail(_this, `{{label}} must be a boolean`, input);
25
25
  }, options);
26
26
  }
@@ -21,9 +21,9 @@ export function isDate(options) {
21
21
  }
22
22
  if (input && input instanceof Date && !isNaN(input.getTime()))
23
23
  return input;
24
- context.fail(_this, `{{label}} is not a valid Date instance` +
24
+ context.fail(_this, `{{label}} must be a Date instance` +
25
25
  (context.coerce
26
- ? ` or a date formatted${options?.format ? " (" + options.format + ")" : ''} string`
26
+ ? ` or a date string${options?.format ? " (" + options.format + ")" : ''}`
27
27
  : ''), input, { format: options?.format });
28
28
  }, omitKeys(options || {}, ['format']));
29
29
  }
@@ -5,29 +5,29 @@ import { validator } from '../core/index.js';
5
5
  */
6
6
  export function isEmpty(options) {
7
7
  return validator('isEmpty', function (input, context, _this) {
8
- if (input != null) {
9
- if (typeof input === 'string') {
10
- if (!input)
11
- return input;
12
- }
13
- else if (Array.isArray(input)) {
14
- if (!input.length)
15
- return input;
16
- }
17
- else if (input instanceof Set) {
18
- if (!input.size)
19
- return input;
20
- }
21
- else if (input instanceof Map) {
22
- if (!input.size)
23
- return input;
24
- }
25
- else if (typeof input === 'object') {
26
- if (!Object.keys(input).length)
27
- return input;
28
- }
8
+ if (input == null)
9
+ return input;
10
+ if (typeof input === 'string') {
11
+ if (!input)
12
+ return input;
13
+ }
14
+ else if (Array.isArray(input)) {
15
+ if (!input.length)
16
+ return input;
17
+ }
18
+ else if (input instanceof Set) {
19
+ if (!input.size)
20
+ return input;
21
+ }
22
+ else if (input instanceof Map) {
23
+ if (!input.size)
24
+ return input;
25
+ }
26
+ else if (typeof input === 'object') {
27
+ if (!Object.keys(input).length)
28
+ return input;
29
29
  }
30
- context.fail(_this, `{{label}} is not empty`, input);
30
+ context.fail(_this, `{{label}} must be empty`, input);
31
31
  }, options);
32
32
  }
33
33
  /**
@@ -58,6 +58,6 @@ export function isNotEmpty(options) {
58
58
  return input;
59
59
  }
60
60
  }
61
- context.fail(_this, `{{label}} is empty`, input);
61
+ context.fail(_this, `{{label}} mustn't be empty`, input);
62
62
  }, options);
63
63
  }
@@ -17,6 +17,6 @@ export function isInteger(options) {
17
17
  }
18
18
  if (typeof input === 'number' && !isNaN(input) && Number.isInteger(input))
19
19
  return input;
20
- context.fail(_this, `{{label}} is not a valid integer number`, input);
20
+ context.fail(_this, `{{label}} must be an integer number`, input);
21
21
  }, options);
22
22
  }
@@ -7,7 +7,22 @@ export function isNull(options) {
7
7
  return validator('isNull', function (input, context, _this) {
8
8
  if (input === null)
9
9
  return input;
10
- context.fail(_this, `{{label}} is not null`, input);
10
+ if (context.coerce)
11
+ return null;
12
+ context.fail(_this, `{{label}} must be null`, input);
13
+ }, options);
14
+ }
15
+ /**
16
+ * Validates if value is "null" or "undefined".
17
+ * @validator isNull
18
+ */
19
+ export function isNullish(options) {
20
+ return validator('isNullish', function (input, context, _this) {
21
+ if (input == null)
22
+ return input;
23
+ if (context.coerce)
24
+ return null;
25
+ context.fail(_this, `{{label}} must be null or undefined`, input);
11
26
  }, options);
12
27
  }
13
28
  /**
@@ -18,17 +33,19 @@ export function isDefined(options) {
18
33
  return validator('is-defined', function (input, context, _this) {
19
34
  if (input !== undefined)
20
35
  return input;
21
- context.fail(_this, `{{label}} is not defined`, input);
36
+ context.fail(_this, `{{label}} must be defined`, input);
22
37
  }, options);
23
38
  }
24
39
  /**
25
40
  * Validates if value is "undefined"
26
- * @validator isNotDefined
41
+ * @validator isUndefined
27
42
  */
28
43
  export function isUndefined(options) {
29
44
  return validator('isUndefined', function (input, context, _this) {
45
+ if (context.coerce)
46
+ return undefined;
30
47
  if (input === undefined)
31
48
  return;
32
- context.fail(_this, `{{label}} defined`, input);
49
+ context.fail(_this, `{{label}} mustn\'t be defined`, input);
33
50
  }, options);
34
51
  }
@@ -17,6 +17,6 @@ export function isNumber(options) {
17
17
  }
18
18
  if (typeof input === 'number' && !isNaN(input))
19
19
  return input;
20
- context.fail(_this, `{{label}} is not a valid number`, input);
20
+ context.fail(_this, `{{label}} must be a number`, input);
21
21
  }, options);
22
22
  }
@@ -12,7 +12,7 @@ export function isObjectId(options) {
12
12
  typeof input.toHexString === 'function' &&
13
13
  _isObjectIdValue(input.toHexString()))))
14
14
  return input;
15
- context.fail(_this, `{{label}} is not a valid ObjectId`, input);
15
+ context.fail(_this, `{{label}} must be an ObjectId`, input);
16
16
  }, options);
17
17
  }
18
18
  function _isObjectIdValue(input) {
@@ -30,7 +30,7 @@ export function isObject(schema, options) {
30
30
  });
31
31
  const _rule = validator('isObject', function (input, context, _this) {
32
32
  if (!(input && typeof input === 'object')) {
33
- context.fail(_this, `{{label}} is not an object`, input);
33
+ context.fail(_this, `{{label}} must be an object`, input);
34
34
  return;
35
35
  }
36
36
  const keys = [...Object.keys(input), ...Object.keys(schema)];
@@ -7,7 +7,7 @@ import { validator, } from '../core/index.js';
7
7
  export function isRecord(keyRule, valueRule, options) {
8
8
  return validator('isRecord', function (input, context, _this) {
9
9
  if (!(input && typeof input === 'object')) {
10
- context.fail(_this, `{{label}} is not an object`, input);
10
+ context.fail(_this, `{{label}} must be an object`, input);
11
11
  return;
12
12
  }
13
13
  const keyContext = context.extend();
@@ -18,7 +18,7 @@ export function isString(options) {
18
18
  }
19
19
  if (typeof input === 'string')
20
20
  return input;
21
- context.fail(_this, `{{label}} is not a string`, input);
21
+ context.fail(_this, `{{label}} must be a string`, input);
22
22
  }, options);
23
23
  }
24
24
  export function stringReplace(searchValue, replacer) {
@@ -4,7 +4,7 @@ export function isTuple(items, options) {
4
4
  if (input != null && context.coerce && !Array.isArray(input))
5
5
  input = [input];
6
6
  if (!Array.isArray(input)) {
7
- context.fail(_this, `{{label}} is not a tuple`, input);
7
+ context.fail(_this, `{{label}} must be a tuple`, input);
8
8
  return;
9
9
  }
10
10
  const location = context.location || '';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "valgen",
3
3
  "description": "Fast runtime type validator, converter and io (encoding/decoding) library",
4
- "version": "4.0.0",
4
+ "version": "4.1.0",
5
5
  "author": "Panates",
6
6
  "contributors": [
7
7
  "Eray Hanoglu <e.hanoglu@panates.com>"
@@ -12,9 +12,9 @@
12
12
  "url": "https://github.com/panates/valgen.git"
13
13
  },
14
14
  "dependencies": {
15
- "@types/validator": "^13.11.1",
15
+ "@types/validator": "^13.11.5",
16
16
  "dayjs": "^2.0.0-alpha.4",
17
- "ts-gems": "^2.4.1",
17
+ "ts-gems": "^2.5.0",
18
18
  "validator": "^13.11.0"
19
19
  },
20
20
  "type": "module",
@@ -18,4 +18,5 @@ export interface ValidationOptions {
18
18
  export interface ExecutionOptions extends ValidationOptions {
19
19
  coerce?: boolean;
20
20
  maxErrors?: number;
21
+ label?: string;
21
22
  }
@@ -4,6 +4,11 @@ import { ValidationOptions } from '../core/index.js';
4
4
  * @validator isNull
5
5
  */
6
6
  export declare function isNull(options?: ValidationOptions): import("../core/validator.js").Validator<null, unknown, import("../core/types.js").ExecutionOptions>;
7
+ /**
8
+ * Validates if value is "null" or "undefined".
9
+ * @validator isNull
10
+ */
11
+ export declare function isNullish(options?: ValidationOptions): import("../core/validator.js").Validator<null, unknown, import("../core/types.js").ExecutionOptions>;
7
12
  /**
8
13
  * Validates if value is not "undefined" nor "null"
9
14
  * @validator isDefined
@@ -11,6 +16,6 @@ export declare function isNull(options?: ValidationOptions): import("../core/val
11
16
  export declare function isDefined(options?: ValidationOptions): import("../core/validator.js").Validator<any, unknown, import("../core/types.js").ExecutionOptions>;
12
17
  /**
13
18
  * Validates if value is "undefined"
14
- * @validator isNotDefined
19
+ * @validator isUndefined
15
20
  */
16
21
  export declare function isUndefined(options?: ValidationOptions): import("../core/validator.js").Validator<any, unknown, import("../core/types.js").ExecutionOptions>;