@naturalcycles/nodejs-lib 15.55.0 → 15.56.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.
@@ -9,10 +9,7 @@ export declare const j: {
9
9
  infer: typeof objectInfer;
10
10
  any(): JsonSchemaObjectBuilder<AnyObject, AnyObject, false>;
11
11
  stringMap<S extends JsonSchemaTerminal<any, any, any>>(schema: S): JsonSchemaObjectBuilder<StringMap<SchemaIn<S>>, StringMap<SchemaOut<S>>>;
12
- /**
13
- * Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
14
- */
15
- withEnumKeys<const T extends readonly (string | number)[] | StringEnum | NumberEnum, S extends JsonSchemaTerminal<any, any, any>, K extends string | number = EnumKeyUnion<T>>(keys: T, schema: S): JsonSchemaObjectBuilder<Record<K, SchemaIn<S>>, Record<K, SchemaOut<S>>>;
12
+ withEnumKeys: typeof withEnumKeys;
16
13
  };
17
14
  array<IN, OUT, Opt>(itemSchema: JsonSchemaAnyBuilder<IN, OUT, Opt>): JsonSchemaArrayBuilder<IN, OUT, Opt>;
18
15
  set<IN, OUT, Opt>(itemSchema: JsonSchemaAnyBuilder<IN, OUT, Opt>): JsonSchemaSet2Builder<IN, OUT, Opt>;
@@ -337,6 +334,18 @@ declare function objectDbEntity<IN extends BaseDBEntity & AnyObject, EXTRA_KEYS
337
334
  } : {
338
335
  updated: BuilderFor<IN['updated']>;
339
336
  })): JsonSchemaObjectBuilder<IN, IN, false>;
337
+ /**
338
+ * Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
339
+ */
340
+ declare function withEnumKeys<const T extends readonly (string | number)[] | StringEnum | NumberEnum, S extends JsonSchemaAnyBuilder<any, any, any>, K extends string | number = EnumKeyUnion<T>, Opt extends boolean = SchemaOpt<S>>(keys: T, schema: S): JsonSchemaObjectBuilder<Opt extends true ? {
341
+ [P in K]?: SchemaIn<S>;
342
+ } : {
343
+ [P in K]: SchemaIn<S>;
344
+ }, Opt extends true ? {
345
+ [P in K]?: SchemaOut<S>;
346
+ } : {
347
+ [P in K]: SchemaOut<S>;
348
+ }, false>;
340
349
  type Expand<T> = T extends infer O ? {
341
350
  [K in keyof O]: O[K];
342
351
  } : never;
@@ -365,4 +374,5 @@ interface JsonBuilderRuleOpt {
365
374
  type EnumKeyUnion<T> = T extends readonly (infer U)[] ? U : T extends StringEnum | NumberEnum ? T[keyof T] : never;
366
375
  type SchemaIn<S> = S extends JsonSchemaAnyBuilder<infer IN, any, any> ? IN : never;
367
376
  type SchemaOut<S> = S extends JsonSchemaAnyBuilder<any, infer OUT, any> ? OUT : never;
377
+ type SchemaOpt<S> = S extends JsonSchemaAnyBuilder<any, any, infer Opt> ? Opt : false;
368
378
  export {};
@@ -35,30 +35,7 @@ export const j = {
35
35
  },
36
36
  });
37
37
  },
38
- /**
39
- * Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
40
- */
41
- withEnumKeys(keys, schema) {
42
- let enumValues;
43
- if (Array.isArray(keys)) {
44
- _assert(isEveryItemPrimitive(keys), 'Every item in the key list should be string, number or symbol');
45
- enumValues = keys;
46
- }
47
- else if (typeof keys === 'object') {
48
- const enumType = getEnumType(keys);
49
- _assert(enumType === 'NumberEnum' || enumType === 'StringEnum', 'The key list should be StringEnum or NumberEnum');
50
- if (enumType === 'NumberEnum') {
51
- enumValues = _numberEnumValues(keys);
52
- }
53
- else if (enumType === 'StringEnum') {
54
- enumValues = _stringEnumValues(keys);
55
- }
56
- }
57
- _assert(enumValues, 'The key list should be an array of values, NumberEnum or a StringEnum');
58
- const typedValues = enumValues;
59
- const props = Object.fromEntries(typedValues.map(key => [key, schema]));
60
- return new JsonSchemaObjectBuilder(props, { hasIsOfTypeCheck: false });
61
- },
38
+ withEnumKeys,
62
39
  }),
63
40
  array(itemSchema) {
64
41
  return new JsonSchemaArrayBuilder(itemSchema);
@@ -717,3 +694,27 @@ function objectDbEntity(props) {
717
694
  ...props,
718
695
  });
719
696
  }
697
+ /**
698
+ * Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
699
+ */
700
+ function withEnumKeys(keys, schema) {
701
+ let enumValues;
702
+ if (Array.isArray(keys)) {
703
+ _assert(isEveryItemPrimitive(keys), 'Every item in the key list should be string, number or symbol');
704
+ enumValues = keys;
705
+ }
706
+ else if (typeof keys === 'object') {
707
+ const enumType = getEnumType(keys);
708
+ _assert(enumType === 'NumberEnum' || enumType === 'StringEnum', 'The key list should be StringEnum or NumberEnum');
709
+ if (enumType === 'NumberEnum') {
710
+ enumValues = _numberEnumValues(keys);
711
+ }
712
+ else if (enumType === 'StringEnum') {
713
+ enumValues = _stringEnumValues(keys);
714
+ }
715
+ }
716
+ _assert(enumValues, 'The key list should be an array of values, NumberEnum or a StringEnum');
717
+ const typedValues = enumValues;
718
+ const props = Object.fromEntries(typedValues.map(key => [key, schema]));
719
+ return new JsonSchemaObjectBuilder(props, { hasIsOfTypeCheck: false });
720
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.55.0",
4
+ "version": "15.56.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -87,47 +87,7 @@ export const j = {
87
87
  )
