@naturalcycles/nodejs-lib 14.6.0 → 14.7.1
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,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { JsonSchema, JsonSchemaBuilder } from '@naturalcycles/js-lib';
|
|
2
2
|
import type { Ajv } from 'ajv';
|
|
3
3
|
import { AjvValidationError } from './ajvValidationError.js';
|
|
4
4
|
export interface AjvValidationOptions {
|
|
5
5
|
objectName?: string;
|
|
6
6
|
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
7
|
}
|
|
18
8
|
export interface AjvSchemaCfg {
|
|
19
9
|
/**
|
|
@@ -27,20 +17,6 @@ export interface AjvSchemaCfg {
|
|
|
27
17
|
*/
|
|
28
18
|
schemas?: (JsonSchema | JsonSchemaBuilder | AjvSchema)[];
|
|
29
19
|
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
20
|
/**
|
|
45
21
|
* Option of Ajv.
|
|
46
22
|
* If set to true - will mutate your input objects!
|
|
@@ -85,6 +61,6 @@ export declare class AjvSchema<T = unknown> {
|
|
|
85
61
|
* Returned object is always the same object (`===`) that was passed, so it is returned just for convenience.
|
|
86
62
|
*/
|
|
87
63
|
validate(obj: T, opt?: AjvValidationOptions): T;
|
|
88
|
-
getValidationError(obj: T, opt?: AjvValidationOptions): AjvValidationError | undefined;
|
|
89
64
|
isValid(obj: T): boolean;
|
|
65
|
+
getValidationError(obj: T, opt?: AjvValidationOptions): AjvValidationError | undefined;
|
|
90
66
|
}
|
|
@@ -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,
|
|
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,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { JsonSchema, JsonSchemaBuilder } from '@naturalcycles/js-lib'
|
|
2
2
|
import {
|
|
3
3
|
_filterNullishValues,
|
|
4
4
|
_isObject,
|
|
@@ -14,18 +14,6 @@ import { getAjv } from './getAjv.js'
|
|
|
14
14
|
export interface AjvValidationOptions {
|
|
15
15
|
objectName?: string
|
|
16
16
|
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
17
|
}
|
|
30
18
|
|
|
31
19
|
export interface AjvSchemaCfg {
|
|
@@ -43,23 +31,6 @@ export interface AjvSchemaCfg {
|
|
|
43
31
|
|
|
44
32
|
objectName?: string
|
|
45
33
|
|
|
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
34
|
/**
|
|
64
35
|
* Option of Ajv.
|
|
65
36
|
* If set to true - will mutate your input objects!
|
|
@@ -82,9 +53,6 @@ export class AjvSchema<T = unknown> {
|
|
|
82
53
|
cfg: Partial<AjvSchemaCfg> = {},
|
|
83
54
|
) {
|
|
84
55
|
this.cfg = {
|
|
85
|
-
logErrors: true,
|
|
86
|
-
logger: console,
|
|
87
|
-
separator: '\n',
|
|
88
56
|
...cfg,
|
|
89
57
|
ajv:
|
|
90
58
|
cfg.ajv ||
|
|
@@ -154,6 +122,10 @@ export class AjvSchema<T = unknown> {
|
|
|
154
122
|
return obj
|
|
155
123
|
}
|
|
156
124
|
|
|
125
|
+
isValid(obj: T): boolean {
|
|
126
|
+
return this.validateFunction(obj)
|
|
127
|
+
}
|
|
128
|
+
|
|
157
129
|
getValidationError(obj: T, opt: AjvValidationOptions = {}): AjvValidationError | undefined {
|
|
158
130
|
if (this.isValid(obj)) return
|
|
159
131
|
|
|
@@ -162,8 +134,6 @@ export class AjvSchema<T = unknown> {
|
|
|
162
134
|
const {
|
|
163
135
|
objectId = _isObject(obj) ? (obj['id' as keyof T] as any) : undefined,
|
|
164
136
|
objectName = this.cfg.objectName,
|
|
165
|
-
logErrors = this.cfg.logErrors,
|
|
166
|
-
separator = this.cfg.separator,
|
|
167
137
|
} = opt
|
|
168
138
|
const name = [objectName || 'Object', objectId].filter(Boolean).join('.')
|
|
169
139
|
|
|
@@ -175,22 +145,15 @@ export class AjvSchema<T = unknown> {
|
|
|
175
145
|
const strValue = _inspect(obj, { maxLen: 1000 })
|
|
176
146
|
message = [message, 'Input: ' + strValue].join(separator)
|
|
177
147
|
|
|
178
|
-
if (logErrors) {
|
|
179
|
-
this.cfg.logger.error(errors)
|
|
180
|
-
}
|
|
181
|
-
|
|
182
148
|
return new AjvValidationError(
|
|
183
149
|
message,
|
|
184
150
|
_filterNullishValues({
|
|
185
151
|
errors,
|
|
186
|
-
userFriendly: true,
|
|
187
152
|
objectName,
|
|
188
153
|
objectId,
|
|
189
154
|
}),
|
|
190
155
|
)
|
|
191
156
|
}
|
|
192
|
-
|
|
193
|
-
isValid(obj: T): boolean {
|
|
194
|
-
return this.validateFunction(obj)
|
|
195
|
-
}
|
|
196
157
|
}
|
|
158
|
+
|
|
159
|
+
const separator = '\n'
|