UNPKG

@energyweb/node-red-contrib-green-proof-worker

Version:
34 lines (33 loc) 1.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonSchemaValidator = void 0; const tslib_1 = require("tslib"); const ajv_1 = require("ajv"); const z = tslib_1.__importStar(require("zod")); const errors_1 = require("../errors"); const node_1 = require("../node"); const InputMessage = z.looseObject({ payload: z.looseObject({}), }); const Config = z.object({ jsonSchema: z.string().optional(), }); const JsonSchemaValidator = (api) => class JsonSchemaValidator extends node_1.Node { constructor(config) { super(api, config, InputMessage); this.config = Config.parse(config); const ajv = new ajv_1.Ajv({ allErrors: true }); this.validateFn = ajv.compile(JSON.parse(this.config.jsonSchema || '{}')); } onInput(message) { const validation = this.validateFn(message.payload); if (validation === false) { throw new errors_1.GGPError(errors_1.ErrorCode.JsonSchemaValidationFailed, { error: this.validateFn.errors, inputMessagePayload: message.payload, }); } this.sendBuilder(message).sendToOutput(0); } }; exports.JsonSchemaValidator = JsonSchemaValidator;