@naturalcycles/nodejs-lib 15.145.0 → 15.146.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/dist/csv/csvReader.js +2 -1
- package/dist/fs/json2env.js +2 -3
- package/dist/util/env.util.js +2 -2
- package/dist/validation/ajv/mock/generateMockFromSchema.d.ts +20 -16
- package/dist/validation/ajv/mock/generateMockFromSchema.js +13 -2
- package/dist/zip/zipReader.js +2 -6
- package/package.json +2 -2
- package/src/csv/csvReader.ts +2 -1
- package/src/fs/json2env.ts +2 -3
- package/src/util/env.util.ts +2 -1
- package/src/validation/ajv/mock/generateMockFromSchema.ts +35 -18
- package/src/zip/zipReader.ts +5 -6
package/dist/csv/csvReader.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// Inspired by: https://gist.github.com/Jezternz/c8e9fafc2c114e079829974e3764db75
|
|
2
|
+
import { _last } from '@naturalcycles/js-lib/array/array.util.js';
|
|
2
3
|
import { _assert } from '@naturalcycles/js-lib/error/assert.js';
|
|
3
4
|
// export class CSVReader {
|
|
4
5
|
// constructor (cfg: CSVReaderConfig) {
|
|
@@ -37,7 +38,7 @@ export function csvStringToArray(str) {
|
|
|
37
38
|
if (matches[1].length && matches[1] !== ',') {
|
|
38
39
|
arr.push([]);
|
|
39
40
|
}
|
|
40
|
-
arr
|
|
41
|
+
_last(arr).push(matches[2] ? matches[2].replaceAll('""', '"') : matches[3]);
|
|
41
42
|
}
|
|
42
43
|
return arr;
|
|
43
44
|
}
|
package/dist/fs/json2env.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
|
+
import { _assert } from '@naturalcycles/js-lib/error/assert.js';
|
|
2
3
|
import { dimGrey } from '../colors/colors.js';
|
|
3
4
|
import { fs2 } from './fs2.js';
|
|
4
5
|
const JSON2ENV_OPT_DEF = {
|
|
@@ -13,9 +14,7 @@ export function json2env(opt) {
|
|
|
13
14
|
...opt,
|
|
14
15
|
};
|
|
15
16
|
if (!fs2.pathExists(jsonPath)) {
|
|
16
|
-
|
|
17
|
-
throw new Error(`Path doesn't exist: ${jsonPath}`);
|
|
18
|
-
}
|
|
17
|
+
_assert(!fail, `Path doesn't exist: ${jsonPath}`);
|
|
19
18
|
if (!silent) {
|
|
20
19
|
console.log(`json2env input file doesn't exist, skipping without error (${jsonPath})`);
|
|
21
20
|
}
|
package/dist/util/env.util.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { _assert } from '@naturalcycles/js-lib/error/assert.js';
|
|
1
2
|
import { fs2 } from '../fs/fs2.js';
|
|
2
3
|
/**
|
|
3
4
|
* @example
|
|
@@ -10,8 +11,7 @@ export function requireEnvKeys(...keys) {
|
|
|
10
11
|
// oxlint-disable-next-line unicorn/no-array-reduce
|
|
11
12
|
return keys.reduce((r, k) => {
|
|
12
13
|
const v = process.env[k];
|
|
13
|
-
|
|
14
|
-
throw new Error(`${k} env variable is required, but missing`);
|
|
14
|
+
_assert(v, `${k} env variable is required, but missing`);
|
|
15
15
|
r[k] = v;
|
|
16
16
|
return r;
|
|
17
17
|
}, {});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { JSchema } from '../jSchema.js';
|
|
2
2
|
/**
|
|
3
3
|
* Generates a value that passes the schema, e.g for a mock in a test.
|
|
4
|
-
* Uses the example or default of a field when it has one.
|
|
4
|
+
* Uses the example or default of a field when it has one, except an empty list or object as default.
|
|
5
5
|
* A string with a pattern needs an example or a default.
|
|
6
6
|
*
|
|
7
7
|
* ```
|
|
@@ -49,19 +49,23 @@ export interface MockVariant<OUT> {
|
|
|
49
49
|
* The choice this variant changes: a branch of a union, or a property of a group of exclusive
|
|
50
50
|
* properties. Absent for the first variant.
|
|
51
51
|
*/
|
|
52
|
-
choice?:
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
52
|
+
choice?: MockChoice;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* @experimental
|
|
56
|
+
*/
|
|
57
|
+
export interface MockChoice {
|
|
58
|
+
/**
|
|
59
|
+
* Path of the union, or of the object with the group, e.g `address.taxId`, empty at the root.
|
|
60
|
+
* Two unions can share a path when one is directly inside a branch of the other.
|
|
61
|
+
*/
|
|
62
|
+
path: string;
|
|
63
|
+
/**
|
|
64
|
+
* Index of the branch, counting only branches that are not `null`, or of the property in the group.
|
|
65
|
+
*/
|
|
66
|
+
option: number;
|
|
67
|
+
/**
|
|
68
|
+
* Key of the branch in a `j.anyOfBy` union, or the property of the group.
|
|
69
|
+
*/
|
|
70
|
+
key?: string;
|
|
67
71
|
}
|
|
@@ -4,7 +4,7 @@ import { _mapValues, Set2 } from '@naturalcycles/js-lib/object';
|
|
|
4
4
|
import { _stringMapEntries } from '@naturalcycles/js-lib/types';
|
|
5
5
|
/**
|
|
6
6
|
* Generates a value that passes the schema, e.g for a mock in a test.
|
|
7
|
-
* Uses the example or default of a field when it has one.
|
|
7
|
+
* Uses the example or default of a field when it has one, except an empty list or object as default.
|
|
8
8
|
* A string with a pattern needs an example or a default.
|
|
9
9
|
*
|
|
10
10
|
* ```
|
|
@@ -73,8 +73,10 @@ function generateValue(node, path, ctx) {
|
|
|
73
73
|
function generateValueFromKeywords(node, path, ctx) {
|
|
74
74
|
if (node.examples?.length)
|
|
75
75
|
return copyPlainValue(node.examples[0]);
|
|
76
|
-
|
|
76
|
+
// An empty list or object as the default would leave nothing to show, e.g with `allFields`
|
|
77
|
+
if (node.default !== undefined && !isEmptyCollection(node.default)) {
|
|
77
78
|
return copyPlainValue(node.default);
|
|
79
|
+
}
|
|
78
80
|
if (node.isUndefined)
|
|
79
81
|
return undefined;
|
|
80
82
|
if (node.enum)
|
|
@@ -118,6 +120,11 @@ function assertSupportedKeywords(node, path) {
|
|
|
118
120
|
}
|
|
119
121
|
}
|
|
120
122
|
}
|
|
123
|
+
function isEmptyCollection(value) {
|
|
124
|
+
if (Array.isArray(value))
|
|
125
|
+
return !value.length;
|
|
126
|
+
return _isPlainObject(value) && !Object.keys(value).length;
|
|
127
|
+
}
|
|
121
128
|
/**
|
|
122
129
|
* Copies plain objects, arrays and sets, so a variant does not share them with the schema
|
|
123
130
|
* or with other variants.
|
|
@@ -377,6 +384,10 @@ function generateObject(node, path, ctx) {
|
|
|
377
384
|
assertKeyIsFree(node, result, key, path);
|
|
378
385
|
result[key] = generateValue(node.additionalProperties, joinPath(path, key), ctx);
|
|
379
386
|
}
|
|
387
|
+
// An object of any shape, e.g `j.object.any()`, gets one entry like a map
|
|
388
|
+
if (node.additionalProperties === true && !Object.keys(properties).length) {
|
|
389
|
+
result['key'] = 'mock';
|
|
390
|
+
}
|
|
380
391
|
return result;
|
|
381
392
|
}
|
|
382
393
|
/**
|
package/dist/zip/zipReader.js
CHANGED
|
@@ -391,9 +391,7 @@ function decodeBuffer(buf, hasUtf8Flag) {
|
|
|
391
391
|
}
|
|
392
392
|
function assertPathInside(root, fileName) {
|
|
393
393
|
const targetPath = path.resolve(root, fileName);
|
|
394
|
-
|
|
395
|
-
throw new Error(`zip entry escapes destination directory: ${fileName}`);
|
|
396
|
-
}
|
|
394
|
+
_assert(targetPath === root || targetPath.startsWith(root + path.sep), `zip entry escapes destination directory: ${fileName}`);
|
|
397
395
|
return targetPath;
|
|
398
396
|
}
|
|
399
397
|
class FileSource {
|
|
@@ -413,9 +411,7 @@ class FileSource {
|
|
|
413
411
|
let read = 0;
|
|
414
412
|
while (read < length) {
|
|
415
413
|
const { bytesRead } = await this.fileHandle.read(buf, read, length - read, position + read);
|
|
416
|
-
|
|
417
|
-
throw new Error(`unexpected EOF: read ${read} of ${length} bytes at offset ${position}`);
|
|
418
|
-
}
|
|
414
|
+
_assert(bytesRead, `unexpected EOF: read ${read} of ${length} bytes at offset ${position}`);
|
|
419
415
|
read += bytesRead;
|
|
420
416
|
}
|
|
421
417
|
return buf;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/nodejs-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "15.
|
|
4
|
+
"version": "15.146.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@naturalcycles/js-lib": "^15",
|
|
7
7
|
"@standard-schema/spec": "^1",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"yaml": "^2"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"@naturalcycles/dev-lib": "
|
|
17
|
+
"@naturalcycles/dev-lib": "20.73.0",
|
|
18
18
|
"typescript": "^7"
|
|
19
19
|
},
|
|
20
20
|
"exports": {
|
package/src/csv/csvReader.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Inspired by: https://gist.github.com/Jezternz/c8e9fafc2c114e079829974e3764db75
|
|
2
2
|
|
|
3
|
+
import { _last } from '@naturalcycles/js-lib/array/array.util.js'
|
|
3
4
|
import { _assert } from '@naturalcycles/js-lib/error/assert.js'
|
|
4
5
|
import type { AnyObject } from '@naturalcycles/js-lib/types'
|
|
5
6
|
|
|
@@ -65,7 +66,7 @@ export function csvStringToArray(str: string): string[][] {
|
|
|
65
66
|
if (matches[1]!.length && matches[1] !== ',') {
|
|
66
67
|
arr.push([])
|
|
67
68
|
}
|
|
68
|
-
arr
|
|
69
|
+
_last(arr).push(matches[2] ? matches[2].replaceAll('""', '"') : matches[3])
|
|
69
70
|
}
|
|
70
71
|
return arr
|
|
71
72
|
}
|
package/src/fs/json2env.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import fs from 'node:fs'
|
|
2
|
+
import { _assert } from '@naturalcycles/js-lib/error/assert.js'
|
|
2
3
|
import type { AnyObject } from '@naturalcycles/js-lib/types'
|
|
3
4
|
import { dimGrey } from '../colors/colors.js'
|
|
4
5
|
import { fs2 } from './fs2.js'
|
|
@@ -47,9 +48,7 @@ export function json2env(opt: Json2EnvOptions): void {
|
|
|
47
48
|
}
|
|
48
49
|
|
|
49
50
|
if (!fs2.pathExists(jsonPath)) {
|
|
50
|
-
|
|
51
|
-
throw new Error(`Path doesn't exist: ${jsonPath}`)
|
|
52
|
-
}
|
|
51
|
+
_assert(!fail, `Path doesn't exist: ${jsonPath}`)
|
|
53
52
|
|
|
54
53
|
if (!silent) {
|
|
55
54
|
console.log(`json2env input file doesn't exist, skipping without error (${jsonPath})`)
|
package/src/util/env.util.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { _assert } from '@naturalcycles/js-lib/error/assert.js'
|
|
1
2
|
import type { ValuesOf } from '@naturalcycles/js-lib/types'
|
|
2
3
|
import { fs2 } from '../fs/fs2.js'
|
|
3
4
|
|
|
@@ -15,7 +16,7 @@ export function requireEnvKeys<T extends readonly string[]>(
|
|
|
15
16
|
return keys.reduce(
|
|
16
17
|
(r, k) => {
|
|
17
18
|
const v = process.env[k]
|
|
18
|
-
|
|
19
|
+
_assert(v, `${k} env variable is required, but missing`)
|
|
19
20
|
r[k as ValuesOf<T>] = v
|
|
20
21
|
return r
|
|
21
22
|
},
|
|
@@ -7,7 +7,7 @@ import type { JSchema, JsonSchema, JsonSchemaIsoDateOptions } from '../jSchema.j
|
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Generates a value that passes the schema, e.g for a mock in a test.
|
|
10
|
-
* Uses the example or default of a field when it has one.
|
|
10
|
+
* Uses the example or default of a field when it has one, except an empty list or object as default.
|
|
11
11
|
* A string with a pattern needs an example or a default.
|
|
12
12
|
*
|
|
13
13
|
* ```
|
|
@@ -87,7 +87,10 @@ function generateValue(node: JsonSchema, path: string, ctx: GenerationContext):
|
|
|
87
87
|
|
|
88
88
|
function generateValueFromKeywords(node: JsonSchema, path: string, ctx: GenerationContext): any {
|
|
89
89
|
if (node.examples?.length) return copyPlainValue(node.examples[0])
|
|
90
|
-
|
|
90
|
+
// An empty list or object as the default would leave nothing to show, e.g with `allFields`
|
|
91
|
+
if (node.default !== undefined && !isEmptyCollection(node.default)) {
|
|
92
|
+
return copyPlainValue(node.default)
|
|
93
|
+
}
|
|
91
94
|
if (node.isUndefined) return undefined
|
|
92
95
|
if (node.enum) return node.enum[0]
|
|
93
96
|
if (node.ifDefined) return generateValue(node.ifDefined, path, ctx)
|
|
@@ -123,6 +126,11 @@ function assertSupportedKeywords(node: JsonSchema, path: string): void {
|
|
|
123
126
|
}
|
|
124
127
|
}
|
|
125
128
|
|
|
129
|
+
function isEmptyCollection(value: unknown): boolean {
|
|
130
|
+
if (Array.isArray(value)) return !value.length
|
|
131
|
+
return _isPlainObject(value) && !Object.keys(value).length
|
|
132
|
+
}
|
|
133
|
+
|
|
126
134
|
/**
|
|
127
135
|
* Copies plain objects, arrays and sets, so a variant does not share them with the schema
|
|
128
136
|
* or with other variants.
|
|
@@ -389,6 +397,10 @@ function generateObject(node: JsonSchema, path: string, ctx: GenerationContext):
|
|
|
389
397
|
assertKeyIsFree(node, result, key, path)
|
|
390
398
|
result[key] = generateValue(node.additionalProperties, joinPath(path, key), ctx)
|
|
391
399
|
}
|
|
400
|
+
// An object of any shape, e.g `j.object.any()`, gets one entry like a map
|
|
401
|
+
if (node.additionalProperties === true && !Object.keys(properties).length) {
|
|
402
|
+
result['key'] = 'mock'
|
|
403
|
+
}
|
|
392
404
|
|
|
393
405
|
return result
|
|
394
406
|
}
|
|
@@ -573,21 +585,26 @@ export interface MockVariant<OUT> {
|
|
|
573
585
|
* The choice this variant changes: a branch of a union, or a property of a group of exclusive
|
|
574
586
|
* properties. Absent for the first variant.
|
|
575
587
|
*/
|
|
576
|
-
choice?:
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
588
|
+
choice?: MockChoice
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* @experimental
|
|
593
|
+
*/
|
|
594
|
+
export interface MockChoice {
|
|
595
|
+
/**
|
|
596
|
+
* Path of the union, or of the object with the group, e.g `address.taxId`, empty at the root.
|
|
597
|
+
* Two unions can share a path when one is directly inside a branch of the other.
|
|
598
|
+
*/
|
|
599
|
+
path: string
|
|
600
|
+
/**
|
|
601
|
+
* Index of the branch, counting only branches that are not `null`, or of the property in the group.
|
|
602
|
+
*/
|
|
603
|
+
option: number
|
|
604
|
+
/**
|
|
605
|
+
* Key of the branch in a `j.anyOfBy` union, or the property of the group.
|
|
606
|
+
*/
|
|
607
|
+
key?: string
|
|
591
608
|
}
|
|
592
609
|
|
|
593
610
|
interface GenerationContext {
|
|
@@ -629,5 +646,5 @@ interface UnionBranch extends ChoiceOption {
|
|
|
629
646
|
|
|
630
647
|
interface QueuedVariant {
|
|
631
648
|
choices: Map<ChoicePoint, number>
|
|
632
|
-
choice?:
|
|
649
|
+
choice?: MockChoice
|
|
633
650
|
}
|
package/src/zip/zipReader.ts
CHANGED
|
@@ -477,9 +477,10 @@ function decodeBuffer(buf: Buffer, hasUtf8Flag: boolean): string {
|
|
|
477
477
|
|
|
478
478
|
function assertPathInside(root: string, fileName: string): string {
|
|
479
479
|
const targetPath = path.resolve(root, fileName)
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
480
|
+
_assert(
|
|
481
|
+
targetPath === root || targetPath.startsWith(root + path.sep),
|
|
482
|
+
`zip entry escapes destination directory: ${fileName}`,
|
|
483
|
+
)
|
|
483
484
|
return targetPath
|
|
484
485
|
}
|
|
485
486
|
|
|
@@ -510,9 +511,7 @@ class FileSource implements ZipSource {
|
|
|
510
511
|
let read = 0
|
|
511
512
|
while (read < length) {
|
|
512
513
|
const { bytesRead } = await this.fileHandle.read(buf, read, length - read, position + read)
|
|
513
|
-
|
|
514
|
-
throw new Error(`unexpected EOF: read ${read} of ${length} bytes at offset ${position}`)
|
|
515
|
-
}
|
|
514
|
+
_assert(bytesRead, `unexpected EOF: read ${read} of ${length} bytes at offset ${position}`)
|
|
516
515
|
read += bytesRead
|
|
517
516
|
}
|
|
518
517
|
return buf
|