@naturalcycles/nodejs-lib 15.122.0 → 15.122.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.
@@ -543,10 +543,15 @@ export class JString extends JBuilder {
543
543
  }
544
544
  pattern(pattern, opt) {
545
545
  const clone = this.cloneAndUpdateSchema({ pattern });
546
- if (opt?.name)
547
- clone.setErrorMessage('pattern', `is not a valid ${opt.name}`);
548
- if (opt?.msg)
546
+ if (opt?.msg) {
549
547
  clone.setErrorMessage('pattern', opt.msg);
548
+ }
549
+ else if (opt?.name) {
550
+ clone.setErrorMessage('pattern', `is not a valid ${opt.name}`);
551
+ }
552
+ else {
553
+ clone.setErrorMessage('pattern', 'is not matching the expected pattern');
554
+ }
550
555
  return clone;
551
556
  }
552
557
  minLength(minLength) {
@@ -1202,22 +1207,28 @@ function executeValidation(fn, builtSchema, input, opt = {}, defaultInputName) {
1202
1207
  // rewritten into below (a property name may itself contain a dot).
1203
1208
  // Root errors (empty instancePath) resolve to undefined - there the value is the whole Input,
1204
1209
  // which is printed anyway.
1210
+ // `required` errors also resolve to undefined - their instancePath points at the parent object
1211
+ // that lacks the property, so `got:` would print that whole object - noise next to the Input print.
1205
1212
  const failingValueByError = new Map(errors.map(e => [
1206
1213
  e,
1207
- e.instancePath ? getValueAtJsonPointer(inputForPrint, e.instancePath) : undefined,
1214
+ e.instancePath && e.keyword !== 'required'
1215
+ ? getValueAtJsonPointer(inputForPrint, e.instancePath)
1216
+ : undefined,
1208
1217
  ]));
1209
1218
  applyImprovementsOnErrorMessages(errors, builtSchema);
1210
1219
  // Same line format as Ajv's errorsText().
1211
- // Each nested error line additionally ends with the narrow failing value itself (`, got: ...`),
1220
+ // Each error line is followed by a `Got: ...` line printing the narrow failing value itself,
1212
1221
  // so it stays visible even when the Input print below gets truncated for large inputs.
1222
+ // `Got` is a separate line (and is allowed to print multi-line) on purpose: consumers that
1223
+ // only take the first line of the message keep getting the unchanged (pre-Got) error line.
1213
1224
  let message = errors
1214
- .map(e => {
1215
- let line = `${dataVar}${e.instancePath} ${e.message}`;
1225
+ .flatMap(e => {
1226
+ const lines = [`${dataVar}${e.instancePath} ${e.message}`];
1216
1227
  const value = failingValueByError.get(e);
1217
1228
  if (value !== undefined) {
1218
- line += `, got: ${_inspect(value, { maxLen: 500 })}`;
1229
+ lines.push(`Got: ${_inspect(value, { maxLen: 500 })}`);
1219
1230
  }
1220
- return line;
1231
+ return lines;
1221
1232
  })
1222
1233
  .join(separator);
1223
1234
  const inputStringified = _inspect(inputForPrint, { maxLen: 4000 });
@@ -60,8 +60,13 @@ const MAX_DOS_DATE = new Date(2107, 11, 31, 23, 59, 58);
60
60
  * timestamp is rendered in the local timezone. Out-of-range dates are clamped to 1980-2107.
61
61
  */
62
62
  export function unixToDosDateTime(ts) {
63
- const jsDate = new Date(ts * 1000);
64
- const d = jsDate < MIN_DOS_DATE ? MIN_DOS_DATE : jsDate > MAX_DOS_DATE ? MAX_DOS_DATE : jsDate;
63
+ let d = new Date(ts * 1000);
64
+ if (d < MIN_DOS_DATE) {
65
+ d = MIN_DOS_DATE;
66
+ }
67
+ else if (d > MAX_DOS_DATE) {
68
+ d = MAX_DOS_DATE;
69
+ }
65
70
  const date = (d.getDate() & 0x1f) | // 1-31
66
71
  (((d.getMonth() + 1) & 0x0f) << 5) | // 1-12
67
72
  (((d.getFullYear() - 1980) & 0x7f) << 9); // 1980-2107
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.122.0",
4
+ "version": "15.122.2",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@standard-schema/spec": "^1",
@@ -734,8 +734,14 @@ export class JString<
734
734
 
735
735
  pattern(pattern: string, opt?: JsonBuilderRuleOpt): this {
736
736
  const clone = this.cloneAndUpdateSchema({ pattern })
737
- if (opt?.name) clone.setErrorMessage('pattern', `is not a valid ${opt.name}`)
738
- if (opt?.msg) clone.setErrorMessage('pattern', opt.msg)
737
+ if (opt?.msg) {
738
+ clone.setErrorMessage('pattern', opt.msg)
739
+ } else if (opt?.name) {
740
+ clone.setErrorMessage('pattern', `is not a valid ${opt.name}`)
741
+ } else {
742
+ clone.setErrorMessage('pattern', 'is not matching the expected pattern')
743
+ }
744
+
739
745
  return clone
740
746
  }
741
747
 
@@ -1732,26 +1738,32 @@ function executeValidation<OUT>(
1732
1738
  // rewritten into below (a property name may itself contain a dot).
1733
1739
  // Root errors (empty instancePath) resolve to undefined - there the value is the whole Input,
1734
1740
  // which is printed anyway.
1741
+ // `required` errors also resolve to undefined - their instancePath points at the parent object
1742
+ // that lacks the property, so `got:` would print that whole object - noise next to the Input print.
1735
1743
  const failingValueByError = new Map<ErrorObject, unknown>(
1736
1744
  errors.map(e => [
1737
1745
  e,
1738
- e.instancePath ? getValueAtJsonPointer(inputForPrint, e.instancePath) : undefined,
1746
+ e.instancePath && e.keyword !== 'required'
1747
+ ? getValueAtJsonPointer(inputForPrint, e.instancePath)
1748
+ : undefined,
1739
1749
  ]),
1740
1750
  )
1741
1751
 
1742
1752
  applyImprovementsOnErrorMessages(errors, builtSchema)
1743
1753
 
1744
1754
  // Same line format as Ajv's errorsText().
1745
- // Each nested error line additionally ends with the narrow failing value itself (`, got: ...`),
1755
+ // Each error line is followed by a `Got: ...` line printing the narrow failing value itself,
1746
1756
  // so it stays visible even when the Input print below gets truncated for large inputs.
1757
+ // `Got` is a separate line (and is allowed to print multi-line) on purpose: consumers that
1758
+ // only take the first line of the message keep getting the unchanged (pre-Got) error line.
1747
1759
  let message = errors
1748
- .map(e => {
1749
- let line = `${dataVar}${e.instancePath} ${e.message}`
1760
+ .flatMap(e => {
1761
+ const lines = [`${dataVar}${e.instancePath} ${e.message}`]
1750
1762
  const value = failingValueByError.get(e)
1751
1763
  if (value !== undefined) {
1752
- line += `, got: ${_inspect(value, { maxLen: 500 })}`
1764
+ lines.push(`Got: ${_inspect(value, { maxLen: 500 })}`)
1753
1765
  }
1754
- return line
1766
+ return lines
1755
1767
  })
1756
1768
  .join(separator)
1757
1769
 
@@ -76,8 +76,12 @@ const MAX_DOS_DATE = new Date(2107, 11, 31, 23, 59, 58)
76
76
  * timestamp is rendered in the local timezone. Out-of-range dates are clamped to 1980-2107.
77
77
  */
78
78
  export function unixToDosDateTime(ts: UnixTimestamp): { date: number; time: number } {
79
- const jsDate = new Date(ts * 1000)
80
- const d = jsDate < MIN_DOS_DATE ? MIN_DOS_DATE : jsDate > MAX_DOS_DATE ? MAX_DOS_DATE : jsDate
79
+ let d = new Date(ts * 1000)
80
+ if (d < MIN_DOS_DATE) {
81
+ d = MIN_DOS_DATE
82
+ } else if (d > MAX_DOS_DATE) {
83
+ d = MAX_DOS_DATE
84
+ }
81
85
 
82
86
  const date =
83
87
  (d.getDate() & 0x1f) | // 1-31