valgen 5.0.1 → 5.2.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
+ # v5.2.0
2
+ [2024-01-04]
3
+
4
+ ### Changes
5
+
6
+ * Now validator options persist in context ([`418901f`](https://github.com/panates/valgen/commit/418901f74ee266d84e50ee82ba5a7221bd2be627))
7
+
8
+ # v5.1.0
9
+ [2023-12-23]
10
+
11
+ ### Changes
12
+
13
+ * Moved from "dayjs" to "date-fns" ([`fd240af`](https://github.com/panates/valgen/commit/fd240afe3cdbb7485695d204aa25b9bf62ad315a))
14
+
1
15
  # v5.0.1
2
16
  [2023-12-23]
3
17
 
@@ -9,14 +9,7 @@ const OPTIONAL_VAR_PATTERN = /^([^?]+)(?:\||(.*))?$/;
9
9
  class Context {
10
10
  constructor(options) {
11
11
  this.errors = [];
12
- if (options?.maxErrors != null)
13
- this.maxErrors = options.maxErrors;
14
- if (typeof options?.onFail === 'function')
15
- this.onFail = options.onFail;
16
- if (options?.coerce != null)
17
- this.coerce = options?.coerce;
18
- if (options?.label != null)
19
- this.label = options?.label;
12
+ Object.assign(this, options);
20
13
  }
21
14
  fail(rule, message, value, details) {
22
15
  const issue = (0, omit_undefined_js_1.omitUndefined)({
@@ -1,13 +1,8 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.isDateString = exports.isDate = void 0;
7
- const dayjs_1 = __importDefault(require("dayjs"));
8
- const customParseFormat_1 = __importDefault(require("dayjs/plugin/customParseFormat"));
4
+ const date_fns_1 = require("date-fns");
9
5
  const index_js_1 = require("../core/index.js");
10
- dayjs_1.default.extend(customParseFormat_1.default);
11
6
  /* eslint-disable-next-line max-len */ // noinspection RegExpUnnecessaryNonCapturingGroup
12
7
  const DATE_PATTERN = /^(\d{4})(?:-(0[0-9]|1[0-2]))?(?:-([0-2][0-9]|3[0-1]))?(?:[T ](([0-1][0-9]|2[0-4]):([0-5][0-9])(?::([0-5][0-9]))?(?:\.(\d{0,3}))?)?((?:[+-](0[0-9]|1[0-2])(?::(\d{2}))?)|Z)?)?$/;
13
8
  /**
@@ -18,27 +13,28 @@ const DATE_PATTERN = /^(\d{4})(?:-(0[0-9]|1[0-2]))?(?:-([0-2][0-9]|3[0-1]))?(?:[
18
13
  function isDate(options) {
19
14
  const precision = options?.precision;
20
15
  return (0, index_js_1.validator)('isDate', function (input, context, _this) {
21
- if (input != null && !(input instanceof Date) && context.coerce) {
16
+ let d;
17
+ if (input instanceof Date)
18
+ d = input;
19
+ else if (input != null && context.coerce) {
22
20
  if (typeof input === 'string' && context.coerce) {
23
- const d = (0, dayjs_1.default)(input);
24
- if (d.isValid())
25
- input = d.toDate();
21
+ d = (0, date_fns_1.parseISO)(input);
26
22
  }
27
23
  else if (typeof input === 'number')
28
- input = new Date(input);
24
+ d = new Date(input);
29
25
  }
30
- if (input && input instanceof Date && !isNaN(input.getTime())) {
26
+ if (d && !isNaN(d.getTime())) {
31
27
  if (precision === 'year') {
32
- input.setHours(0, 0, 0, 0);
33
- input.setMonth(0, 1);
28
+ d.setHours(0, 0, 0, 0);
29
+ d.setMonth(0, 1);
34
30
  }
35
31
  if (precision === 'month') {
36
- input.setHours(0, 0, 0, 0);
37
- input.setDate(1);
32
+ d.setHours(0, 0, 0, 0);
33
+ d.setDate(1);
38
34
  }
39
35
  if (precision === 'date')
40
- input.setHours(0, 0, 0, 0);
41
- return input;
36
+ d.setHours(0, 0, 0, 0);
37
+ return d;
42
38
  }
43
39
  context.fail(_this, `{{label}} must be a Date instance or a date string`, input);
44
40
  }, options);
@@ -54,10 +50,10 @@ function isDateString(options) {
54
50
  const trim = options?.trim;
55
51
  return (0, index_js_1.validator)('isDateString', function (input, context, _this) {
56
52
  if (typeof input === 'string') {
57
- const d = (0, dayjs_1.default)(input);
58
- if (d.isValid()) {
59
- const m = DATE_PATTERN.exec(input);
60
- if (m) {
53
+ const m = DATE_PATTERN.exec(input);
54
+ if (m) {
55
+ const d = (0, date_fns_1.parseISO)(input);
56
+ if (d && !isNaN(d.getTime())) {
61
57
  if (!precision ||
62
58
  precision === 'year' ||
63
59
  (precision === 'month' && m[2]) ||
@@ -76,7 +72,7 @@ function isDateString(options) {
76
72
  return s;
77
73
  if (trim === 'date' || !m[4])
78
74
  return s;
79
- s += 'T' + m[4];
75
+ s += 'T' + m[4].substring(0, 8);
80
76
  if (trim === 'time')
81
77
  return s;
82
78
  if (m[9])
@@ -86,13 +82,10 @@ function isDateString(options) {
86
82
  }
87
83
  }
88
84
  }
89
- else if (input != null) {
90
- const d = (0, dayjs_1.default)(input);
91
- if (d.isValid()) {
92
- if (trim === 'date')
93
- return d.format('YYYY-MM-DD');
94
- return d.millisecond() ? d.format('YYYY-MM-DDTHH:mm:ss.SSS') : d.format('YYYY-MM-DDTHH:mm:ss');
95
- }
85
+ else if (input instanceof Date) {
86
+ return trim === 'date'
87
+ ? (0, date_fns_1.formatISO)(input, { representation: 'date' })
88
+ : (0, date_fns_1.formatISO)(input).substring(0, 19);
96
89
  }
97
90
  context.fail(_this, `{{label}} is not a valid date string`, input, { ...options });
98
91
  }, options);
@@ -6,14 +6,7 @@ const OPTIONAL_VAR_PATTERN = /^([^?]+)(?:\||(.*))?$/;
6
6
  export class Context {
7
7
  constructor(options) {
8
8
  this.errors = [];
9
- if (options?.maxErrors != null)
10
- this.maxErrors = options.maxErrors;
11
- if (typeof options?.onFail === 'function')
12
- this.onFail = options.onFail;
13
- if (options?.coerce != null)
14
- this.coerce = options?.coerce;
15
- if (options?.label != null)
16
- this.label = options?.label;
9
+ Object.assign(this, options);
17
10
  }
18
11
  fail(rule, message, value, details) {
19
12
  const issue = omitUndefined({
@@ -1,7 +1,5 @@
1
- import dayjs from 'dayjs';
2
- import customParseFormat from 'dayjs/plugin/customParseFormat';
1
+ import { formatISO, parseISO } from 'date-fns';
3
2
  import { validator } from '../core/index.js';
4
- dayjs.extend(customParseFormat);
5
3
  /* eslint-disable-next-line max-len */ // noinspection RegExpUnnecessaryNonCapturingGroup
6
4
  const DATE_PATTERN = /^(\d{4})(?:-(0[0-9]|1[0-2]))?(?:-([0-2][0-9]|3[0-1]))?(?:[T ](([0-1][0-9]|2[0-4]):([0-5][0-9])(?::([0-5][0-9]))?(?:\.(\d{0,3}))?)?((?:[+-](0[0-9]|1[0-2])(?::(\d{2}))?)|Z)?)?$/;
7
5
  /**
@@ -12,27 +10,28 @@ const DATE_PATTERN = /^(\d{4})(?:-(0[0-9]|1[0-2]))?(?:-([0-2][0-9]|3[0-1]))?(?:[
12
10
  export function isDate(options) {
13
11
  const precision = options?.precision;
14
12
  return validator('isDate', function (input, context, _this) {
15
- if (input != null && !(input instanceof Date) && context.coerce) {
13
+ let d;
14
+ if (input instanceof Date)
15
+ d = input;
16
+ else if (input != null && context.coerce) {
16
17
  if (typeof input === 'string' && context.coerce) {
17
- const d = dayjs(input);
18
- if (d.isValid())
19
- input = d.toDate();
18
+ d = parseISO(input);
20
19
  }
21
20
  else if (typeof input === 'number')
22
- input = new Date(input);
21
+ d = new Date(input);
23
22
  }
24
- if (input && input instanceof Date && !isNaN(input.getTime())) {
23
+ if (d && !isNaN(d.getTime())) {
25
24
  if (precision === 'year') {
26
- input.setHours(0, 0, 0, 0);
27
- input.setMonth(0, 1);
25
+ d.setHours(0, 0, 0, 0);
26
+ d.setMonth(0, 1);
28
27
  }
29
28
  if (precision === 'month') {
30
- input.setHours(0, 0, 0, 0);
31
- input.setDate(1);
29
+ d.setHours(0, 0, 0, 0);
30
+ d.setDate(1);
32
31
  }
33
32
  if (precision === 'date')
34
- input.setHours(0, 0, 0, 0);
35
- return input;
33
+ d.setHours(0, 0, 0, 0);
34
+ return d;
36
35
  }
37
36
  context.fail(_this, `{{label}} must be a Date instance or a date string`, input);
38
37
  }, options);
@@ -47,10 +46,10 @@ export function isDateString(options) {
47
46
  const trim = options?.trim;
48
47
  return validator('isDateString', function (input, context, _this) {
49
48
  if (typeof input === 'string') {
50
- const d = dayjs(input);
51
- if (d.isValid()) {
52
- const m = DATE_PATTERN.exec(input);
53
- if (m) {
49
+ const m = DATE_PATTERN.exec(input);
50
+ if (m) {
51
+ const d = parseISO(input);
52
+ if (d && !isNaN(d.getTime())) {
54
53
  if (!precision ||
55
54
  precision === 'year' ||
56
55
  (precision === 'month' && m[2]) ||
@@ -69,7 +68,7 @@ export function isDateString(options) {
69
68
  return s;
70
69
  if (trim === 'date' || !m[4])
71
70
  return s;
72
- s += 'T' + m[4];
71
+ s += 'T' + m[4].substring(0, 8);
73
72
  if (trim === 'time')
74
73
  return s;
75
74
  if (m[9])
@@ -79,13 +78,10 @@ export function isDateString(options) {
79
78
  }
80
79
  }
81
80
  }
82
- else if (input != null) {
83
- const d = dayjs(input);
84
- if (d.isValid()) {
85
- if (trim === 'date')
86
- return d.format('YYYY-MM-DD');
87
- return d.millisecond() ? d.format('YYYY-MM-DDTHH:mm:ss.SSS') : d.format('YYYY-MM-DDTHH:mm:ss');
88
- }
81
+ else if (input instanceof Date) {
82
+ return trim === 'date'
83
+ ? formatISO(input, { representation: 'date' })
84
+ : formatISO(input).substring(0, 19);
89
85
  }
90
86
  context.fail(_this, `{{label}} is not a valid date string`, input, { ...options });
91
87
  }, options);
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": "5.0.1",
4
+ "version": "5.2.0",
5
5
  "author": "Panates",
6
6
  "contributors": [
7
7
  "Eray Hanoglu <e.hanoglu@panates.com>"
@@ -13,7 +13,7 @@
13
13
  },
14
14
  "dependencies": {
15
15
  "@types/validator": "^13.11.7",
16
- "dayjs": "^2.0.0-alpha.4",
16
+ "date-fns": "^3.0.6",
17
17
  "ts-gems": "^3.1.0",
18
18
  "validator": "^13.11.0"
19
19
  },
@@ -12,6 +12,7 @@ export declare class Context implements ExecutionOptions {
12
12
  property?: string;
13
13
  index?: number;
14
14
  label?: string;
15
+ [key: string]: any;
15
16
  constructor(options?: ExecutionOptions);
16
17
  fail(rule: Validator, message: string | Error, value: any, details?: Record<string, any>): void;
17
18
  extend(options?: ExecutionOptions): Context;
@@ -19,4 +19,5 @@ export interface ExecutionOptions extends ValidationOptions {
19
19
  coerce?: boolean;
20
20
  maxErrors?: number;
21
21
  label?: string;
22
+ [key: string]: any;
22
23
  }
@@ -15,4 +15,4 @@ export interface Validator<T = any, I = any, O extends ExecutionOptions = Execut
15
15
  }
16
16
  export declare function validator<T, I = T, O extends ExecutionOptions = ExecutionOptions>(fn: ValidateFunction<T, I>, validatorOptions?: ValidationOptions): Validator<T, I, O>;
17
17
  export declare function validator<T, I = T, O extends ExecutionOptions = ExecutionOptions>(id: string, fn: ValidateFunction<T, I>, validatorOptions?: ValidationOptions): Validator<T, I, O>;
18
- export declare function isValidator(x: any): x is Validator<any, any>;
18
+ export declare function isValidator(x: any): x is Validator;
@@ -23,5 +23,5 @@ export declare function isTuple<T1, I1, T2, I2, T3, I3, T4, I4, T5, I5, T6, I6>(
23
23
  Validator<T4, I4>,
24
24
  Validator<T5, I5>,
25
25
  Validator<T6, I6>,
26
- ...Validator<any, any>[]
26
+ ...Validator[]
27
27
  ], options?: ValidationOptions): Validator<[T1, T2, T3, T4, T5, T6, ...any[]], [I1, I2, I3, I4, I5, I5, ...any[]]>;
@@ -3,9 +3,9 @@ import { Validator } from '../core/index.js';
3
3
  *
4
4
  * @validator pipe
5
5
  */
6
- export declare function pipe<T>(...rules: [...Validator<any, any>[], Validator<T, any>]): Validator<T, any, import("../core/types.js").ExecutionOptions>;
6
+ export declare function pipe<T>(...rules: [...Validator[], Validator<T, any>]): Validator<T, any, import("../core/types.js").ExecutionOptions>;
7
7
  /**
8
8
  * Test given value against to all codecs and returns original input
9
9
  * @validator allOf
10
10
  */
11
- export declare function allOf(...rules: Validator<any, any>[]): Validator<any, any, import("../core/types.js").ExecutionOptions>;
11
+ export declare function allOf(...rules: Validator[]): Validator<any, any, import("../core/types.js").ExecutionOptions>;