@bonsae/nrg 0.13.0 → 0.13.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.
- package/package.json +1 -1
- package/test/index.js +1 -168
- package/types/test.d.ts +29 -40
package/package.json
CHANGED
package/test/index.js
CHANGED
|
@@ -145,175 +145,8 @@ function createMockNodeRedNode(options = {}) {
|
|
|
145
145
|
};
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
-
// src/core/validator.ts
|
|
149
|
-
import Ajv from "ajv";
|
|
150
|
-
import addFormats from "ajv-formats";
|
|
151
|
-
import addErrors from "ajv-errors";
|
|
152
|
-
var Validator = class {
|
|
153
|
-
ajv;
|
|
154
|
-
constructor(options) {
|
|
155
|
-
const { customKeywords, customFormats, ...ajvOptions } = options || {};
|
|
156
|
-
this.ajv = new Ajv({
|
|
157
|
-
allErrors: true,
|
|
158
|
-
code: {
|
|
159
|
-
source: false
|
|
160
|
-
},
|
|
161
|
-
coerceTypes: true,
|
|
162
|
-
removeAdditional: false,
|
|
163
|
-
strict: false,
|
|
164
|
-
strictSchema: false,
|
|
165
|
-
useDefaults: true,
|
|
166
|
-
validateFormats: true,
|
|
167
|
-
// NOTE: typebox handles validation via typescript
|
|
168
|
-
// NOTE: if true, types that are not serializable JSON, like Function, would not work
|
|
169
|
-
validateSchema: false,
|
|
170
|
-
verbose: true,
|
|
171
|
-
...ajvOptions
|
|
172
|
-
});
|
|
173
|
-
addFormats(this.ajv);
|
|
174
|
-
addErrors(this.ajv);
|
|
175
|
-
this.addCustomKeywords(customKeywords || []);
|
|
176
|
-
this.addCustomFormats(customFormats || {});
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Add custom keywords to the validator
|
|
180
|
-
*/
|
|
181
|
-
addCustomKeywords(keywords) {
|
|
182
|
-
if (!keywords) return;
|
|
183
|
-
keywords.forEach((keyword) => {
|
|
184
|
-
this.ajv.addKeyword(keyword);
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Add custom formats to the validator
|
|
189
|
-
*/
|
|
190
|
-
addCustomFormats(formats) {
|
|
191
|
-
if (!formats) return;
|
|
192
|
-
Object.entries(formats).forEach(([name, validator2]) => {
|
|
193
|
-
if (validator2 instanceof RegExp) {
|
|
194
|
-
this.ajv.addFormat(name, validator2);
|
|
195
|
-
} else {
|
|
196
|
-
this.ajv.addFormat(name, { validate: validator2 });
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
/**
|
|
201
|
-
* Create a validator function with caching
|
|
202
|
-
* @param schema - JSON Schema to validate against
|
|
203
|
-
* @param cacheKey - Optional cache key for reusing validators
|
|
204
|
-
*/
|
|
205
|
-
createValidator(schema, cacheKey) {
|
|
206
|
-
if (cacheKey && !schema.$id) {
|
|
207
|
-
schema.$id = cacheKey;
|
|
208
|
-
}
|
|
209
|
-
if (schema.$id) {
|
|
210
|
-
const cached = this.ajv.getSchema(schema.$id);
|
|
211
|
-
if (cached) return cached;
|
|
212
|
-
}
|
|
213
|
-
const validator2 = this.ajv.compile(schema);
|
|
214
|
-
return validator2;
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Validate data against a schema and return a structured result
|
|
218
|
-
*/
|
|
219
|
-
validate(data, schema, options) {
|
|
220
|
-
const validator2 = this.createValidator(schema, options?.cacheKey);
|
|
221
|
-
const valid = validator2(data);
|
|
222
|
-
if (!valid) {
|
|
223
|
-
const errorMessage = this.formatErrors(validator2.errors);
|
|
224
|
-
if (options?.throwOnError) {
|
|
225
|
-
throw new ValidationError(errorMessage, validator2.errors || []);
|
|
226
|
-
}
|
|
227
|
-
return {
|
|
228
|
-
valid: false,
|
|
229
|
-
errors: validator2.errors || void 0,
|
|
230
|
-
errorMessage
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
return {
|
|
234
|
-
valid: true,
|
|
235
|
-
data
|
|
236
|
-
};
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Format errors into a human-readable string
|
|
240
|
-
*/
|
|
241
|
-
formatErrors(errors, options) {
|
|
242
|
-
if (!errors || errors.length === 0) {
|
|
243
|
-
return "No errors";
|
|
244
|
-
}
|
|
245
|
-
return this.ajv.errorsText(errors, {
|
|
246
|
-
separator: "; ",
|
|
247
|
-
dataVar: "data",
|
|
248
|
-
...options
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* Get detailed error information
|
|
253
|
-
*/
|
|
254
|
-
getDetailedErrors(errors) {
|
|
255
|
-
if (!errors || errors.length === 0) return [];
|
|
256
|
-
return errors.map((error) => ({
|
|
257
|
-
field: error.instancePath || "/",
|
|
258
|
-
message: error.message || "Validation failed",
|
|
259
|
-
keyword: error.keyword,
|
|
260
|
-
params: error.params,
|
|
261
|
-
schemaPath: error.schemaPath
|
|
262
|
-
}));
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* Add a schema to the validator for reference
|
|
266
|
-
*/
|
|
267
|
-
addSchema(schema, key) {
|
|
268
|
-
this.ajv.addSchema(schema, key);
|
|
269
|
-
return this;
|
|
270
|
-
}
|
|
271
|
-
/**
|
|
272
|
-
* Remove a schema from the validator
|
|
273
|
-
*/
|
|
274
|
-
removeSchema(key) {
|
|
275
|
-
this.ajv.removeSchema(key);
|
|
276
|
-
return this;
|
|
277
|
-
}
|
|
278
|
-
};
|
|
279
|
-
var ValidationError = class _ValidationError extends Error {
|
|
280
|
-
constructor(message, errors) {
|
|
281
|
-
super(message);
|
|
282
|
-
this.errors = errors;
|
|
283
|
-
this.name = "ValidationError";
|
|
284
|
-
Object.setPrototypeOf(this, _ValidationError.prototype);
|
|
285
|
-
}
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
// src/core/server/validation.ts
|
|
289
|
-
var validator = void 0;
|
|
290
|
-
function initValidator(RED) {
|
|
291
|
-
validator = new Validator({
|
|
292
|
-
customKeywords: [
|
|
293
|
-
{
|
|
294
|
-
keyword: "x-nrg-skip-validation",
|
|
295
|
-
schemaType: "boolean",
|
|
296
|
-
valid: true
|
|
297
|
-
},
|
|
298
|
-
{
|
|
299
|
-
keyword: "x-nrg-node-type",
|
|
300
|
-
type: "string",
|
|
301
|
-
validate: (schemaValue, dataValue) => {
|
|
302
|
-
if (!dataValue) return true;
|
|
303
|
-
const node = RED.nodes.getNode(dataValue);
|
|
304
|
-
return node?.type === schemaValue;
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
],
|
|
308
|
-
customFormats: {
|
|
309
|
-
"node-id": /^[a-zA-Z0-9-_]+$/,
|
|
310
|
-
"flow-id": /^[a-f0-9]{16}$/,
|
|
311
|
-
"topic-path": (data) => /^[a-zA-Z0-9/_-]+$/.test(data)
|
|
312
|
-
}
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
|
|
316
148
|
// src/test/index.ts
|
|
149
|
+
import { initValidator } from "@bonsae/nrg/server";
|
|
317
150
|
function buildConfig(NodeClass, userConfig = {}) {
|
|
318
151
|
const defaults = {};
|
|
319
152
|
if (NodeClass.configSchema?.properties) {
|
package/types/test.d.ts
CHANGED
|
@@ -1,49 +1,38 @@
|
|
|
1
|
-
// Generated by dts-bundle-generator v9.5.1
|
|
2
1
|
|
|
3
|
-
export interface MockNodeRedNodeOptions {
|
|
4
|
-
id?: string;
|
|
5
|
-
type?: string;
|
|
6
|
-
name?: string;
|
|
7
|
-
z?: string;
|
|
8
|
-
wires?: string[][];
|
|
9
|
-
credentials?: Record<string, any>;
|
|
10
|
-
[key: string]: any;
|
|
11
|
-
}
|
|
12
2
|
export interface CreateNodeOptions {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
3
|
+
config?: Record<string, any>;
|
|
4
|
+
credentials?: Record<string, any>;
|
|
5
|
+
settings?: Record<string, any>;
|
|
6
|
+
overrides?: Record<string, any>;
|
|
17
7
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
} ?
|
|
21
|
-
|
|
22
|
-
send(msg: infer O): any;
|
|
23
|
-
} ? O : any;
|
|
8
|
+
|
|
9
|
+
type ExtractInput<T> = T extends { input(msg: infer I): any } ? I : any;
|
|
10
|
+
type ExtractOutput<T> = T extends { send(msg: infer O): any } ? O : any;
|
|
11
|
+
|
|
24
12
|
export interface TestNodeHelpers<TInput = any, TOutput = any> {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
13
|
+
receive(msg: TInput): Promise<void>;
|
|
14
|
+
close(removed?: boolean): Promise<void>;
|
|
15
|
+
reset(): void;
|
|
16
|
+
sent(): TOutput[];
|
|
17
|
+
sent(port: number): any[];
|
|
18
|
+
statuses(): any[];
|
|
19
|
+
logged(level?: "info" | "warn" | "error" | "debug"): string[];
|
|
20
|
+
warned(): string[];
|
|
21
|
+
errored(): string[];
|
|
34
22
|
}
|
|
23
|
+
|
|
35
24
|
export interface CreateNodeResult<T> {
|
|
36
|
-
|
|
37
|
-
|
|
25
|
+
node: T & TestNodeHelpers<ExtractInput<T>, ExtractOutput<T>>;
|
|
26
|
+
RED: any;
|
|
38
27
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
28
|
+
|
|
29
|
+
interface NodeClass {
|
|
30
|
+
readonly type: string;
|
|
31
|
+
readonly category?: string;
|
|
32
|
+
readonly configSchema?: any;
|
|
33
|
+
registered?(RED: any): void | Promise<void>;
|
|
34
|
+
_registered?(RED: any): void | Promise<void>;
|
|
35
|
+
new (...args: any[]): any;
|
|
46
36
|
}
|
|
47
|
-
export declare function createNode<T extends NodeClass>(NodeClass: T, options?: CreateNodeOptions): Promise<CreateNodeResult<InstanceType<T>>>;
|
|
48
37
|
|
|
49
|
-
export
|
|
38
|
+
export declare function createNode<T extends NodeClass>(NodeClass: T, options?: CreateNodeOptions): Promise<CreateNodeResult<InstanceType<T>>>;
|