valgen 5.19.1 → 5.19.2

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/constants.js CHANGED
@@ -1,4 +1,4 @@
1
- export const version = '5.19.1';
1
+ export const version = '5.19.2';
2
2
  export const postValidation = Symbol('postValidation');
3
3
  export const preValidation = Symbol('preValidation');
4
4
  export const VALIDATE_METADATA = Symbol('VALIDATE_METADATA');
package/index.d.ts CHANGED
@@ -3,6 +3,7 @@ import { DatePrecision } from './rules/index.js';
3
3
  import * as vg from './rules/index.js';
4
4
  export * from './constants.js';
5
5
  export * from './core/index.js';
6
+ export type { DatePrecision } from './rules/type-rules/is-date.js';
6
7
  export type { IsObject } from './rules/type-rules/is-object.js';
7
8
  declare const isAlpha: import("./core/validator.js").Validator<string, string, ExecutionOptions>;
8
9
  declare const isAlphanumeric: import("./core/validator.js").Validator<string, string, ExecutionOptions>;
package/index.js CHANGED
@@ -61,7 +61,7 @@ const toDateString = (input, options, ctx) => {
61
61
  const trim = options?.trim ?? 'seconds';
62
62
  let validator = toDateStringValidators.get(trim);
63
63
  if (!validator) {
64
- validator = vg.isDateString({ coerce: true, trim });
64
+ validator = vg.isDateString({ coerce: true, precisionMax: trim });
65
65
  toDateStringValidators.set(trim, validator);
66
66
  }
67
67
  return validator(input, options, ctx);
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.19.1",
4
+ "version": "5.19.2",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
7
7
  "dependencies": {
@@ -12,7 +12,7 @@ export declare function isDate(options?: IsDateOptions): import("../../core/vali
12
12
  export interface IsDateStringOptions extends ValidationOptions {
13
13
  precisionMin?: DatePrecision;
14
14
  precisionMax?: DatePrecision;
15
- trim?: DatePrecision;
15
+ trim?: boolean;
16
16
  timeZone?: boolean | number;
17
17
  }
18
18
  /**
@@ -37,33 +37,32 @@ export function isDate(options) {
37
37
  * @validator isDateString
38
38
  */
39
39
  export function isDateString(options) {
40
- const trimIdx = PRECISION_INDEX[options?.trim || 'tz'] || 9;
41
- const precisionMin = options?.precisionMin || options?.trim || 'minutes';
40
+ const trim = options?.trim;
42
41
  const precisionMax = options?.precisionMax || 'tz';
43
- const precisionMaxIdx = PRECISION_INDEX[precisionMax] || 9;
44
- const precisionMinIdx = Math.min(trimIdx, precisionMaxIdx, PRECISION_INDEX[precisionMin] || 6);
42
+ const precisionMaxIdx = PRECISION_INDEX[precisionMax] || 8;
43
+ let precisionMin = options?.precisionMin || 'minutes';
44
+ const precisionMinIdx = Math.min(precisionMaxIdx, PRECISION_INDEX[precisionMin] || 6);
45
+ precisionMin = (PRECISION_INDEX_KEYS[PRECISION_INDEX_VALUES.indexOf(precisionMinIdx)] || precisionMin);
45
46
  return validator('isDateString', (input, context, _this) => {
46
47
  const coerce = options?.coerce ?? context.coerce;
47
- const parsed = coerceDateString(input, options?.trim);
48
+ const parsed = coerceDateString(input, trim ? precisionMax : undefined);
48
49
  if (parsed) {
49
- if (parsed.precision >= precisionMinIdx &&
50
- parsed.precision <= precisionMaxIdx) {
51
- return coerce ? parsed.value : input;
50
+ if (parsed.precision < precisionMinIdx) {
51
+ context.fail(_this, `Minimum date precision should be ${precisionMin}`, input, options);
52
+ }
53
+ if (parsed.precision > precisionMaxIdx) {
54
+ context.fail(_this, `Maximum date precision should be ${precisionMax}`, input, options);
52
55
  }
56
+ return coerce ? parsed.value : input;
53
57
  }
54
- context.fail(_this, `Value is not valid date string` +
55
- (options?.precisionMin || options?.precisionMax
56
- ? ` with required precision`
57
- : ''), input, {
58
- ...options,
59
- });
58
+ context.fail(_this, `Value "${input}" is not a valid date string`, input, options);
60
59
  }, options);
61
60
  }
62
61
  // noinspection RegExpUnnecessaryNonCapturingGroup
63
62
  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,6}))?)?((?:[+-](0[0-9]|1[0-2])(?::(\d{2}))?)|Z)?)?$/;
64
63
  const DATE_PATTERN2 = /^(\d{4,14})(?:\.(\d{1,6}))?(?:(?:([+-])(0[0-9]|1[0-2])(\d{2})?)|Z)?$/;
65
- function coerceDateString(input, precision) {
66
- const precisionIndex = (precision ? PRECISION_INDEX[precision] : 9) || 9;
64
+ function coerceDateString(input, trimPrecision) {
65
+ const precisionIndex = (trimPrecision ? PRECISION_INDEX[trimPrecision] : 9) || 9;
67
66
  let dateParts;
68
67
  let detectedPrecision = 0;
69
68
  if (input instanceof Date || typeof input === 'number') {
@@ -114,8 +113,8 @@ function coerceDateString(input, precision) {
114
113
  }
115
114
  if (!dateParts)
116
115
  return;
117
- detectedPrecision = dateParts.findIndex(v => !v);
118
- if (detectedPrecision === -1)
116
+ detectedPrecision = dateParts[7] ? 8 : dateParts.findIndex(v => !v);
117
+ if (detectedPrecision < 0)
119
118
  detectedPrecision = Math.min(dateParts.length, 8);
120
119
  let value = dateParts[0] || '0000';
121
120
  if (precisionIndex > 1)
@@ -134,7 +133,7 @@ function coerceDateString(input, precision) {
134
133
  value += dateParts[7] || '';
135
134
  return {
136
135
  value,
137
- precision: detectedPrecision || 8,
136
+ precision: Math.min(precisionIndex, detectedPrecision),
138
137
  };
139
138
  }
140
139
  const PRECISION_INDEX = {
@@ -154,6 +153,8 @@ const PRECISION_INDEX = {
154
153
  ms: 7,
155
154
  tz: 8,
156
155
  };
156
+ const PRECISION_INDEX_KEYS = Object.keys(PRECISION_INDEX);
157
+ const PRECISION_INDEX_VALUES = Object.values(PRECISION_INDEX);
157
158
  function setPrecision(d, precision) {
158
159
  switch (precision) {
159
160
  case 'year': {