|
| 1 | +/** |
| 2 | + * Copyright 2018 Cloud Ecosystem e.V. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +"use strict"; |
| 18 | +const eioUtils = require('elasticio-node').messages; |
| 19 | +const request = require('request-promise'); |
| 20 | +const Ajv = require('ajv'); |
| 21 | +const folder = require('../schemas/documents/folder.json'); |
| 22 | +const object = require('../schemas/documents/object.json'); |
| 23 | +const record = require('../schemas/oih-data-record.json'); |
| 24 | +const sharedDefinitions = require('../schemas/documents/sharedDefinitions.json'); |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + * Executes the action's logic by sending a request to the Petstore API and emitting response to the platform. |
| 29 | + * The function returns a Promise sending a request and resolving the response as platform message. |
| 30 | + * |
| 31 | + * @param msg incoming messages which is empty for triggers |
| 32 | + * @param cfg object to retrieve triggers configuration values, such as apiKey and pet status |
| 33 | + * @returns promise resolving a message to be emitted to the platform |
| 34 | + */ |
| 35 | +async function processAction(msg, cfg) { |
| 36 | + |
| 37 | + try{ |
| 38 | + let ajv = new Ajv(); |
| 39 | + let validate = ajv.addSchema(sharedDefinitions).addSchema(object).addSchema(record).compile(folder); |
| 40 | + let valid = validate(msg.body); |
| 41 | + |
| 42 | + if (valid) { |
| 43 | + console.log(`FLAG 1`); |
| 44 | + console.log('Validation successful'); |
| 45 | + return eioUtils.newMessageWithBody(msg.body); |
| 46 | + } else { |
| 47 | + console.log(`FLAG 2`); |
| 48 | + console.log(JSON.stringify(validate.errors)); |
| 49 | + throw new Error(JSON.stringify(validate.errors)); |
| 50 | + } |
| 51 | + |
| 52 | + } catch (e) { |
| 53 | + throw new Error(e); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +module.exports.process = processAction; |
0 commit comments