valgen 4.0.0-alpha.2 → 4.0.0-alpha.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.
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./is-any.js"), exports);
18
18
  __exportStar(require("./is-array.js"), exports);
19
+ __exportStar(require("./is-bigint.js"), exports);
19
20
  __exportStar(require("./is-boolean.js"), exports);
20
21
  __exportStar(require("./is-date.js"), exports);
21
22
  __exportStar(require("./is-empty.js"), exports);
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isBigint = void 0;
4
+ const index_js_1 = require("../core/index.js");
5
+ /**
6
+ * Validates if value is "BigInt".
7
+ * Converts input value to number if coerce option is set to 'true'.
8
+ * @validator isNumber
9
+ */
10
+ function isBigint(options) {
11
+ return (0, index_js_1.validator)('isBigint', function (input, context) {
12
+ if (input != null && typeof input !== 'bigint' && context.coerce) {
13
+ if (typeof input === 'string' || typeof input === 'number')
14
+ input = BigInt(input);
15
+ }
16
+ if (typeof input === 'bigint')
17
+ return input;
18
+ context.failure(`{{label}} is not a valid BigInt`);
19
+ }, options);
20
+ }
21
+ exports.isBigint = isBigint;
@@ -43,16 +43,21 @@ exports.isDate = isDate;
43
43
  * @validator isDateString
44
44
  */
