@naturalcycles/nodejs-lib 15.52.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
  */
@@ -114,6 +115,7 @@ export declare class JsonSchemaStringBuilder<IN extends string = string, OUT = I
114
115
  */
115
116
  ianaTimezone(): JsonSchemaEnumBuilder<string | IANATimezone, IANATimezone, false>;
116
117
  base64Url(): this;
118
+ uuid(): this;
117
119
  }
118
120
  export interface JsonSchemaStringEmailOptions {
119
121
  checkTLD: boolean;
@@ -178,6 +180,7 @@ export declare class JsonSchemaObjectBuilder<IN extends AnyObject, OUT extends A
178
180
  }
179
181
  interface JsonSchemaObjectBuilderOpts {
180
182
  hasIsOfTypeCheck?: false;
183
+ patternProperties?: StringMap<JsonSchema<any, any>>;
181
184
  }
182
185
  export declare class JsonSchemaObjectInferringBuilder<PROPS extends Record<string, JsonSchemaAnyBuilder<any, any, any>>, Opt extends boolean = false> extends JsonSchemaAnyBuilder<Expand<{
183
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;
@@ -242,6 +245,7 @@ export interface JsonSchema<IN = unknown, OUT = IN> {
242
245
  properties?: {
243
246
  [K in keyof IN & keyof OUT]: JsonSchema<IN[K], OUT[K]>;
244
247
  };
248
+ patternProperties?: StringMap<JsonSchema<any, any>>;
245
249
  required?: string[];
246
250
  additionalProperties?: boolean;
247
251
  minProperties?: number;
@@ -5,7 +5,7 @@ import { _uniq } from '@naturalcycles/js-lib/array';
5
5
  import { _assert } from '@naturalcycles/js-lib/error';
6
6
  import { _deepCopy, _sortObject } from '@naturalcycles/js-lib/object';
7
7
  import { _objectAssign, JWT_REGEX, } from '@naturalcycles/js-lib/types';
8
- import { BASE64URL_REGEX, COUNTRY_CODE_REGEX, CURRENCY_REGEX, IPV4_REGEX, IPV6_REGEX, LANGUAGE_TAG_REGEX, SEMVER_REGEX, SLUG_REGEX, } from '../regexes.js';
8
+ import { BASE64URL_REGEX, COUNTRY_CODE_REGEX, CURRENCY_REGEX, IPV4_REGEX, IPV6_REGEX, LANGUAGE_TAG_REGEX, SEMVER_REGEX, SLUG_REGEX, UUID_REGEX, } from '../regexes.js';
9
9
  import { TIMEZONES } from '../timezones.js';
10
10
  import { isEveryItemNumber, isEveryItemPrimitive, isEveryItemString, JSON_SCHEMA_ORDER, mergeJsonSchemaObjects, } from './jsonSchemaBuilder.util.js';
11
11
  export const j = {
@@ -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 });
@@ -325,6 +336,9 @@ export class JsonSchemaStringBuilder extends JsonSchemaAnyBuilder {
325
336
  msg: 'contains characters not allowed in Base64 URL characterset',
326
337
  });
327
338
  }
339
+ uuid() {
340
+ return this.regex(UUID_REGEX, { msg: 'is an invalid UUID' });
341
+ }
328
342
  }
