Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
feature(engine): trigger now returns the concerto validated JSON for …
Browse files Browse the repository at this point in the history
…the request

Signed-off-by: Jerome Simeon <jeromesimeon@me.com>
  • Loading branch information
jeromesimeon committed Mar 17, 2022
1 parent a4a0145 commit cc36756
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/ergo-engine/lib/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions packages/ergo-engine/lib/validateES6.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -123,6 +144,7 @@ function validateOutputArray(modelManager, output, utcOffset) {
}

module.exports = {
validateStandard,
validateContract,
validateInput,
validateInputRecord,
Expand Down

0 comments on commit cc36756

Please sign in to comment.