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

feature(engine): trigger now returns the concerto validated JSON for … #866

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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