@naturalcycles/nodejs-lib 15.145.0 → 15.145.1

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.
@@ -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
- * Path of the union, or of the object with the group, e.g `address.taxId`, empty at the root.
55
- * Two unions can share a path when one is directly inside a branch of the other.
56
- */
57
- path: string;
58
- /**
59
- * Index of the branch, counting only branches that are not `null`, or of the property in the group.
60
- */
61
- option: number;
62
- /**
63
- * Key of the branch in a `j.anyOfBy` union, or the property of the group.
64
- */
65
- key?: string;
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
- if (node.default !== undefined)
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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.145.0",
4
+ "version": "15.145.1",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@standard-schema/spec": "^1",
@@ -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
- if (node.default !== undefined) return copyPlainValue(node.default)
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
- * Path of the union, or of the object with the group, e.g `address.taxId`, empty at the root.
579
- * Two unions can share a path when one is directly inside a branch of the other.
580
- */
581
- path: string
582
- /**
583
- * Index of the branch, counting only branches that are not `null`, or of the property in the group.
584
- */
585
- option: number
586
- /**
587
- * Key of the branch in a `j.anyOfBy` union, or the property of the group.
588
- */
589
- key?: string
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?: MockVariant<unknown>['choice']
649
+ choice?: MockChoice
633
650
  }