45
45
  function isDateString(options) {
46
+ const inputFormat = options?.format;
47
+ const coerceFormat = Array.isArray(options?.format) ? options?.format[0] : options?.format;
46
48
  return (0, index_js_1.validator)('isDateString', function (input, context) {
47
- if (input && typeof input !== 'string' && context.coerce) {
49
+ if (input instanceof Date) {
48
50
  const d = (0, dayjs_1.default)(input);
49
51
  if (d.isValid())
50
- return options?.format ? d.format(options?.format) : d.toISOString();
52
+ return coerceFormat ? d.format(coerceFormat) : d.toISOString();
51
53
  }
52
- if (typeof input === 'string') {
53
- const d = (0, dayjs_1.default)(input, options?.format);
54
- if (d.isValid())
54
+ else if (typeof input === 'string') {
55
+ const d = (0, dayjs_1.default)(input, inputFormat);
56
+ if (d.isValid()) {
57
+ if (context.coerce && options?.format)
58
+ return coerceFormat ? d.format(coerceFormat) : d.toISOString();
55
59
  return input;
60
+ }
56
61
  }
57
62
  context.failure({
58
63
  message: `{{label}} is not a valid date formatted${options?.format ? " (" + options.format + ")" : ''} string`,
@@ -20,7 +20,7 @@ function isNumber(options) {
20
20
  }
21
21
  if (typeof input === 'number' && !isNaN(input))
22
22
  return input;
23
- context.failure(`{{label}} not a valid number`);
23
+ context.failure(`{{label}} is not a valid number`);
24
24
  }, options);
25
25
  }
26
26
  exports.isNumber = isNumber;
@@ -13,13 +13,6 @@ function isObject(schema, options) {
13
13
  const additionalFields = options?.additionalFields ?? false;
14
14
  const caseInSensitive = !!options?.caseInSensitive;
15
15
  const detectCircular = !!options?.detectCircular;
16
- if (options) {
17
- delete options.name;
18
- delete options.ctor;
19
- delete options.additionalFields;
20
- delete options.caseInSensitive;
21
- delete options.detectCircular;
22
- }
23
16
  const propertyRules = {};
24
17
  const propertyOptions = {};
25
18
  Object.keys(schema).forEach(k => {
@@ -38,21 +31,12 @@ function isObject(schema, options) {
38
31
  else
39
32
  throw new TypeError(`Invalid definition in validation schema (${k})`);
40
33
  });
41
- const schemaKeys = Object.keys(propertyRules);
42
34
  const _rule = (0, index_js_1.validator)('isObject', function (input, context) {
43
35
  if (!(input && typeof input === 'object')) {
44
36
  context.failure(`{{label}} must be an object`);
45
37
  return;
46
38
  }
47
- const keyMap = [...Object.keys(input), ...schemaKeys]
48
- .reduce((a, k) => {
49
- if (caseInSensitive)
50
- a[k.toLowerCase()] = k;
51
- else
52
- a[k] = k;
53
- return a;
54
- }, {});
55
- const keys = Object.keys(keyMap);
39
+ const keys = [...Object.keys(input), ...Object.keys(schema)];
56
40
  const l = keys.length;
57
41
  let i = 0;
58
42
  let inputKey;
@@ -70,14 +54,18 @@ function isObject(schema, options) {
70
54
  }
71
55
  const rootName = context.root?.input.context;
72
56
  const location = context.input.location || '';
57
+ const processedSchemaKeys = {};
73
58
  // Iterate object keys and perform rules
74
59
  for (i = 0; i < l; i++) {
75
60
  inputKey = keys[i];
76
- schemaKey = keyMap[inputKey];
61
+ schemaKey = caseInSensitive ? inputKey.toLowerCase() : inputKey;
77
62
  v = input[inputKey];
78
63
  _propRule = propertyRules[schemaKey] ||
79
64
  ((0, index_js_1.isValidator)(additionalFields) ? additionalFields : undefined);
80
65
  if (_propRule) {
66
+ if (processedSchemaKeys[schemaKey])
67
+ continue;
68
+ processedSchemaKeys[schemaKey] = true;
81
69
  const subCtx = context.extend(_propRule);
82
70
  if (ctorName) {
83
71
  if (rootName && rootName !== ctorName)
@@ -90,8 +78,12 @@ function isObject(schema, options) {
90
78
  subCtx.input.property = schemaKey;
91
79
  v = _propRule[index_js_1.kValidatorFn](v, subCtx, _propRule);
92
80
  }
93
- else if (v !== undefined && !additionalFields)
94
- context.failure(`${ctorName || 'Object'} does not accept additional fields`);
81
+ else if (v !== undefined) {
82
+ if (additionalFields === 'ignore')
83
+ continue;
84
+ if (!additionalFields)
85
+ context.failure(`${ctorName || 'Object'} has no field '${inputKey}' and does not accept additional fields`);
86
+ }
95
87
  if (v !== undefined)
96
88
  out[propertyOptions[schemaKey]?.as || schemaKey] = v;
97
89
  }
@@ -1,5 +1,6 @@
1
1
  export * from './is-any.js';
2
2
  export * from './is-array.js';
3
+ export * from './is-bigint.js';
3
4
  export * from './is-boolean.js';
4
5
  export * from './is-date.js';
5
6
  export * from './is-empty.js';
@@ -0,0 +1,17 @@
1
+ import { validator } from '../core/index.js';
2
+ /**
3
+ * Validates if value is "BigInt".
4
+ * Converts input value to number if coerce option is set to 'true'.
5
+ * @validator isNumber
6
+ */
7
+ export function isBigint(options) {
8
+ return validator('isBigint', function (input, context) {
9
+ if (input != null && typeof input !== 'bigint' && context.coerce) {
10
+ if (typeof input === 'string' || typeof input === 'number')
11
+ input = BigInt(input);
12
+ }
13
+ if (typeof input === 'bigint')
14
+ return input;
15
+ context.failure(`{{label}} is not a valid BigInt`);
16
+ }, options);
17
+ }
@@ -36,16 +36,21 @@ export function isDate(options) {
36
36
  * @validator isDateString
37
37
  */
38
38
  export function isDateString(options) {
39
+ const inputFormat = options?.format;
40
+ const coerceFormat = Array.isArray(options?.format) ? options?.format[0] : options?.format;
39
41
  return validator('isDateString', function (input, context) {
40
- if (input && typeof input !== 'string' && context.coerce) {
42
+ if (input instanceof Date) {
41
43
  const d = dayjs(input);
42
44
  if (d.isValid())
43
- return options?.format ? d.format(options?.format) : d.toISOString();
45
+ return coerceFormat ? d.format(coerceFormat) : d.toISOString();
44
46
  }
45
- if (typeof input === 'string') {
46
- const d = dayjs(input, options?.format);
47
- if (d.isValid())
47
+ else if (typeof input === 'string') {
48
+ const d = dayjs(input, inputFormat);
49
+ if (d.isValid()) {
50
+ if (context.coerce && options?.format)
51
+ return coerceFormat ? d.format(coerceFormat) : d.toISOString();
48
52
  return input;
53
+ }
49
54
  }
50
55
  context.failure({
51
56
  message: `{{label}} is not a valid date formatted${options?.format ? " (" + options.format + ")" : ''} string`,
@@ -17,6 +17,6 @@ export function isNumber(options) {
17
17
  }
18
18
  if (typeof input === 'number' && !isNaN(input))
19
19
  return input;
20
- context.failure(`{{label}} not a valid number`);
20
+ context.failure(`{{label}} is not a valid number`);
21
21
  }, options);
22
22
  }
@@ -10,13 +10,6 @@ export function isObject(schema, options) {
10
10
  const additionalFields = options?.additionalFields ?? false;
11
11
  const caseInSensitive = !!options?.caseInSensitive;
12
12
  const detectCircular = !!options?.detectCircular;
13
- if (options) {
14
- delete options.name;
15
- delete options.ctor;
16
- delete options.additionalFields;
17
- delete options.caseInSensitive;
18
- delete options.detectCircular;
19
- }
20
13
  const propertyRules = {};
21
14
  const propertyOptions = {};
22
15
  Object.keys(schema).forEach(k => {
@@ -35,21 +28,12 @@ export function isObject(schema, options) {
35
28
  else
36
29
  throw new TypeError(`Invalid definition in validation schema (${k})`);
37
30
  });
38
- const schemaKeys = Object.keys(propertyRules);
39
31
  const _rule = validator('isObject', function (input, context) {
40
32
  if (!(input && typeof input === 'object')) {
41
33
  context.failure(`{{label}} must be an object`);
42
34
  return;
43
35
  }
44
- const keyMap = [...Object.keys(input), ...schemaKeys]
45
- .reduce((a, k) => {
46
- if (caseInSensitive)
47
- a[k.toLowerCase()] = k;
48
- else
49
- a[k] = k;
50
- return a;
51
- }, {});
52
- const keys = Object.keys(keyMap);
36
+ const keys = [...Object.keys(input), ...Object.keys(schema)];
53
37
  const l = keys.length;
54
38
  let i = 0;
55
39
  let inputKey;
@@ -67,14 +51,18 @@ export function isObject(schema, options) {
67
51
  }
68
52
  const rootName = context.root?.input.context;
69
53
  const location = context.input.location || '';
54
+ const processedSchemaKeys = {};
70
55
  // Iterate object keys and perform rules
71
56
  for (i = 0; i < l; i++) {
72
57
  inputKey = keys[i];
73
- schemaKey = keyMap[inputKey];
58
+ schemaKey = caseInSensitive ? inputKey.toLowerCase() : inputKey;
74
59
  v = input[inputKey];
75
60
  _propRule = propertyRules[schemaKey] ||
76
61
  (isValidator(additionalFields) ? additionalFields : undefined);
77
62
  if (_propRule) {
63
+ if (processedSchemaKeys[schemaKey])
64
+ continue;
65
+ processedSchemaKeys[schemaKey] = true;
78
66
  const subCtx = context.extend(_propRule);
79
67
  if (ctorName) {
80
68
  if (rootName && rootName !== ctorName)
@@ -87,8 +75,12 @@ export function isObject(schema, options) {
87
75
  subCtx.input.property = schemaKey;
88
76
  v = _propRule[kValidatorFn](v, subCtx, _propRule);
89
77
  }
90
- else if (v !== undefined && !additionalFields)
91
- context.failure(`${ctorName || 'Object'} does not accept additional fields`);
78
+ else if (v !== undefined) {
79
+ if (additionalFields === 'ignore')
80
+ continue;
81
+ if (!additionalFields)
82
+ context.failure(`${ctorName || 'Object'} has no field '${inputKey}' and does not accept additional fields`);
83
+ }
92
84
  if (v !== undefined)
93
85
  out[propertyOptions[schemaKey]?.as || schemaKey] = v;
94
86
  }
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-alpha.2",
4
+ "version": "4.0.0-alpha.4",
5
5
  "author": "Panates",
6
6
  "contributors": [
7
7
  "Eray Hanoglu <e.hanoglu@panates.com>"
@@ -1,5 +1,6 @@
1
1
  export * from './is-any.js';
2
2
  export * from './is-array.js';
3
+ export * from './is-bigint.js';
3
4
  export * from './is-boolean.js';
4
5
  export * from './is-date.js';
5
6
  export * from './is-empty.js';
@@ -0,0 +1,7 @@
1
+ import { ValidationOptions } from '../core/index.js';
2
+ /**
3
+ * Validates if value is "BigInt".
4
+ * Converts input value to number if coerce option is set to 'true'.
5
+ * @validator isNumber
6
+ */
7
+ export declare function isBigint(options?: ValidationOptions): import("../core/validator.js").Validator<bigint, unknown, import("../core/types.js").ExecutionOptions>;
@@ -9,7 +9,7 @@ export interface IsDateOptions extends ValidationOptions {
9
9
  */
10
10
  export declare function isDate(options?: IsDateOptions): import("../core/validator.js").Validator<Date, string | number | Date, import("../core/types.js").ExecutionOptions>;
11
11
  export interface IsDateStringOptions extends ValidationOptions {
12
- format?: string;
12
+ format?: string | string[];
13
13
  }
14
14
  /**
15
15
  * Validates if value is DFS (date formatted string).
@@ -7,7 +7,7 @@ export type ObjectSchema = Record<string | number, Validator<any, any> | [Valida
7
7
  export interface IsObjectOptions<T> extends ValidationOptions {
8
8
  name?: string;
9
9
  ctor?: Type<T>;
10
- additionalFields?: boolean | Validator<any, any>;
10
+ additionalFields?: boolean | Validator<any, any> | 'ignore';
11
11
  caseInSensitive?: boolean;
12
12
  detectCircular?: boolean;
13
13
  }