@naturalcycles/nodejs-lib 15.16.0 → 15.17.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.
@@ -7,6 +7,13 @@ import { Ajv } from 'ajv';
7
7
  * to benefit from cached Ajv instance.
8
8
  */
9
9
  export declare const getAjv: any;
10
+ /**
11
+ * Returns cached instance of Ajv, which is non-mutating.
12
+ *
13
+ * To be used in places where we only need to know if an item is valid or not,
14
+ * and are not interested in transforming the data.
15
+ */
16
+ export declare const getNonMutatingAjv: any;
10
17
  /**
11
18
  * Create Ajv with modified defaults.
12
19
  *
@@ -11,6 +11,11 @@ const AJV_OPTIONS = {
11
11
  // https://ajv.js.org/options.html#coercetypes
12
12
  coerceTypes: false, // while `false` - it won't mutate your input
13
13
  };
14
+ const AJV_NON_MUTATING_OPTIONS = {
15
+ ...AJV_OPTIONS,
16
+ removeAdditional: false,
17
+ useDefaults: false,
18
+ };
14
19
  /**
15
20
  * Return cached instance of Ajv with default (recommended) options.
16
21
  *
@@ -18,6 +23,13 @@ const AJV_OPTIONS = {
18
23
  * to benefit from cached Ajv instance.
19
24
  */
20
25
  export const getAjv = _lazyValue(createAjv);
26
+ /**
27
+ * Returns cached instance of Ajv, which is non-mutating.
28
+ *
29
+ * To be used in places where we only need to know if an item is valid or not,
30
+ * and are not interested in transforming the data.
31
+ */
32
+ export const getNonMutatingAjv = _lazyValue(() => createAjv(AJV_NON_MUTATING_OPTIONS));
21
33
  /**
22
34
  * Create Ajv with modified defaults.
23
35
  *
@@ -32,6 +44,7 @@ export function createAjv(opt) {
32
44
  });
33
45
  // Add custom formats
34
46
  addCustomAjvFormats(ajv);
47
+ // todo: review and possibly cherry-pick/vendor the formats
35
48
  // Adds ajv "formats"
36
49
  // https://ajv.js.org/guide/formats.html
37
50
  // @ts-expect-error types are wrong
@@ -50,7 +63,9 @@ export function createAjv(opt) {
50
63
  return ajv;
51
64
  }
52
65
  const TS_2500 = 16725225600; // 2500-01-01
66
+ const TS_2500_MILLIS = TS_2500 * 1000;
53
67
  const TS_2000 = 946684800; // 2000-01-01
68
+ const TS_2000_MILLIS = TS_2000 * 1000;
54
69
  function addCustomAjvFormats(ajv) {
55
70
  return (ajv
56
71
  .addFormat('id', /^[a-z0-9_]{6,64}$/)
@@ -75,13 +90,13 @@ function addCustomAjvFormats(ajv) {
75
90
  .addFormat('unixTimestampMillis', {
76
91
  type: 'number',
77
92
  validate: (n) => {
78
- return n >= 0 && n < TS_2500 * 1000;
93
+ return n >= 0 && n < TS_2500_MILLIS;
79
94
  },
80
95
  })
81
96
  .addFormat('unixTimestampMillis2000', {
82
97
  type: 'number',
83
98
  validate: (n) => {
84
- return n >= TS_2000 * 1000 && n < TS_2500 * 1000;
99
+ return n >= TS_2000_MILLIS && n < TS_2500_MILLIS;
85
100
  },
86
101
  })
87
102
  .addFormat('utcOffset', {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.16.0",
4
+ "version": "15.17.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -14,6 +14,12 @@ const AJV_OPTIONS: Options = {
14
14
  coerceTypes: false, // while `false` - it won't mutate your input
15
15
  }
16
16
 
17
+ const AJV_NON_MUTATING_OPTIONS: Options = {
18
+ ...AJV_OPTIONS,
19
+ removeAdditional: false,
20
+ useDefaults: false,
21
+ }
22
+
17
23
  /**
18
24
  * Return cached instance of Ajv with default (recommended) options.
19
25
  *
@@ -22,6 +28,14 @@ const AJV_OPTIONS: Options = {
22
28
  */
23
29
  export const getAjv = _lazyValue(createAjv)
24
30
 
31
+ /**
32
+ * Returns cached instance of Ajv, which is non-mutating.
33
+ *
34
+ * To be used in places where we only need to know if an item is valid or not,
35
+ * and are not interested in transforming the data.
36
+ */
37
+ export const getNonMutatingAjv = _lazyValue(() => createAjv(AJV_NON_MUTATING_OPTIONS))
38
+
25
39
  /**
26
40
  * Create Ajv with modified defaults.
27
41
  *
@@ -38,6 +52,7 @@ export function createAjv(opt?: Options): Ajv {
38
52
  // Add custom formats
39
53
  addCustomAjvFormats(ajv)
40
54
 
55
+ // todo: review and possibly cherry-pick/vendor the formats
41
56
  // Adds ajv "formats"
42
57
  // https://ajv.js.org/guide/formats.html
43
58
  // @ts-expect-error types are wrong
@@ -60,7 +75,9 @@ export function createAjv(opt?: Options): Ajv {
60
75
  }
61
76
 
62
77
  const TS_2500 = 16725225600 // 2500-01-01
78
+ const TS_2500_MILLIS = TS_2500 * 1000
63
79
  const TS_2000 = 946684800 // 2000-01-01
80
+ const TS_2000_MILLIS = TS_2000 * 1000
64
81
 
65
82
  function addCustomAjvFormats(ajv: Ajv): Ajv {
66
83
  return (
@@ -87,13 +104,13 @@ function addCustomAjvFormats(ajv: Ajv): Ajv {
87
104
  .addFormat('unixTimestampMillis', {
88
105
  type: 'number',
89
106
  validate: (n: number) => {
90
- return n >= 0 && n < TS_2500 * 1000
107
+ return n >= 0 && n < TS_2500_MILLIS
91
108
  },
92
109
  })
93
110
  .addFormat('unixTimestampMillis2000', {
94
111
  type: 'number',
95
112
  validate: (n: number) => {
96
- return n >= TS_2000 * 1000 && n < TS_2500 * 1000
113
+ return n >= TS_2000_MILLIS && n < TS_2500_MILLIS
97
114
  },
98
115
  })
99
116
  .addFormat('utcOffset', {