valgen 4.0.0-alpha.2 → 4.0.0-alpha.3

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,16 @@ 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) {
48
- const d = (0, dayjs_1.default)(input);
49
- if (d.isValid())
50
- return options?.format ? d.format(options?.format) : d.toISOString();
51
- }
52
- if (typeof input === 'string') {
53
- const d = (0, dayjs_1.default)(input, options?.format);
54
- if (d.isValid())
49
+ if (typeof input === 'string' || input instanceof Date) {
50
+ const d = (0, dayjs_1.default)(input, inputFormat);
51
+ if (d.isValid()) {
52
+ if (context.coerce && options?.format || input instanceof Date)
53
+ return coerceFormat ? d.format(coerceFormat) : d.toISOString();
55
54
  return input;
55
+ }
56
56
  }
57
57
  context.failure({
58
58
  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;
@@ -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,16 @@ 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) {
41
- const d = dayjs(input);
42
- if (d.isValid())
43
- return options?.format ? d.format(options?.format) : d.toISOString();
44
- }
45
- if (typeof input === 'string') {
46
- const d = dayjs(input, options?.format);
47
- if (d.isValid())
42
+ if (typeof input === 'string' || input instanceof Date) {
43
+ const d = dayjs(input, inputFormat);
44
+ if (d.isValid()) {
45
+ if (context.coerce && options?.format || input instanceof Date)
46
+ return coerceFormat ? d.format(coerceFormat) : d.toISOString();
48
47
  return input;
48
+ }
49
49
  }
50
50
  context.failure({
51
51
  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
  }
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.3",
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).