@naturalcycles/nodejs-lib 15.97.4 → 15.97.5

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.
@@ -430,7 +430,7 @@ declare function objectDbEntity<OUT extends BaseDBEntity, EXTRA_KEYS extends Exc
430
430
  } : {
431
431
  updated: BuilderFor<OUT['updated']>;
432
432
  })): JObject<OUT, false>;
433
- declare function record<KS extends JSchema<any, any>, VS extends JSchema<any, any>, Opt extends boolean = SchemaOpt<VS>>(keySchema: KS, valueSchema: VS): JObject<Opt extends true ? Partial<Record<SchemaOut<KS>, SchemaOut<VS>>> : Record<SchemaOut<KS>, SchemaOut<VS>>, false>;
433
+ declare function record<KS extends JSchema<any, any>, VS extends JSchema<any, any>, Opt extends boolean = SchemaOpt<VS>>(keySchema: KS, valueSchema: VS): SchemaOut<KS> extends string ? JObject<Opt extends true ? Partial<Record<SchemaOut<KS>, SchemaOut<VS>>> : Record<SchemaOut<KS>, SchemaOut<VS>>, false> : never;
434
434
  declare function withRegexKeys<S extends JSchema<any, any>>(keyRegex: RegExp | string, schema: S): JObject<StringMap<SchemaOut<S>>, false>;
435
435
  /**
436
436
  * Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
@@ -647,7 +647,7 @@ export interface JsonBuilderRuleOpt {
647
647
  */
648
648
  name?: string;
649
649
  }
650
- type EnumKeyUnion<T> = T extends readonly (infer U)[] ? U : T extends StringEnum | NumberEnum ? T[keyof T] : never;
650
+ type EnumKeyUnion<T> = T extends readonly (infer U)[] ? `${U & (string | number)}` : T extends StringEnum | NumberEnum ? `${T[keyof T] & (string | number)}` : never;
651
651
  type SchemaOut<S> = S extends JSchema<infer OUT, any> ? OUT : never;
652
652
  type SchemaOpt<S> = S extends JSchema<any, infer Opt> ? (Opt extends true ? true : false) : false;
653
653
  type TupleOut<T extends readonly JSchema<any, any>[]> = {
@@ -964,6 +964,7 @@ function objectDbEntity(props) {
964
964
  }
965
965
  function record(keySchema, valueSchema) {
966
966
  const keyJsonSchema = keySchema.build();
967
+ _assert(keyJsonSchema.type !== 'number' && keyJsonSchema.type !== 'integer', 'record() key schema must validate strings, not numbers. JSON object keys are always strings.');
967
968
  // Check if value schema is optional before build() strips the optionalField flag
968
969
  const isValueOptional = valueSchema.getSchema().optionalField;
969
970
  const valueJsonSchema = valueSchema.build();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.97.4",
4
+ "version": "15.97.5",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@standard-schema/spec": "^1",
@@ -19,7 +19,7 @@
19
19
  "yargs": "^18"
20
20
  },
21
21
  "devDependencies": {
22
- "@typescript/native-preview": "7.0.0-dev.20260201.1",
22
+ "@typescript/native-preview": "7.0.0-dev.20260301.1",
23
23
  "@naturalcycles/dev-lib": "18.4.2"
24
24
  },
25
25
  "exports": {
@@ -1380,13 +1380,20 @@ function record<
1380
1380
  >(
1381
1381
  keySchema: KS,
1382
1382
  valueSchema: VS,
1383
- ): JObject<
1384
- Opt extends true
1385
- ? Partial<Record<SchemaOut<KS>, SchemaOut<VS>>>
1386
- : Record<SchemaOut<KS>, SchemaOut<VS>>,
1387
- false
1388
- > {
1383
+ ): SchemaOut<KS> extends string
1384
+ ? JObject<
1385
+ Opt extends true
1386
+ ? Partial<Record<SchemaOut<KS>, SchemaOut<VS>>>
1387
+ : Record<SchemaOut<KS>, SchemaOut<VS>>,
1388
+ false
1389
+ >
1390
+ : never {
1389
1391
  const keyJsonSchema = keySchema.build()
1392
+
1393
+ _assert(
1394
+ keyJsonSchema.type !== 'number' && keyJsonSchema.type !== 'integer',
1395
+ 'record() key schema must validate strings, not numbers. JSON object keys are always strings.',
1396
+ )
1390
1397
  // Check if value schema is optional before build() strips the optionalField flag
1391
1398
  const isValueOptional = (valueSchema as JSchema<any, any>).getSchema().optionalField
1392
1399
  const valueJsonSchema = valueSchema.build()
@@ -1396,18 +1403,13 @@ function record<
1396
1403
  ? { anyOf: [{ isUndefined: true }, valueJsonSchema] }
1397
1404
  : valueJsonSchema
1398
1405
 
1399
- return new JObject<
1400
- Opt extends true
1401
- ? Partial<Record<SchemaOut<KS>, SchemaOut<VS>>>
1402
- : Record<SchemaOut<KS>, SchemaOut<VS>>,
1403
- false
1404
- >([], {
1406
+ return new JObject([], {
1405
1407
  hasIsOfTypeCheck: false,
1406
1408
  keySchema: keyJsonSchema,
1407
1409
  patternProperties: {
1408
1410
  ['^.*$']: finalValueSchema,
1409
1411
  },
1410
- })
1412
+ }) as any
1411
1413
  }
1412
1414
 
1413
1415
  function withRegexKeys<S extends JSchema<any, any>>(
@@ -2151,12 +2153,12 @@ export interface JsonBuilderRuleOpt {
2151
2153
  }
2152
2154
 
2153
2155
  type EnumKeyUnion<T> =
2154
- // array of literals -> union of its elements
2156
+ // array of literals -> union of its elements (stringified)
2155
2157
  T extends readonly (infer U)[]
2156
- ? U
2157
- : // enum object -> union of its values
2158
+ ? `${U & (string | number)}`
2159
+ : // enum object -> union of its values (stringified)
2158
2160
  T extends StringEnum | NumberEnum
2159
- ? T[keyof T]
2161
+ ? `${T[keyof T] & (string | number)}`
2160
2162
  : never
2161
2163
 
2162
2164
  type SchemaOut<S> = S extends JSchema<infer OUT, any> ? OUT : never