88
88
  },
89
89
 
90
- /**
91
- * Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
92
- */
93
- withEnumKeys<
94
- const T extends readonly (string | number)[] | StringEnum | NumberEnum,
95
- S extends JsonSchemaTerminal<any, any, any>,
96
- K extends string | number = EnumKeyUnion<T>,
97
- >(
98
- keys: T,
99
- schema: S,
100
- ): JsonSchemaObjectBuilder<Record<K, SchemaIn<S>>, Record<K, SchemaOut<S>>> {
101
- let enumValues: readonly (string | number)[] | undefined
102
- if (Array.isArray(keys)) {
103
- _assert(
104
- isEveryItemPrimitive(keys),
105
- 'Every item in the key list should be string, number or symbol',
106
- )
107
- enumValues = keys
108
- } else if (typeof keys === 'object') {
109
- const enumType = getEnumType(keys)
110
- _assert(
111
- enumType === 'NumberEnum' || enumType === 'StringEnum',
112
- 'The key list should be StringEnum or NumberEnum',
113
- )
114
- if (enumType === 'NumberEnum') {
115
- enumValues = _numberEnumValues(keys as NumberEnum)
116
- } else if (enumType === 'StringEnum') {
117
- enumValues = _stringEnumValues(keys as StringEnum)
118
- }
119
- }
120
-
121
- _assert(enumValues, 'The key list should be an array of values, NumberEnum or a StringEnum')
122
-
123
- const typedValues = enumValues as readonly K[]
124
- const props = Object.fromEntries(typedValues.map(key => [key, schema])) as any
125
-
126
- return new JsonSchemaObjectBuilder<Record<K, SchemaIn<S>>, Record<K, SchemaOut<S>>, false>(
127
- props,
128
- { hasIsOfTypeCheck: false },
129
- )
130
- },
90
+ withEnumKeys,
131
91
  }),
132
92
 
133
93
  array<IN, OUT, Opt>(
@@ -1160,6 +1120,54 @@ function objectDbEntity(props: AnyObject): any {
1160
1120
  })
1161
1121
  }
1162
1122
 
1123
+ /**
1124
+ * Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
1125
+ */
1126
+ function withEnumKeys<
1127
+ const T extends readonly (string | number)[] | StringEnum | NumberEnum,
1128
+ S extends JsonSchemaAnyBuilder<any, any, any>,
1129
+ K extends string | number = EnumKeyUnion<T>,
1130
+ Opt extends boolean = SchemaOpt<S>,
1131
+ >(
1132
+ keys: T,
1133
+ schema: S,
1134
+ ): JsonSchemaObjectBuilder<
1135
+ Opt extends true ? { [P in K]?: SchemaIn<S> } : { [P in K]: SchemaIn<S> },
1136
+ Opt extends true ? { [P in K]?: SchemaOut<S> } : { [P in K]: SchemaOut<S> },
1137
+ false
1138
+ > {
1139
+ let enumValues: readonly (string | number)[] | undefined
1140
+ if (Array.isArray(keys)) {
1141
+ _assert(
1142
+ isEveryItemPrimitive(keys),
1143
+ 'Every item in the key list should be string, number or symbol',
1144
+ )
1145
+ enumValues = keys
1146
+ } else if (typeof keys === 'object') {
1147
+ const enumType = getEnumType(keys)
1148
+ _assert(
1149
+ enumType === 'NumberEnum' || enumType === 'StringEnum',
1150
+ 'The key list should be StringEnum or NumberEnum',
1151
+ )
1152
+ if (enumType === 'NumberEnum') {
1153
+ enumValues = _numberEnumValues(keys as NumberEnum)
1154
+ } else if (enumType === 'StringEnum') {
1155
+ enumValues = _stringEnumValues(keys as StringEnum)
1156
+ }
1157
+ }
1158
+
1159
+ _assert(enumValues, 'The key list should be an array of values, NumberEnum or a StringEnum')
1160
+
1161
+ const typedValues = enumValues as readonly K[]
1162
+ const props = Object.fromEntries(typedValues.map(key => [key, schema])) as any
1163
+
1164
+ return new JsonSchemaObjectBuilder<
1165
+ Opt extends true ? { [P in K]?: SchemaIn<S> } : { [P in K]: SchemaIn<S> },
1166
+ Opt extends true ? { [P in K]?: SchemaOut<S> } : { [P in K]: SchemaOut<S> },
1167
+ false
1168
+ >(props, { hasIsOfTypeCheck: false })
1169
+ }
1170
+
1163
1171
  type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never
1164
1172
 
1165
1173
  type ExactMatch<A, B> =
@@ -1201,3 +1209,4 @@ type EnumKeyUnion<T> =
1201
1209
 
1202
1210
  type SchemaIn<S> = S extends JsonSchemaAnyBuilder<infer IN, any, any> ? IN : never
1203
1211
  type SchemaOut<S> = S extends JsonSchemaAnyBuilder<any, infer OUT, any> ? OUT : never
1212
+ type SchemaOpt<S> = S extends JsonSchemaAnyBuilder<any, any, infer Opt> ? Opt : false