@naturalcycles/nodejs-lib 15.88.0 → 15.89.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.
@@ -243,6 +243,18 @@ export interface JsonSchemaStringEmailOptions {
243
243
  }
244
244
  export declare class JsonSchemaIsoDateBuilder<Opt extends boolean = false> extends JsonSchemaAnyBuilder<string | IsoDate, IsoDate, Opt> {
245
245
  constructor();
246
+ /**
247
+ * @param optionalValues List of values that should be considered/converted as `undefined`.
248
+ *
249
+ * This `optionalValues` feature only works when the current schema is nested in an object or array schema,
250
+ * due to how mutability works in Ajv.
251
+ *
252
+ * Make sure this `optional()` call is at the end of your call chain.
253
+ *
254
+ * When `null` is included in optionalValues, the return type becomes `JsonSchemaTerminal`
255
+ * (no further chaining allowed) because the schema is wrapped in an anyOf structure.
256
+ */
257
+ optional<T extends readonly null[] | undefined = undefined>(optionalValues?: T): T extends readonly null[] ? JsonSchemaTerminal<string | IsoDate | undefined, IsoDate | undefined, true> : JsonSchemaIsoDateBuilder<true>;
246
258
  before(date: string): this;
247
259
  sameOrBefore(date: string): this;
248
260
  after(date: string): this;
@@ -508,6 +508,31 @@ export class JsonSchemaIsoDateBuilder extends JsonSchemaAnyBuilder {
508
508
  IsoDate: {},
509
509
  });
510
510
  }
511
+ /**
512
+ * @param optionalValues List of values that should be considered/converted as `undefined`.
513
+ *
514
+ * This `optionalValues` feature only works when the current schema is nested in an object or array schema,
515
+ * due to how mutability works in Ajv.
516
+ *
517
+ * Make sure this `optional()` call is at the end of your call chain.
518
+ *
519
+ * When `null` is included in optionalValues, the return type becomes `JsonSchemaTerminal`
520
+ * (no further chaining allowed) because the schema is wrapped in an anyOf structure.
521
+ */
522
+ optional(optionalValues) {
523
+ if (!optionalValues) {
524
+ return super.optional();
525
+ }
526
+ _typeCast(optionalValues);
527
+ const newBuilder = new JsonSchemaTerminal({
528
+ anyOf: [
529
+ { type: 'null', optionalValues },
530
+ this.cloneAndUpdateSchema({ optionalField: true }).build(),
531
+ ],
532
+ optionalField: true,
533
+ });
534
+ return newBuilder;
535
+ }
511
536
  before(date) {
512
537
  return this.cloneAndUpdateSchema({ IsoDate: { before: date } });
513
538
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.88.0",
4
+ "version": "15.89.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -703,6 +703,43 @@ export class JsonSchemaIsoDateBuilder<Opt extends boolean = false> extends JsonS
703
703
  })
704
704
  }
705
705
 
706
+ /**
707
+ * @param optionalValues List of values that should be considered/converted as `undefined`.
708
+ *
709
+ * This `optionalValues` feature only works when the current schema is nested in an object or array schema,
710
+ * due to how mutability works in Ajv.
711
+ *
712
+ * Make sure this `optional()` call is at the end of your call chain.
713
+ *
714
+ * When `null` is included in optionalValues, the return type becomes `JsonSchemaTerminal`
715
+ * (no further chaining allowed) because the schema is wrapped in an anyOf structure.
716
+ */
717
+ override optional<T extends readonly null[] | undefined = undefined>(
718
+ optionalValues?: T,
719
+ ): T extends readonly null[]
720
+ ? JsonSchemaTerminal<string | IsoDate | undefined, IsoDate | undefined, true>
721
+ : JsonSchemaIsoDateBuilder<true> {
722
+ if (!optionalValues) {
723
+ return super.optional() as any
724
+ }
725
+
726
+ _typeCast<null[]>(optionalValues)
727
+
728
+ const newBuilder = new JsonSchemaTerminal<
729
+ string | IsoDate | undefined,
730
+ IsoDate | undefined,
731
+ true
732
+ >({
733
+ anyOf: [
734
+ { type: 'null', optionalValues },
735
+ this.cloneAndUpdateSchema({ optionalField: true }).build(),
736
+ ],
737
+ optionalField: true,
738
+ })
739
+
740
+ return newBuilder as any
741
+ }
742
+
706
743
  before(date: string): this {
707
744
  return this.cloneAndUpdateSchema({ IsoDate: { before: date } })
708
745
  }