@naturalcycles/nodejs-lib 15.144.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.
@@ -3,5 +3,6 @@ export * from './ajvValidationError.js';
3
3
  export * from './from-data/generateJsonSchemaFromData.js';
4
4
  export * from './getAjv.js';
5
5
  export * from './jSchema.js';
6
+ export * from './mock/generateMockFromSchema.js';
6
7
  export { jFormats, type JFormat } from './jsonSchemaBuilder.util.js';
7
8
  export { Ajv };
@@ -3,5 +3,6 @@ export * from './ajvValidationError.js';
3
3
  export * from './from-data/generateJsonSchemaFromData.js';
4
4
  export * from './getAjv.js';
5
5
  export * from './jSchema.js';
6
+ export * from './mock/generateMockFromSchema.js';
6
7
  export { jFormats } from './jsonSchemaBuilder.util.js';
7
8
  export { Ajv };
@@ -204,6 +204,15 @@ export declare class JBuilder<OUT, Opt> extends JSchema<OUT, Opt> {
204
204
  deprecated(deprecated?: boolean): this;
205
205
  type(type: string): this;
206
206
  default(v: any): this;
207
+ /**
208
+ * Gives an example of a valid input value. It is not validated.
209
+ * The example must be a plain value: `build()` does not keep class instances.
210
+ *
211
+ * ```
212
+ * const schema = j.string().regex(/^[A-Z]{3}-\d{4}$/).example('ABC-1234')
213
+ * ```
214
+ */
215
+ example(example: any): this;
207
216
  instanceof(of: string): this;
208
217
  /**
209
218
  * @param optionalValues List of values that should be considered/converted as `undefined`.
@@ -611,7 +620,12 @@ export interface JsonSchema<OUT = unknown> {
611
620
  additionalProperties?: boolean | JsonSchema<any>;
612
621
  minProperties?: number;
613
622
  maxProperties?: number;
614
- default?: OUT;
623
+ default?: unknown;
624
+ /**
625
+ * Examples of a valid input value.
626
+ * Annotation only (not validated), lets consumers generate e.g a mock value.
627
+ */
628
+ examples?: unknown[];
615
629
  if?: JsonSchema;
616
630
  then?: JsonSchema;
617
631
  else?: JsonSchema;
@@ -434,6 +434,17 @@ export class JBuilder extends JSchema {
434
434
  default(v) {
435
435
  return this.cloneAndUpdateSchema({ default: v });
436
436
  }
437
+ /**
438
+ * Gives an example of a valid input value. It is not validated.
439
+ * The example must be a plain value: `build()` does not keep class instances.
440
+ *
441
+ * ```
442
+ * const schema = j.string().regex(/^[A-Z]{3}-\d{4}$/).example('ABC-1234')
443
+ * ```
444
+ */
445
+ example(example) {
446
+ return this.cloneAndUpdateSchema({ examples: [example] });
447
+ }
437
448
  instanceof(of) {
438
449
  return this.cloneAndUpdateSchema({ type: 'object', instanceof: of });
439
450
  }
@@ -630,25 +641,25 @@ export class JString extends JBuilder {
630
641
  * Expects the JWT to be signed!
631
642
  */
632
643
  jwt() {
633
- return this.regex(JWT_REGEX, { msg: 'is not a valid JWT format' });
644
+ return this.regex(JWT_REGEX, { msg: 'is not a valid JWT format' }).example('eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyIn0.c2lnbmF0dXJl');
634
645
  }
635
646
  url() {
636
- return this.regex(URL_REGEX, { msg: 'is not a valid URL format' });
647
+ return this.regex(URL_REGEX, { msg: 'is not a valid URL format' }).example('https://example.com');
637
648
  }
638
649
  ipv4() {
639
- return this.regex(IPV4_REGEX, { msg: 'is not a valid IPv4 format' });
650
+ return this.regex(IPV4_REGEX, { msg: 'is not a valid IPv4 format' }).example('192.0.2.1');
640
651
  }
641
652
  ipv6() {
642
- return this.regex(IPV6_REGEX, { msg: 'is not a valid IPv6 format' });
653
+ return this.regex(IPV6_REGEX, { msg: 'is not a valid IPv6 format' }).example('2001:db8::1');
643
654
  }
644
655
  slug() {
645
- return this.regex(SLUG_REGEX, { msg: 'is not a valid slug format' });
656
+ return this.regex(SLUG_REGEX, { msg: 'is not a valid slug format' }).example('my-slug');
646
657
  }
647
658
  /**
648
659
  * Validates the 3-part semver format: `major.minor.patch`, e.g `1.2.3`.
649
660
  */
650
661
  semVer() {
651
- return this.regex(SEMVER_REGEX, { msg: 'is not a valid semver format' });
662
+ return this.regex(SEMVER_REGEX, { msg: 'is not a valid semver format' }).example('1.2.3');
652
663
  }
653
664
  /**
654
665
  * Validates the 4-part version format: `major.minor.patch.build`, e.g `1.2.3.4`.
@@ -656,16 +667,24 @@ export class JString extends JBuilder {
656
667
  * Not a semver by the spec, but is used by e.g .NET and some mobile app versions.
657
668
  */
658
669
  semVer4() {
659
- return this.regex(SEMVER4_REGEX, { msg: 'is not a valid 4-part semver format' });
670
+ return this.regex(SEMVER4_REGEX, {
671
+ msg: 'is not a valid 4-part semver format',
672
+ }).example('1.2.3.4');
660
673
  }
661
674
  languageTag() {
662
- return this.regex(LANGUAGE_TAG_REGEX, { msg: 'is not a valid language format' });
675
+ return this.regex(LANGUAGE_TAG_REGEX, {
676
+ msg: 'is not a valid language format',
677
+ }).example('en-US');
663
678
  }
664
679
  countryCode() {
665
- return this.regex(COUNTRY_CODE_REGEX, { msg: 'is not a valid country code format' });
680
+ return this.regex(COUNTRY_CODE_REGEX, {
681
+ msg: 'is not a valid country code format',
682
+ }).example('US');
666
683
  }
667
684
  currency() {
668
- return this.regex(CURRENCY_REGEX, { msg: 'is not a valid currency format' });
685
+ return this.regex(CURRENCY_REGEX, {
686
+ msg: 'is not a valid currency format',
687
+ }).example('USD');
669
688
  }
670
689
  /**
671
690
  * Validates that the input is a valid IANATimzone value.
@@ -680,10 +699,10 @@ export class JString extends JBuilder {
680
699
  base64Url() {
681
700
  return this.regex(BASE64URL_REGEX, {
682
701
  msg: 'contains characters not allowed in Base64 URL characterset',
683
- });
702
+ }).example('abc-DEF_123');
684
703
  }
685
704
  uuid() {
686
- return this.regex(UUID_REGEX, { msg: 'is an invalid UUID' });
705
+ return this.regex(UUID_REGEX, { msg: 'is an invalid UUID' }).example('123e4567-e89b-12d3-a456-426614174000');
687
706
  }
688
707
  }
689
708
  export class JIsoDate extends JBuilder {
@@ -10,6 +10,7 @@ export const JSON_SCHEMA_ORDER = [
10
10
  'writeOnly',
11
11
  'type',
12
12
  'default',
13
+ 'examples',
13
14
  // Object,
14
15
  'properties',
15
16
  'required',
@@ -0,0 +1,71 @@
1
+ import type { JSchema } from '../jSchema.js';
2
+ /**
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, except an empty list or object as default.
5
+ * A string with a pattern needs an example or a default.
6
+ *
7
+ * ```
8
+ * const account = generateMockFromSchema(accountSchema, { allFields: true })
9
+ * ```
10
+ *
11
+ * @experimental
12
+ */
13
+ export declare function generateMockFromSchema<OUT>(schema: JSchema<OUT, any>, opt?: GenerateMockFromSchemaOptions): OUT;
14
+ /**
15
+ * Generates the mock of `generateMockFromSchema` with `allFields`, plus one variant for each other
16
+ * branch of each `j.anyOfThese` and `j.anyOfBy` union, and for each other property of each group
17
+ * of exclusive properties, so every field appears in some variant.
18
+ * `j.anyOf` and `j.oneOf` always take their first branch, since their branches add no fields.
19
+ * With `maxProperties`, only the first fields up to the limit appear,
20
+ * e.g `b` never appears for `{ a?, b? }.maxProperties(1)`. With overlapping groups of exclusive
21
+ * properties, a property shared by two groups may not appear. When an earlier `j.anyOfThese` branch
22
+ * also matches a later branch's value, validation drops the later branch's extra fields, so put the
23
+ * wider branch first.
24
+ *
25
+ * ```
26
+ * const variants = generateMockVariantsFromSchema(accountSchema)
27
+ * // [{ value }, { value, choice: { path: 'address.taxId', option: 1, key: 'mx_rfc' } }]
28
+ * ```
29
+ *
30
+ * @experimental
31
+ */
32
+ export declare function generateMockVariantsFromSchema<OUT>(schema: JSchema<OUT, any>): MockVariant<OUT>[];
33
+ /**
34
+ * @experimental
35
+ */
36
+ export interface GenerateMockFromSchemaOptions {
37
+ /**
38
+ * Fills every optional field, and puts one item in each list and one key in each map.
39
+ * Default: false, only the required fields.
40
+ */
41
+ allFields?: boolean;
42
+ }
43
+ /**
44
+ * @experimental
45
+ */
46
+ export interface MockVariant<OUT> {
47
+ value: OUT;
48
+ /**
49
+ * The choice this variant changes: a branch of a union, or a property of a group of exclusive
50
+ * properties. Absent for the first variant.
51
+ */
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;
71
+ }