@naturalcycles/nodejs-lib 15.53.0 → 15.54.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.
@@ -8,6 +8,7 @@ export declare const j: {
8
8
  dbEntity: typeof objectDbEntity;
9
9
  infer: typeof objectInfer;
10
10
  any(): JsonSchemaObjectBuilder<AnyObject, AnyObject, false>;
11
+ stringMap<S extends JsonSchemaTerminal<any, any, any>>(schema: S): JsonSchemaObjectBuilder<StringMap<SchemaIn<S>>, StringMap<SchemaOut<S>>>;
11
12
  /**
12
13
  * Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
13
14
  */
@@ -179,6 +180,7 @@ export declare class JsonSchemaObjectBuilder<IN extends AnyObject, OUT extends A
179
180
  }
180
181
  interface JsonSchemaObjectBuilderOpts {
181
182
  hasIsOfTypeCheck?: false;
183
+ patternProperties?: StringMap<JsonSchema<any, any>>;
182
184
  }
183
185
  export declare class JsonSchemaObjectInferringBuilder<PROPS extends Record<string, JsonSchemaAnyBuilder<any, any, any>>, Opt extends boolean = false> extends JsonSchemaAnyBuilder<Expand<{
184
186
  [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;
@@ -243,6 +245,7 @@ export interface JsonSchema<IN = unknown, OUT = IN> {
243
245
  properties?: {
244
246
  [K in keyof IN & keyof OUT]: JsonSchema<IN[K], OUT[K]>;
245
247
  };
248
+ patternProperties?: StringMap<JsonSchema<any, any>>;
246
249
  required?: string[];
247
250
  additionalProperties?: boolean;
248
251
  minProperties?: number;
@@ -24,6 +24,17 @@ export const j = {
24
24
  any() {
25
25
  return j.object({}).allowAdditionalProperties();
26
26
  },
27
+ stringMap(schema) {
28
+ const builtSchema = schema.build();
29
+ _assert(!builtSchema?.optionalField, 'In a StringMap schema the value cannot be `undefined`, because `undefined` is not a valid JSON Schema value.');
30
+ builtSchema.optionalField = undefined;
31
+ return new JsonSchemaObjectBuilder({}, {
32
+ hasIsOfTypeCheck: false,
33
+ patternProperties: {
34
+ '^.+$': builtSchema,
35
+ },
36
+ });
37
+ },
27
38
  /**
28
39
  * Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
29
40
  */
@@ -43,7 +54,7 @@ export const j = {
43
54
  enumValues = _stringEnumValues(keys);
44
55
  }
45
56
  }
46
- _assert(enumValues, 'The key list should be an array of values, NumberEnum or a StrinEnum');
57
+ _assert(enumValues, 'The key list should be an array of values, NumberEnum or a StringEnum');
47
58
  const typedValues = enumValues;
48
59
  const props = Object.fromEntries(typedValues.map(key => [key, schema]));
49
60
  return new JsonSchemaObjectBuilder(props, { hasIsOfTypeCheck: false });
@@ -475,6 +486,7 @@ export class JsonSchemaObjectBuilder extends JsonSchemaAnyBuilder {
475
486
  required: [],
476
487
  additionalProperties: false,
477
488
  hasIsOfTypeCheck: opt?.hasIsOfTypeCheck ?? true,
489
+ patternProperties: opt?.patternProperties ?? undefined,
478
490
  });
479
491
  if (props)
480
492
  this.addProperties(props);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.53.0",
4
+ "version": "15.54.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -66,6 +66,27 @@ export const j = {
66
66
  return j.object<AnyObject>({}).allowAdditionalProperties()
67
67
  },
68
68
 
69
+ stringMap<S extends JsonSchemaTerminal<any, any, any>>(
70
+ schema: S,
71
+ ): JsonSchemaObjectBuilder<StringMap<SchemaIn<S>>, StringMap<SchemaOut<S>>> {
72
+ const builtSchema = schema.build()
73
+ _assert(
74
+ !builtSchema?.optionalField,
75
+ 'In a StringMap schema the value cannot be `undefined`, because `undefined` is not a valid JSON Schema value.',
76
+ )
77
+ builtSchema.optionalField = undefined
78
+
79
+ return new JsonSchemaObjectBuilder<StringMap<SchemaIn<S>>, StringMap<SchemaOut<S>>>(
80
+ {},
81
+ {
82
+ hasIsOfTypeCheck: false,
83
+ patternProperties: {
84
+ '^.+$': builtSchema,
85
+ },
86
+ },
87
+ )
88
+ },
89
+
69
90
  /**
70
91
  * Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
71
92
  */
@@ -97,7 +118,7 @@ export const j = {
97
118
  }
98
119
  }
99
120
 
100
- _assert(enumValues, 'The key list should be an array of values, NumberEnum or a StrinEnum')
121
+ _assert(enumValues, 'The key list should be an array of values, NumberEnum or a StringEnum')
101
122
 
102
123
  const typedValues = enumValues as readonly K[]
103
124
  const props = Object.fromEntries(typedValues.map(key => [key, schema])) as any
@@ -672,6 +693,7 @@ export class JsonSchemaObjectBuilder<
672
693
  required: [],
673
694
  additionalProperties: false,
674
695
  hasIsOfTypeCheck: opt?.hasIsOfTypeCheck ?? true,
696
+ patternProperties: opt?.patternProperties ?? undefined,
675
697
  })
676
698
 
677
699
  if (props) this.addProperties(props)
@@ -742,6 +764,7 @@ export class JsonSchemaObjectBuilder<
742
764
 
743
765
  interface JsonSchemaObjectBuilderOpts {
744
766
  hasIsOfTypeCheck?: false
767
+ patternProperties?: StringMap<JsonSchema<any, any>>
745
768
  }
746
769
 
747
770
  export class JsonSchemaObjectInferringBuilder<
@@ -972,6 +995,7 @@ export interface JsonSchema<IN = unknown, OUT = IN> {
972
995
  properties?: {
973
996
  [K in keyof IN & keyof OUT]: JsonSchema<IN[K], OUT[K]>
974
997
  }
998
+ patternProperties?: StringMap<JsonSchema<any, any>>
975
999
  required?: string[]
976
1000
  additionalProperties?: boolean
977
1001
  minProperties?: number