From 98be44cca5e2d07abed007521d3e4f4e773157c2 Mon Sep 17 00:00:00 2001 From: Jerome Simeon Date: Thu, 17 Mar 2022 17:08:41 -0400 Subject: [PATCH] feature(engine): trigger now returns the concerto validated JSON for the request Signed-off-by: Jerome Simeon --- packages/ergo-engine/lib/engine.js | 3 ++- packages/ergo-engine/lib/validateES6.js | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/ergo-engine/lib/engine.js b/packages/ergo-engine/lib/engine.js index 83fea73f..28fb6422 100644 --- a/packages/ergo-engine/lib/engine.js +++ b/packages/ergo-engine/lib/engine.js @@ -160,13 +160,14 @@ class Engine { // execute the logic const result = this.runVMScriptCall(offset,now,validOptions,context,script,callScript); + const outputRequest = validateES6.validateStandard(modelManager, request, offset); const validResponse = validateES6.validateOutput(modelManager, result.__response, offset); // ensure the response is valid const validNewState = validateES6.validateOutput(modelManager, result.__state, offset); // ensure the new state is valid const validEmit = validateES6.validateOutputArray(modelManager, result.__emit, offset); // ensure all the emits are valid const answer = { 'clause': contractId, - 'request': request, // Keep the original request + 'request': outputRequest, 'response': validResponse, 'state': validNewState, 'emit': validEmit, diff --git a/packages/ergo-engine/lib/validateES6.js b/packages/ergo-engine/lib/validateES6.js index c6e8822c..cabbd7e2 100644 --- a/packages/ergo-engine/lib/validateES6.js +++ b/packages/ergo-engine/lib/validateES6.js @@ -63,6 +63,27 @@ function validateInput(modelManager, input, utcOffset) { return vJson; } +/** + * Validate standard + * @param {object} modelManager - the Concerto model manager + * @param {object} input - the input JSON + * @param {number} utcOffset - UTC Offset for DateTime values + * @return {object} the validated input + */ +function validateStandard(modelManager, input, utcOffset) { + const factory = new Factory(modelManager); + const serializer = new Serializer(factory, modelManager); + + if (input === null) { return null; } + + // ensure the input is valid + const validInput = serializer.fromJSON(input, {validate: false, acceptResourcesForRelationships: true, utcOffset}); + validInput.$validator = new ResourceValidator({permitResourcesForRelationships: true}); + validInput.validate(); + const vJson = serializer.toJSON(validInput, {permitResourcesForRelationships:true, utcOffset}); + return vJson; +} + /** * Validate input JSON record * @param {object} modelManager - the Concerto model manager @@ -123,6 +144,7 @@ function validateOutputArray(modelManager, output, utcOffset) { } module.exports = { + validateStandard, validateContract, validateInput, validateInputRecord,