@naturalcycles/nodejs-lib 15.44.0 → 15.46.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.
@@ -14,6 +14,7 @@ import { _deepCopy, _sortObject } from '@naturalcycles/js-lib/object'
14
14
  import {
15
15
  type AnyObject,
16
16
  type IANATimezone,
17
+ type Inclusiveness,
17
18
  type IsoDate,
18
19
  type IsoDateTime,
19
20
  JWT_REGEX,
@@ -23,6 +24,7 @@ import {
23
24
  type UnixTimestamp,
24
25
  type UnixTimestampMillis,
25
26
  } from '@naturalcycles/js-lib/types'
27
+ import { TIMEZONES } from '../timezones.js'
26
28
  import { JSON_SCHEMA_ORDER, mergeJsonSchemaObjects } from './jsonSchemaBuilder.util.js'
27
29
 
28
30
  export const j = {
@@ -248,17 +250,18 @@ export class JsonSchemaStringBuilder<
248
250
  }
249
251
 
250
252
  pattern(pattern: string, opt?: JsonBuilderRuleOpt): this {
253
+ if (opt?.name) this.setErrorMessage('pattern', `is not a valid ${opt.name}`)
251
254
  if (opt?.msg) this.setErrorMessage('pattern', opt.msg)
252
255
  Object.assign(this.schema, { pattern })
253
256
  return this
254
257
  }
255
258
 
256
- min(minLength: number): this {
259
+ minLength(minLength: number): this {
257
260
  Object.assign(this.schema, { minLength })
258
261
  return this
259
262
  }
260
263
 
261
- max(maxLength: number): this {
264
+ maxLength(maxLength: number): this {
262
265
  Object.assign(this.schema, { maxLength })
263
266
  return this
264
267
  }
@@ -380,9 +383,7 @@ export class JsonSchemaStringBuilder<
380
383
  */
381
384
  ianaTimezone(): JsonSchemaEnumBuilder<string | IANATimezone, IANATimezone, false> {
382
385
  // UTC is added to assist unit-testing, which uses UTC by default (not technically a valid Iana timezone identifier)
383
- return j
384
- .enum([...Intl.supportedValuesOf('timeZone'), 'UTC'], { msg: 'is an invalid IANA timezone' })
385
- .branded<IANATimezone>()
386
+ return j.enum(TIMEZONES, { msg: 'is an invalid IANA timezone' }).branded<IANATimezone>()
386
387
  }
387
388
  }
388
389
 
@@ -435,12 +436,31 @@ export class JsonSchemaNumberBuilder<
435
436
  return this
436
437
  }
437
438
 
438
- /**
439
- * Both ranges are inclusive.
440
- */
441
- range(minimum: number, maximum: number): this {
442
- Object.assign(this.schema, { minimum, maximum })
443
- return this
439
+ lessThan(value: number): this {
440
+ return this.exclusiveMax(value)
441
+ }
442
+
443
+ lessThanOrEqual(value: number): this {
444
+ return this.max(value)
445
+ }
446
+
447
+ moreThan(value: number): this {
448
+ return this.exclusiveMin(value)
449
+ }
450
+
451
+ moreThanOrEqual(value: number): this {
452
+ return this.min(value)
453
+ }
454
+
455
+ equal(value: number): this {
456
+ return this.min(value).max(value)
457
+ }
458
+
459
+ range(minimum: number, maximum: number, incl: Inclusiveness): this {
460
+ if (incl === '[)') {
461
+ return this.moreThanOrEqual(minimum).lessThan(maximum)
462
+ }
463
+ return this.moreThanOrEqual(minimum).lessThanOrEqual(maximum)
444
464
  }
445
465
 
446
466
  int32(): this {
@@ -709,16 +729,24 @@ export class JsonSchemaArrayBuilder<IN, OUT, Opt> extends JsonSchemaAnyBuilder<I
709
729
  })
710
730
  }
711
731
 
712
- min(minItems: number): this {
732
+ minLength(minItems: number): this {
713
733
  Object.assign(this.schema, { minItems })
714
734
  return this
715
735
  }
716
736
 
717
- max(maxItems: number): this {
737
+ maxLength(maxItems: number): this {
718
738
  Object.assign(this.schema, { maxItems })
719
739
  return this
720
740
  }
721
741
 
742
+ length(minItems: number, maxItems: number): this {
743
+ return this.minLength(minItems).maxLength(maxItems)
744
+ }
745
+
746
+ exactLength(length: number): this {
747
+ return this.minLength(length).maxLength(length)
748
+ }
749
+
722
750
  unique(uniqueItems: number): this {
723
751
  Object.assign(this.schema, { uniqueItems })
724
752
  return this
@@ -767,7 +795,7 @@ export class JsonSchemaEnumBuilder<
767
795
  > extends JsonSchemaAnyBuilder<IN, OUT, Opt> {
768
796
  constructor(enumValues: readonly IN[], opt?: JsonBuilderRuleOpt) {
769
797
  super({ enum: enumValues })
770
-
798
+ if (opt?.name) this.setErrorMessage('pattern', `is not a valid ${opt.name}`)
771
799
  if (opt?.msg) this.setErrorMessage('enum', opt.msg)
772
800
  }
773
801
 
@@ -900,5 +928,16 @@ type BuilderInUnion<B extends readonly JsonSchemaAnyBuilder<any, any, any>[]> =
900
928
  }[number]
901
929
 
902
930
  interface JsonBuilderRuleOpt {
931
+ /**
932
+ * Text of error message to return when the validation fails for the given rule:
933
+ *
934
+ * `{ msg: "is not a valid Oompa-loompa" } => "Object.property is not a valid Oompa-loompa"`
935
+ */
903
936
  msg?: string
937
+ /**
938
+ * A friendly name for what we are validating, that will be used in error messages:
939
+ *
940
+ * `{ name: "Oompa-loompa" } => "Object.property is not a valid Oompa-loompa"`
941
+ */
942
+ name?: string
904
943
  }