@naturalcycles/nodejs-lib 15.56.0 → 15.56.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.
@@ -218,8 +218,16 @@ export class JsonSchemaStringBuilder extends JsonSchemaAnyBuilder {
218
218
  * due to how mutability works in Ajv.
219
219
  */
220
220
  optional(optionalValues) {
221
- _objectAssign(this.schema, { optionalValues });
222
- return super.optional();
221
+ if (!optionalValues) {
222
+ return super.optional();
223
+ }
224
+ const newBuilder = new JsonSchemaStringBuilder().optional();
225
+ const alternativesSchema = j.enum(optionalValues);
226
+ Object.assign(newBuilder.getSchema(), {
227
+ anyOf: [this.build(), alternativesSchema.build()],
228
+ optionalValues,
229
+ });
230
+ return newBuilder;
223
231
  }
224
232
  regex(pattern, opt) {
225
233
  return this.pattern(pattern.source, opt);
@@ -380,8 +388,16 @@ export class JsonSchemaNumberBuilder extends JsonSchemaAnyBuilder {
380
388
  * due to how mutability works in Ajv.
381
389
  */
382
390
  optional(optionalValues) {
383
- _objectAssign(this.schema, { optionalValues });
384
- return super.optional();
391
+ if (!optionalValues) {
392
+ return super.optional();
393
+ }
394
+ const newBuilder = new JsonSchemaNumberBuilder().optional();
395
+ const alternativesSchema = j.enum(optionalValues);
396
+ Object.assign(newBuilder.getSchema(), {
397
+ anyOf: [this.build(), alternativesSchema.build()],
398
+ optionalValues,
399
+ });
400
+ return newBuilder;
385
401
  }
386
402
  integer() {
387
403
  _objectAssign(this.schema, { type: 'integer' });
@@ -488,10 +504,16 @@ export class JsonSchemaBooleanBuilder extends JsonSchemaAnyBuilder {
488
504
  * due to how mutability works in Ajv.
489
505
  */
490
506
  optional(optionalValue) {
491
- if (typeof optionalValue !== 'undefined') {
492
- _objectAssign(this.schema, { optionalValues: [optionalValue] });
507
+ if (typeof optionalValue === 'undefined') {
508
+ return super.optional();
493
509
  }
494
- return super.optional();
510
+ const newBuilder = new JsonSchemaBooleanBuilder().optional();
511
+ const alternativesSchema = j.enum([optionalValue]);
512
+ Object.assign(newBuilder.getSchema(), {
513
+ anyOf: [this.build(), alternativesSchema.build()],
514
+ optionalValues: [optionalValue],
515
+ });
516
+ return newBuilder;
495
517
  }
496
518
  }
497
519
  export class JsonSchemaObjectBuilder extends JsonSchemaAnyBuilder {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.56.0",
4
+ "version": "15.56.1",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -325,12 +325,22 @@ export class JsonSchemaStringBuilder<
325
325
  override optional(
326
326
  optionalValues?: string[],
327
327
  ): JsonSchemaStringBuilder<IN | undefined, OUT | undefined, true> {
328
- _objectAssign(this.schema, { optionalValues })
329
- return super.optional() as unknown as JsonSchemaStringBuilder<
330
- IN | undefined,
331
- OUT | undefined,
332
- true
333
- >
328
+ if (!optionalValues) {
329
+ return super.optional() as unknown as JsonSchemaStringBuilder<
330
+ IN | undefined,
331
+ OUT | undefined,
332
+ true
333
+ >
334
+ }
335
+
336
+ const newBuilder = new JsonSchemaStringBuilder<IN, OUT, Opt>().optional()
337
+ const alternativesSchema = j.enum(optionalValues)
338
+ Object.assign(newBuilder.getSchema(), {
339
+ anyOf: [this.build(), alternativesSchema.build()],
340
+ optionalValues,
341
+ })
342
+
343
+ return newBuilder
334
344
  }
335
345
 
336
346
  regex(pattern: RegExp, opt?: JsonBuilderRuleOpt): this {
@@ -544,12 +554,22 @@ export class JsonSchemaNumberBuilder<
544
554
  override optional(
545
555
  optionalValues?: number[],
546
556
  ): JsonSchemaNumberBuilder<IN | undefined, OUT | undefined, true> {
547
- _objectAssign(this.schema, { optionalValues })
548
- return super.optional() as unknown as JsonSchemaNumberBuilder<
549
- IN | undefined,
550
- OUT | undefined,
551
- true
552
- >
557
+ if (!optionalValues) {
558
+ return super.optional() as unknown as JsonSchemaNumberBuilder<
559
+ IN | undefined,
560
+ OUT | undefined,
561
+ true
562
+ >
563
+ }
564
+
565
+ const newBuilder = new JsonSchemaNumberBuilder<IN, OUT, Opt>().optional()
566
+ const alternativesSchema = j.enum(optionalValues)
567
+ Object.assign(newBuilder.getSchema(), {
568
+ anyOf: [this.build(), alternativesSchema.build()],
569
+ optionalValues,
570
+ })
571
+
572
+ return newBuilder
553
573
  }
554
574
 
555
575
  integer(): this {
@@ -691,15 +711,22 @@ export class JsonSchemaBooleanBuilder<
691
711
  override optional(
692
712
  optionalValue?: boolean,
693
713
  ): JsonSchemaBooleanBuilder<IN | undefined, OUT | undefined, true> {
694
- if (typeof optionalValue !== 'undefined') {
695
- _objectAssign(this.schema, { optionalValues: [optionalValue] })
714
+ if (typeof optionalValue === 'undefined') {
715
+ return super.optional() as unknown as JsonSchemaBooleanBuilder<
716
+ IN | undefined,
717
+ OUT | undefined,
718
+ true
719
+ >
696
720
  }
697
721
 
698
- return super.optional() as unknown as JsonSchemaBooleanBuilder<
699
- IN | undefined,
700
- OUT | undefined,
701
- true
702
- >
722
+ const newBuilder = new JsonSchemaBooleanBuilder<IN, OUT, Opt>().optional()
723
+ const alternativesSchema = j.enum([optionalValue])
724
+ Object.assign(newBuilder.getSchema(), {
725
+ anyOf: [this.build(), alternativesSchema.build()],
726
+ optionalValues: [optionalValue],
727
+ })
728
+
729
+ return newBuilder
703
730
  }
704
731
  }
705
732