@naturalcycles/nodejs-lib 15.46.1 → 15.46.2

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.
@@ -1,5 +1,5 @@
1
1
  import type { Set2 } from '@naturalcycles/js-lib/object';
2
- import { type AnyObject, type IANATimezone, type Inclusiveness, type IsoDate, type IsoDateTime, type NumberEnum, type StringEnum, type StringMap, type UnixTimestamp, type UnixTimestampMillis } from '@naturalcycles/js-lib/types';
2
+ import { type AnyObject, type BaseDBEntity, type IANATimezone, type Inclusiveness, type IsoDate, type IsoDateTime, type NumberEnum, type StringEnum, type StringMap, type UnixTimestamp, type UnixTimestampMillis } from '@naturalcycles/js-lib/types';
3
3
  export declare const j: {
4
4
  string(): JsonSchemaStringBuilder<string, string, false>;
5
5
  number(): JsonSchemaNumberBuilder<number, number, false>;
@@ -253,9 +253,21 @@ declare function object<IN extends AnyObject>(props: {
253
253
  }): JsonSchemaObjectBuilder<IN, IN, false>;
254
254
  declare function objectInfer<P extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(props: P): JsonSchemaObjectInferringBuilder<P, false>;
255
255
  declare function objectDbEntity(props: AnyObject): never;
256
- declare function objectDbEntity<IN extends AnyObject, EXTRA_KEYS extends Exclude<keyof IN, 'id' | 'created' | 'updated'> = Exclude<keyof IN, 'id' | 'created' | 'updated'>>(props: {
257
- [K in EXTRA_KEYS]: JsonSchemaAnyBuilder<any, IN[K], any>;
258
- }): JsonSchemaObjectBuilder<IN, IN, false>;
256
+ declare function objectDbEntity<IN extends BaseDBEntity & AnyObject, EXTRA_KEYS extends Exclude<keyof IN, keyof BaseDBEntity> = Exclude<keyof IN, keyof BaseDBEntity>>(props: {
257
+ [K in EXTRA_KEYS]-?: BuilderFor<IN[K]>;
258
+ } & (ExactMatch<IN['id'], BaseDBEntity['id']> extends true ? {
259
+ id?: BuilderFor<BaseDBEntity['id']>;
260
+ } : {
261
+ id: BuilderFor<IN['id']>;
262
+ }) & (ExactMatch<IN['created'], BaseDBEntity['created']> extends true ? {
263
+ created?: BuilderFor<BaseDBEntity['created']>;
264
+ } : {
265
+ created: BuilderFor<IN['created']>;
266
+ }) & (ExactMatch<IN['updated'], BaseDBEntity['updated']> extends true ? {
267
+ updated?: BuilderFor<BaseDBEntity['updated']>;
268
+ } : {
269
+ updated: BuilderFor<IN['updated']>;
270
+ })): JsonSchemaObjectBuilder<IN, IN, false>;
259
271
  type Expand<T> = T extends infer O ? {
260
272
  [K in keyof O]: O[K];
261
273
  } : never;
@@ -266,6 +278,7 @@ type BuilderOutUnion<B extends readonly JsonSchemaAnyBuilder<any, any, any>[]> =
266
278
  type BuilderInUnion<B extends readonly JsonSchemaAnyBuilder<any, any, any>[]> = {
267
279
  [K in keyof B]: B[K] extends JsonSchemaAnyBuilder<infer I, any, any> ? I : never;
268
280
  }[number];
281
+ type BuilderFor<T> = JsonSchemaAnyBuilder<any, T, any>;
269
282
  interface JsonBuilderRuleOpt {
270
283
  /**
271
284
  * Text of error message to return when the validation fails for the given rule:
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.46.1",
4
+ "version": "15.46.2",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -13,6 +13,7 @@ import type { Set2 } from '@naturalcycles/js-lib/object'
13
13
  import { _deepCopy, _sortObject } from '@naturalcycles/js-lib/object'
14
14
  import {
15
15
  type AnyObject,
16
+ type BaseDBEntity,
16
17
  type IANATimezone,
17
18
  type Inclusiveness,
18
19
  type IsoDate,
@@ -890,24 +891,26 @@ function objectInfer<P extends Record<string, JsonSchemaAnyBuilder<any, any, any
890
891
 
891
892
  function objectDbEntity(props: AnyObject): never
892
893
  function objectDbEntity<
893
- IN extends AnyObject,
894
- EXTRA_KEYS extends Exclude<keyof IN, 'id' | 'created' | 'updated'> = Exclude<
895
- keyof IN,
896
- 'id' | 'created' | 'updated'
897
- >,
898
- >(props: {
899
- [K in EXTRA_KEYS]: JsonSchemaAnyBuilder<any, IN[K], any>
900
- }): JsonSchemaObjectBuilder<IN, IN, false>
901
-
902
- function objectDbEntity<
903
- IN extends AnyObject,
904
- EXTRA_KEYS extends Exclude<keyof IN, 'id' | 'created' | 'updated'> = Exclude<
905
- keyof IN,
906
- 'id' | 'created' | 'updated'
907
- >,
908
- >(props: {
909
- [K in EXTRA_KEYS]: JsonSchemaAnyBuilder<any, IN[K], any>
910
- }): JsonSchemaObjectBuilder<IN, IN, false> {
894
+ IN extends BaseDBEntity & AnyObject,
895
+ EXTRA_KEYS extends Exclude<keyof IN, keyof BaseDBEntity> = Exclude<keyof IN, keyof BaseDBEntity>,
896
+ >(
897
+ props: {
898
+ // ✅ all non-system fields must be explicitly provided
899
+ [K in EXTRA_KEYS]-?: BuilderFor<IN[K]>
900
+ } &
901
+ // if `id` differs, it’s required
902
+ (ExactMatch<IN['id'], BaseDBEntity['id']> extends true
903
+ ? { id?: BuilderFor<BaseDBEntity['id']> }
904
+ : { id: BuilderFor<IN['id']> }) &
905
+ (ExactMatch<IN['created'], BaseDBEntity['created']> extends true
906
+ ? { created?: BuilderFor<BaseDBEntity['created']> }
907
+ : { created: BuilderFor<IN['created']> }) &
908
+ (ExactMatch<IN['updated'], BaseDBEntity['updated']> extends true
909
+ ? { updated?: BuilderFor<BaseDBEntity['updated']> }
910
+ : { updated: BuilderFor<IN['updated']> }),
911
+ ): JsonSchemaObjectBuilder<IN, IN, false>
912
+
913
+ function objectDbEntity(props: AnyObject): any {
911
914
  return j.object({
912
915
  id: j.string(),
913
916
  created: j.number().unixTimestamp2000(),
@@ -929,6 +932,8 @@ type BuilderInUnion<B extends readonly JsonSchemaAnyBuilder<any, any, any>[]> =
929
932
  [K in keyof B]: B[K] extends JsonSchemaAnyBuilder<infer I, any, any> ? I : never
930
933
  }[number]
931
934
 
935
+ type BuilderFor<T> = JsonSchemaAnyBuilder<any, T, any>
936
+
932
937
  interface JsonBuilderRuleOpt {
933
938
  /**
934
939
  * Text of error message to return when the validation fails for the given rule: