@naturalcycles/nodejs-lib 14.6.0 → 14.7.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.
@@ -1,19 +1,10 @@
1
- import type { CommonLogger, JsonSchema, JsonSchemaBuilder } from '@naturalcycles/js-lib';
1
+ import type { JsonSchema, JsonSchemaBuilder } from '@naturalcycles/js-lib';
2
+ import type { ZodJSONSchema } from '@naturalcycles/js-lib/zod';
2
3
  import type { Ajv } from 'ajv';
3
4
  import { AjvValidationError } from './ajvValidationError.js';
4
5
  export interface AjvValidationOptions {
5
6
  objectName?: string;
6
7
  objectId?: string;
7
- /**
8
- * @default to cfg.logErrors, which defaults to true
9
- */
10
- logErrors?: boolean;
11
- /**
12
- * Used to separate multiple validation errors.
13
- *
14
- * @default cfg.separator || '\n'
15
- */
16
- separator?: string;
17
8
  }
18
9
  export interface AjvSchemaCfg {
19
10
  /**
@@ -27,20 +18,6 @@ export interface AjvSchemaCfg {
27
18
  */
28
19
  schemas?: (JsonSchema | JsonSchemaBuilder | AjvSchema)[];
29
20
  objectName?: string;
30
- /**
31
- * Used to separate multiple validation errors.
32
- *
33
- * @default '\n'
34
- */
35
- separator: string;
36
- /**
37
- * @default true
38
- */
39
- logErrors: boolean;
40
- /**
41
- * Default to `console`
42
- */
43
- logger: CommonLogger;
44
21
  /**
45
22
  * Option of Ajv.
46
23
  * If set to true - will mutate your input objects!
@@ -68,7 +45,7 @@ export declare class AjvSchema<T = unknown> {
68
45
  * Implementation note: JsonSchemaBuilder goes first in the union type, otherwise TypeScript fails to infer <T> type
69
46
  * correctly for some reason.
70
47
  */
71
- static create<T>(schema: JsonSchemaBuilder<T> | JsonSchema<T> | AjvSchema<T>, cfg?: Partial<AjvSchemaCfg>): AjvSchema<T>;
48
+ static create<T>(schema: JsonSchemaBuilder<T> | JsonSchema<T> | AjvSchema<T> | ZodJSONSchema, cfg?: Partial<AjvSchemaCfg>): AjvSchema<T>;
72
49
  /**
73
50
  * Create AjvSchema directly from a filePath of json schema.
74
51
  * Convenient method that just does fs.readFileSync for you.
@@ -85,6 +62,6 @@ export declare class AjvSchema<T = unknown> {
85
62
  * Returned object is always the same object (`===`) that was passed, so it is returned just for convenience.
86
63
  */
87
64
  validate(obj: T, opt?: AjvValidationOptions): T;
88
- getValidationError(obj: T, opt?: AjvValidationOptions): AjvValidationError | undefined;
89
65
  isValid(obj: T): boolean;
66
+ getValidationError(obj: T, opt?: AjvValidationOptions): AjvValidationError | undefined;
90
67
  }
@@ -14,9 +14,6 @@ export class AjvSchema {
14
14
  constructor(schema, cfg = {}) {
15
15
  this.schema = schema;
16
16
  this.cfg = {
17
- logErrors: true,
18
- logger: console,
19
- separator: '\n',
20
17
  ...cfg,
21
18
  ajv: cfg.ajv ||
22
19
  getAjv({
@@ -77,11 +74,14 @@ export class AjvSchema {
77
74
  throw err;
78
75
  return obj;
79
76
  }
77
+ isValid(obj) {
78
+ return this.validateFunction(obj);
79
+ }
80
80
  getValidationError(obj, opt = {}) {
81
81
  if (this.isValid(obj))
82
82
  return;
83
83
  const errors = this.validateFunction.errors;
84
- const { objectId = _isObject(obj) ? obj['id'] : undefined, objectName = this.cfg.objectName, logErrors = this.cfg.logErrors, separator = this.cfg.separator, } = opt;
84
+ const { objectId = _isObject(obj) ? obj['id'] : undefined, objectName = this.cfg.objectName, } = opt;
85
85
  const name = [objectName || 'Object', objectId].filter(Boolean).join('.');
86
86
  let message = this.cfg.ajv.errorsText(errors, {
87
87
  dataVar: name,
@@ -89,17 +89,11 @@ export class AjvSchema {
89
89
  });
90
90
  const strValue = _inspect(obj, { maxLen: 1000 });
91
91
  message = [message, 'Input: ' + strValue].join(separator);
92
- if (logErrors) {
93
- this.cfg.logger.error(errors);
94
- }
95
92
  return new AjvValidationError(message, _filterNullishValues({
96
93
  errors,
97
- userFriendly: true,
98
94
  objectName,
99
95
  objectId,
100
96
  }));
101
97
  }
102
- isValid(obj) {
103
- return this.validateFunction(obj);
104
- }
105
98
  }
99
+ const separator = '\n';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "14.6.0",
4
+ "version": "14.7.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -1,10 +1,11 @@
1
- import type { CommonLogger, JsonSchema, JsonSchemaBuilder } from '@naturalcycles/js-lib'
1
+ import type { JsonSchema, JsonSchemaBuilder } from '@naturalcycles/js-lib'
2
2
  import {
3
3
  _filterNullishValues,
4
4
  _isObject,
5
5
  _substringBefore,
6
6
  JsonSchemaAnyBuilder,
7
7
  } from '@naturalcycles/js-lib'
8
+ import type { ZodJSONSchema } from '@naturalcycles/js-lib/zod'
8
9
  import type { Ajv, ValidateFunction } from 'ajv'
9
10
  import { fs2 } from '../../fs/fs2.js'
10
11
  import { _inspect } from '../../string/inspect.js'
@@ -14,18 +15,6 @@ import { getAjv } from './getAjv.js'
14
15
  export interface AjvValidationOptions {
15
16
  objectName?: string
16
17
  objectId?: string
17
-
18
- /**
19
- * @default to cfg.logErrors, which defaults to true
20
- */
21
- logErrors?: boolean
22
-
23
- /**
24
- * Used to separate multiple validation errors.
25
- *
26
- * @default cfg.separator || '\n'
27
- */
28
- separator?: string
29
18
  }
30
19
 
31
20
  export interface AjvSchemaCfg {
@@ -43,23 +32,6 @@ export interface AjvSchemaCfg {
43
32
 
44
33
  objectName?: string
45
34
 
46
- /**
47
- * Used to separate multiple validation errors.
48
- *
49
- * @default '\n'
50
- */
51
- separator: string
52
-
53
- /**
54
- * @default true
55
- */
56
- logErrors: boolean
57
-
58
- /**
59
- * Default to `console`
60
- */
61
- logger: CommonLogger
62
-
63
35
  /**
64
36
  * Option of Ajv.
65
37
  * If set to true - will mutate your input objects!
@@ -82,9 +54,6 @@ export class AjvSchema<T = unknown> {
82
54
  cfg: Partial<AjvSchemaCfg> = {},
83
55
  ) {
84
56
  this.cfg = {
85
- logErrors: true,
86
- logger: console,
87
- separator: '\n',
88
57
  ...cfg,
89
58
  ajv:
90
59
  cfg.ajv ||
@@ -114,7 +83,7 @@ export class AjvSchema<T = unknown> {
114
83
  * correctly for some reason.
115
84
  */
116
85
  static create<T>(
117
- schema: JsonSchemaBuilder<T> | JsonSchema<T> | AjvSchema<T>,
86
+ schema: JsonSchemaBuilder<T> | JsonSchema<T> | AjvSchema<T> | ZodJSONSchema,
118
87
  cfg: Partial<AjvSchemaCfg> = {},
119
88
  ): AjvSchema<T> {
120
89
  if (schema instanceof AjvSchema) return schema
@@ -154,6 +123,10 @@ export class AjvSchema<T = unknown> {
154
123
  return obj
155
124
  }
156
125
 
126
+ isValid(obj: T): boolean {
127
+ return this.validateFunction(obj)
128
+ }
129
+
157
130
  getValidationError(obj: T, opt: AjvValidationOptions = {}): AjvValidationError | undefined {
158
131
  if (this.isValid(obj)) return
159
132
 
@@ -162,8 +135,6 @@ export class AjvSchema<T = unknown> {
162
135
  const {
163
136
  objectId = _isObject(obj) ? (obj['id' as keyof T] as any) : undefined,
164
137
  objectName = this.cfg.objectName,
165
- logErrors = this.cfg.logErrors,
166
- separator = this.cfg.separator,
167
138
  } = opt
168
139
  const name = [objectName || 'Object', objectId].filter(Boolean).join('.')
169
140
 
@@ -175,22 +146,15 @@ export class AjvSchema<T = unknown> {
175
146
  const strValue = _inspect(obj, { maxLen: 1000 })
176
147
  message = [message, 'Input: ' + strValue].join(separator)
177
148
 
178
- if (logErrors) {
179
- this.cfg.logger.error(errors)
180
- }
181
-
182
149
  return new AjvValidationError(
183
150
  message,
184
151
  _filterNullishValues({
185
152
  errors,
186
- userFriendly: true,
187
153
  objectName,
188
154
  objectId,
189
155
  }),
190
156
  )
191
157
  }
192
-
193
- isValid(obj: T): boolean {
194
- return this.validateFunction(obj)
195
- }
196
158
  }
159
+
160
+ const separator = '\n'