@naturalcycles/nodejs-lib 15.40.0 → 15.42.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.
@@ -4,7 +4,10 @@ export declare const j: {
4
4
  string(): JsonSchemaStringBuilder<string, string, false>;
5
5
  number(): JsonSchemaNumberBuilder<number, number, false>;
6
6
  boolean(): JsonSchemaBooleanBuilder<boolean, boolean, false>;
7
- object<P extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(props: P): JsonSchemaObjectBuilder<P, false>;
7
+ object: typeof object & {
8
+ dbEntity: typeof objectDbEntity;
9
+ };
10
+ objectInfer<P extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(props: P): JsonSchemaObjectInferringBuilder<P, false>;
8
11
  array<IN, OUT, Opt>(itemSchema: JsonSchemaAnyBuilder<IN, OUT, Opt>): JsonSchemaArrayBuilder<IN, OUT, Opt>;
9
12
  set<IN, OUT, Opt>(itemSchema: JsonSchemaAnyBuilder<IN, OUT, Opt>): JsonSchemaSet2Builder<IN, OUT, Opt>;
10
13
  buffer(): JsonSchemaBufferBuilder;
@@ -118,7 +121,20 @@ export declare class JsonSchemaNumberBuilder<IN extends number = number, OUT = I
118
121
  export declare class JsonSchemaBooleanBuilder<IN extends boolean = boolean, OUT = IN, Opt extends boolean = false> extends JsonSchemaAnyBuilder<IN, OUT, Opt> {
119
122
  constructor();
120
123
  }
121
- export declare class JsonSchemaObjectBuilder<PROPS extends Record<string, JsonSchemaAnyBuilder<any, any, any>>, Opt extends boolean = false> extends JsonSchemaAnyBuilder<Expand<{
124
+ export declare class JsonSchemaObjectBuilder<IN extends AnyObject, OUT extends AnyObject, Opt extends boolean = false> extends JsonSchemaAnyBuilder<IN, OUT, Opt> {
125
+ constructor(props?: AnyObject);
126
+ addProperties(props: AnyObject): this;
127
+ /**
128
+ * When set, the validation will not strip away properties that are not specified explicitly in the schema.
129
+ */
130
+ allowAdditionalProperties(): this;
131
+ extend<IN2 extends AnyObject>(props: AnyObject): JsonSchemaObjectBuilder<IN & IN2, OUT & IN2, Opt>;
132
+ /**
133
+ * Extends the current schema with `id`, `created` and `updated` according to NC DB conventions.
134
+ */
135
+ dbEntity(): JsonSchemaObjectBuilder<any, any, Opt>;
136
+ }
137
+ export declare class JsonSchemaObjectInferringBuilder<PROPS extends Record<string, JsonSchemaAnyBuilder<any, any, any>>, Opt extends boolean = false> extends JsonSchemaAnyBuilder<Expand<{
122
138
  [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt> ? IsOpt extends true ? never : K : never]: PROPS[K] extends JsonSchemaAnyBuilder<infer IN, any, any> ? IN : never;
123
139
  } & {
124
140
  [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt> ? IsOpt extends true ? K : never : never]?: PROPS[K] extends JsonSchemaAnyBuilder<infer IN, any, any> ? IN : never;
@@ -133,13 +149,13 @@ export declare class JsonSchemaObjectBuilder<PROPS extends Record<string, JsonSc
133
149
  * When set, the validation will not strip away properties that are not specified explicitly in the schema.
134
150
  */
135
151
  allowAdditionalProperties(): this;
136
- extend<NEW_PROPS extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(props: NEW_PROPS): JsonSchemaObjectBuilder<{
152
+ extend<NEW_PROPS extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(props: NEW_PROPS): JsonSchemaObjectInferringBuilder<{
137
153
  [K in keyof PROPS | keyof NEW_PROPS]: K extends keyof NEW_PROPS ? NEW_PROPS[K] : K extends keyof PROPS ? PROPS[K] : never;
138
154
  }, Opt>;
139
155
  /**
140
156
  * Extends the current schema with `id`, `created` and `updated` according to NC DB conventions.
141
157
  */
142
- dbEntity(): JsonSchemaObjectBuilder<{ [K in keyof PROPS | "id" | "created" | "updated"]: K extends "id" | "created" | "updated" ? {
158
+ dbEntity(): JsonSchemaObjectInferringBuilder<{ [K in keyof PROPS | "id" | "created" | "updated"]: K extends "id" | "created" | "updated" ? {
143
159
  id: JsonSchemaStringBuilder<string, string, false>;
144
160
  created: JsonSchemaNumberBuilder<UnixTimestamp, UnixTimestamp, false>;
145
161
  updated: JsonSchemaNumberBuilder<UnixTimestamp, UnixTimestamp, false>;
@@ -218,6 +234,12 @@ export interface JsonSchema<IN = unknown, OUT = IN> {
218
234
  errorMessages?: StringMap<string>;
219
235
  hasIsOfTypeCheck?: boolean;
220
236
  }
237
+ declare function object(props: AnyObject): never;
238
+ declare function object<IN extends AnyObject>(props: {
239
+ [key in keyof IN]: JsonSchemaAnyBuilder<any, IN[key], any>;
240
+ }): JsonSchemaObjectBuilder<IN, IN, false>;
241
+ declare function objectDbEntity(props?: AnyObject): never;
242
+ declare function objectDbEntity<IN extends AnyObject>(props?: AnyObject): JsonSchemaObjectBuilder<IN, IN, false>;
221
243
  type Expand<T> = T extends infer O ? {
222
244
  [K in keyof O]: O[K];
223
245
  } : never;
@@ -16,8 +16,11 @@ export const j = {
16
16
  boolean() {
17
17
  return new JsonSchemaBooleanBuilder();
18
18
  },
19
- object(props) {
20
- return new JsonSchemaObjectBuilder(props);
19
+ object: Object.assign(object, {
20
+ dbEntity: objectDbEntity,
21
+ }),
22
+ objectInfer(props) {
23
+ return new JsonSchemaObjectInferringBuilder(props);
21
24
  },
22
25
  array(itemSchema) {
23
26
  return new JsonSchemaArrayBuilder(itemSchema);
@@ -370,6 +373,7 @@ export class JsonSchemaObjectBuilder extends JsonSchemaAnyBuilder {
370
373
  properties: {},
371
374
  required: [],
372
375
  additionalProperties: false,
376
+ hasIsOfTypeCheck: true,
373
377
  });
374
378
  if (props)
375
379
  this.addProperties(props);
@@ -417,6 +421,60 @@ export class JsonSchemaObjectBuilder extends JsonSchemaAnyBuilder {
417
421
  });
418
422
  }
419
423
  }
424
+ export class JsonSchemaObjectInferringBuilder extends JsonSchemaAnyBuilder {
425
+ constructor(props) {
426
+ super({
427
+ type: 'object',
428
+ properties: {},
429
+ required: [],
430
+ additionalProperties: false,
431
+ });
432
+ if (props)
433
+ this.addProperties(props);
434
+ }
435
+ addProperties(props) {
436
+ const properties = {};
437
+ const required = [];
438
+ for (const [key, builder] of Object.entries(props)) {
439
+ const schema = builder.build();
440
+ if (!schema.optionalField) {
441
+ required.push(key);
442
+ }
443
+ else {
444
+ schema.optionalField = undefined;
445
+ }
446
+ properties[key] = schema;
447
+ }
448
+ this.schema.properties = properties;
449
+ this.schema.required = _uniq(required).sort();
450
+ return this;
451
+ }
452
+ /**
453
+ * When set, the validation will not strip away properties that are not specified explicitly in the schema.
454
+ */
455
+ allowAdditionalProperties() {
456
+ Object.assign(this.schema, { additionalProperties: true });
457
+ return this;
458
+ }
459
+ extend(props) {
460
+ const newBuilder = new JsonSchemaObjectInferringBuilder();
461
+ Object.assign(newBuilder.schema, _deepCopy(this.schema));
462
+ const incomingSchemaBuilder = new JsonSchemaObjectInferringBuilder(props);
463
+ mergeJsonSchemaObjects(newBuilder.schema, incomingSchemaBuilder.schema);
464
+ return newBuilder;
465
+ }
466
+ /**
467
+ * Extends the current schema with `id`, `created` and `updated` according to NC DB conventions.
468
+ */
469
+ // eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/explicit-module-boundary-types
470
+ dbEntity() {
471
+ return this.extend({
472
+ id: j.string(),
473
+ created: j.number().unixTimestamp2000(),
474
+ updated: j.number().unixTimestamp2000(),
475
+ });
476
+ }
477
+ }
420
478
  export class JsonSchemaArrayBuilder extends JsonSchemaAnyBuilder {
421
479
  constructor(itemsSchema) {
422
480
  super({
@@ -465,3 +523,14 @@ export class JsonSchemaEnumBuilder extends JsonSchemaAnyBuilder {
465
523
  super({ enum: enumValues });
466
524
  }
467
525
  }
526
+ function object(props) {
527
+ return new JsonSchemaObjectBuilder(props);
528
+ }
529
+ function objectDbEntity(props) {
530
+ return j.object({
531
+ id: j.string(),
532
+ created: j.number().unixTimestamp2000(),
533
+ updated: j.number().unixTimestamp2000(),
534
+ ...props,
535
+ });
536
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.40.0",
4
+ "version": "15.42.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -37,10 +37,14 @@ export const j = {
37
37
  return new JsonSchemaBooleanBuilder()
38
38
  },
39
39
 
40
- object<P extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(
40
+ object: Object.assign(object, {
41
+ dbEntity: objectDbEntity,
42
+ }),
43
+
44
+ objectInfer<P extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(
41
45
  props: P,
42
- ): JsonSchemaObjectBuilder<P, false> {
43
- return new JsonSchemaObjectBuilder<P, false>(props)
46
+ ): JsonSchemaObjectInferringBuilder<P, false> {
47
+ return new JsonSchemaObjectInferringBuilder<P, false>(props)
44
48
  },
45
49
 
46
50
  array<IN, OUT, Opt>(
@@ -497,6 +501,76 @@ export class JsonSchemaBooleanBuilder<
497
501
  }
498
502
 
499
503
  export class JsonSchemaObjectBuilder<
504
+ IN extends AnyObject,
505
+ OUT extends AnyObject,
506
+ Opt extends boolean = false,
507
+ > extends JsonSchemaAnyBuilder<IN, OUT, Opt> {
508
+ constructor(props?: AnyObject) {
509
+ super({
510
+ type: 'object',
511
+ properties: {},
512
+ required: [],
513
+ additionalProperties: false,
514
+ hasIsOfTypeCheck: true,
515
+ })
516
+
517
+ if (props) this.addProperties(props)
518
+ }
519
+
520
+ addProperties(props: AnyObject): this {
521
+ const properties: Record<string, JsonSchema> = {}
522
+ const required: string[] = []
523
+
524
+ for (const [key, builder] of Object.entries(props)) {
525
+ const schema = builder.build()
526
+ if (!schema.optionalField) {
527
+ required.push(key)
528
+ } else {
529
+ schema.optionalField = undefined
530
+ }
531
+ properties[key] = schema
532
+ }
533
+
534
+ this.schema.properties = properties
535
+ this.schema.required = _uniq(required).sort()
536
+
537
+ return this
538
+ }
539
+
540
+ /**
541
+ * When set, the validation will not strip away properties that are not specified explicitly in the schema.
542
+ */
543
+ allowAdditionalProperties(): this {
544
+ Object.assign(this.schema, { additionalProperties: true })
545
+ return this
546
+ }
547
+
548
+ extend<IN2 extends AnyObject>(
549
+ props: AnyObject,
550
+ ): JsonSchemaObjectBuilder<IN & IN2, OUT & IN2, Opt> {
551
+ const newBuilder = new JsonSchemaObjectBuilder<IN & IN2, OUT & IN2, Opt>()
552
+ Object.assign(newBuilder.schema, _deepCopy(this.schema))
553
+
554
+ const incomingSchemaBuilder = new JsonSchemaObjectBuilder<IN2, IN2, false>(props)
555
+ mergeJsonSchemaObjects(newBuilder.schema as any, incomingSchemaBuilder.schema as any)
556
+
557
+ return newBuilder
558
+ }
559
+
560
+ /**
561
+ * Extends the current schema with `id`, `created` and `updated` according to NC DB conventions.
562
+ */
563
+ // eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/explicit-module-boundary-types
564
+ dbEntity() {
565
+ return this.extend({
566
+ id: j.string(),
567
+ created: j.number().unixTimestamp2000(),
568
+ updated: j.number().unixTimestamp2000(),
569
+ })
570
+ }
571
+ }
572
+
573
+ export class JsonSchemaObjectInferringBuilder<
500
574
  PROPS extends Record<string, JsonSchemaAnyBuilder<any, any, any>>,
501
575
  Opt extends boolean = false,
502
576
  > extends JsonSchemaAnyBuilder<
@@ -573,7 +647,7 @@ export class JsonSchemaObjectBuilder<
573
647
 
574
648
  extend<NEW_PROPS extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(
575
649
  props: NEW_PROPS,
576
- ): JsonSchemaObjectBuilder<
650
+ ): JsonSchemaObjectInferringBuilder<
577
651
  {
578
652
  [K in keyof PROPS | keyof NEW_PROPS]: K extends keyof NEW_PROPS
579
653
  ? NEW_PROPS[K]
@@ -583,13 +657,13 @@ export class JsonSchemaObjectBuilder<
583
657
  },
584
658
  Opt
585
659
  > {
586
- const newBuilder = new JsonSchemaObjectBuilder<PROPS, Opt>()
660
+ const newBuilder = new JsonSchemaObjectInferringBuilder<PROPS, Opt>()
587
661
  Object.assign(newBuilder.schema, _deepCopy(this.schema))
588
662
 
589
- const incomingSchemaBuilder = new JsonSchemaObjectBuilder<NEW_PROPS, false>(props)
663
+ const incomingSchemaBuilder = new JsonSchemaObjectInferringBuilder<NEW_PROPS, false>(props)
590
664
  mergeJsonSchemaObjects(newBuilder.schema as any, incomingSchemaBuilder.schema as any)
591
665
 
592
- return newBuilder as JsonSchemaObjectBuilder<
666
+ return newBuilder as JsonSchemaObjectInferringBuilder<
593
667
  {
594
668
  [K in keyof PROPS | keyof NEW_PROPS]: K extends keyof NEW_PROPS
595
669
  ? NEW_PROPS[K]
@@ -750,6 +824,33 @@ export interface JsonSchema<IN = unknown, OUT = IN> {
750
824
  hasIsOfTypeCheck?: boolean
751
825
  }
752
826
 
827
+ function object(props: AnyObject): never
828
+ function object<IN extends AnyObject>(props: {
829
+ [key in keyof IN]: JsonSchemaAnyBuilder<any, IN[key], any>
830
+ }): JsonSchemaObjectBuilder<IN, IN, false>
831
+
832
+ function object<IN extends AnyObject>(props: {
833
+ [key in keyof IN]: JsonSchemaAnyBuilder<any, IN[key], any>
834
+ }): JsonSchemaObjectBuilder<IN, IN, false> {
835
+ return new JsonSchemaObjectBuilder<IN, IN, false>(props)
836
+ }
837
+
838
+ function objectDbEntity(props?: AnyObject): never
839
+ function objectDbEntity<IN extends AnyObject>(
840
+ props?: AnyObject,
841
+ ): JsonSchemaObjectBuilder<IN, IN, false>
842
+
843
+ function objectDbEntity<IN extends AnyObject>(
844
+ props?: AnyObject,
845
+ ): JsonSchemaObjectBuilder<IN, IN, false> {
846
+ return j.object({
847
+ id: j.string(),
848
+ created: j.number().unixTimestamp2000(),
849
+ updated: j.number().unixTimestamp2000(),
850
+ ...props,
851
+ })
852
+ }
853
+
753
854
  type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never
754
855
 
755
856
  type ExactMatch<A, B> =