329
343
  export class JsonSchemaIsoDateBuilder extends JsonSchemaAnyBuilder {
330
344
  constructor() {
@@ -472,6 +486,7 @@ export class JsonSchemaObjectBuilder extends JsonSchemaAnyBuilder {
472
486
  required: [],
473
487
  additionalProperties: false,
474
488
  hasIsOfTypeCheck: opt?.hasIsOfTypeCheck ?? true,
489
+ patternProperties: opt?.patternProperties ?? undefined,
475
490
  });
476
491
  if (props)
477
492
  this.addProperties(props);
@@ -1,6 +1,7 @@
1
1
  export declare const BASE62_REGEX: RegExp;
2
2
  export declare const BASE64_REGEX: RegExp;
3
3
  export declare const BASE64URL_REGEX: RegExp;
4
+ export declare const UUID_REGEX: RegExp;
4
5
  export declare const COUNTRY_CODE_REGEX: RegExp;
5
6
  export declare const CURRENCY_REGEX: RegExp;
6
7
  /**
@@ -1,6 +1,8 @@
1
1
  export const BASE62_REGEX = /^[a-zA-Z0-9]+$/;
2
2
  export const BASE64_REGEX = /^[a-zA-Z0-9+/]+={0,2}$/;
3
3
  export const BASE64URL_REGEX = /^[a-zA-Z0-9_-]+$/;
4
+ // from `ajv-formats`
5
+ export const UUID_REGEX = /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i;
4
6
  export const COUNTRY_CODE_REGEX = /^[A-Z]{2}$/;
5
7
  export const CURRENCY_REGEX = /^[A-Z]{3}$/;
6
8
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.52.0",
4
+ "version": "15.54.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -35,6 +35,7 @@ import {
35
35
  LANGUAGE_TAG_REGEX,
36
36
  SEMVER_REGEX,
37
37
  SLUG_REGEX,
38
+ UUID_REGEX,
38
39
  } from '../regexes.js'
39
40
  import { TIMEZONES } from '../timezones.js'
40
41
  import {
@@ -65,6 +66,27 @@ export const j = {
65
66
  return j.object<AnyObject>({}).allowAdditionalProperties()
66
67
  },
67
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
+
68
90
  /**
69
91
  * Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
70
92
  */
@@ -96,7 +118,7 @@ export const j = {
96
118
  }
97
119
  }
98
120
 
99
- _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')
100
122
 
101
123
  const typedValues = enumValues as readonly K[]
102
124
  const props = Object.fromEntries(typedValues.map(key => [key, schema])) as any
@@ -457,6 +479,10 @@ export class JsonSchemaStringBuilder<
457
479
  msg: 'contains characters not allowed in Base64 URL characterset',
458
480
  })
459
481
  }
482
+
483
+ uuid(): this {
484
+ return this.regex(UUID_REGEX, { msg: 'is an invalid UUID' })
485
+ }
460
486
  }
461
487
 
462
488
  export interface JsonSchemaStringEmailOptions {
@@ -667,6 +693,7 @@ export class JsonSchemaObjectBuilder<
667
693
  required: [],
668
694
  additionalProperties: false,
669
695
  hasIsOfTypeCheck: opt?.hasIsOfTypeCheck ?? true,
696
+ patternProperties: opt?.patternProperties ?? undefined,
670
697
  })
671
698
 
672
699
  if (props) this.addProperties(props)
@@ -737,6 +764,7 @@ export class JsonSchemaObjectBuilder<
737
764
 
738
765
  interface JsonSchemaObjectBuilderOpts {
739
766
  hasIsOfTypeCheck?: false
767
+ patternProperties?: StringMap<JsonSchema<any, any>>
740
768
  }
741
769
 
742
770
  export class JsonSchemaObjectInferringBuilder<
@@ -967,6 +995,7 @@ export interface JsonSchema<IN = unknown, OUT = IN> {
967
995
  properties?: {
968
996
  [K in keyof IN & keyof OUT]: JsonSchema<IN[K], OUT[K]>
969
997
  }
998
+ patternProperties?: StringMap<JsonSchema<any, any>>
970
999
  required?: string[]
971
1000
  additionalProperties?: boolean
972
1001
  minProperties?: number
@@ -1,6 +1,8 @@
1
1
  export const BASE62_REGEX = /^[a-zA-Z0-9]+$/
2
2
  export const BASE64_REGEX = /^[a-zA-Z0-9+/]+={0,2}$/
3
3
  export const BASE64URL_REGEX = /^[a-zA-Z0-9_-]+$/
4
+ // from `ajv-formats`
5
+ export const UUID_REGEX = /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i
4
6
  export const COUNTRY_CODE_REGEX = /^[A-Z]{2}$/
5
7
  export const CURRENCY_REGEX = /^[A-Z]{3}$/
6
8
  /**