@accordproject/template-engine 2.9.0 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -693,7 +693,7 @@ function getJsonPath(rootData, currentNode, paths) {
693
693
  }
694
694
  }
695
695
  }
696
- if (currentNode.name !== "this") {
696
+ if (currentNode.name !== "this" && currentNode.name !== "top") {
697
697
  withPath.push(`['${currentNode.name}']`);
698
698
  }
699
699
  return withPath.length > 0 ? `$${withPath.join("")}` : "$";
@@ -883,8 +883,17 @@ async function generateAgreement(modelManager, clauseLibrary, templateMark, data
883
883
  }
884
884
  } else if (CONDITIONAL_DEFINITION_RE.test(nodeClass)) {
885
885
  if (context.condition) {
886
- const result = userCodeResults[this.path.join("/")];
887
- context.isTrue = !!result;
886
+ const key = this.path.join("/");
887
+ const resultStr = userCodeResults[key];
888
+ if (resultStr === void 0 || resultStr === null) {
889
+ context.isTrue = false;
890
+ } else {
891
+ try {
892
+ context.isTrue = !!JSON.parse(resultStr);
893
+ } catch (err) {
894
+ throw new Error(`Invalid JSON boolean result for condition '${key}': ${String(err)}`);
895
+ }
896
+ }
888
897
  } else {
889
898
  const path = getJsonPath(templateMark, context, this.path);
890
899
  const variableValues = jp.query(data, path, 1);
@@ -905,16 +914,27 @@ async function generateAgreement(modelManager, clauseLibrary, templateMark, data
905
914
  } else if (CLAUSE_DEFINITION_RE.test(nodeClass)) {
906
915
  const path = getJsonPath(templateMark, context, this.path);
907
916
  const variableValues = jp.query(data, path, 1);
908
- if (context.name !== "top" && (variableValues.length === 0 || variableValues[0] === void 0 || variableValues[0] === null)) {
909
- delete context.nodes;
910
- stopHere = true;
911
- } else if (context.condition) {
917
+ if (context.condition) {
912
918
  checkCode(context.condition);
913
- const result = !!userCodeResults[this.path.join("/")];
919
+ const key = this.path.join("/");
920
+ const resultStr = userCodeResults[key];
921
+ let result = false;
922
+ if (resultStr === void 0 || resultStr === null) {
923
+ result = false;
924
+ } else {
925
+ try {
926
+ result = !!JSON.parse(resultStr);
927
+ } catch (err) {
928
+ throw new Error(`Invalid JSON boolean result for condition '${key}': ${String(err)}`);
929
+ }
930
+ }
914
931
  if (!result) {
915
932
  delete context.nodes;
916
933
  stopHere = true;
917
934
  }
935
+ } else if (context.name !== "top" && (variableValues.length === 0 || variableValues[0] === void 0 || variableValues[0] === null)) {
936
+ delete context.nodes;
937
+ stopHere = true;
918
938
  }
919
939
  delete context.condition;
920
940
  delete context.functionName;
@@ -963,14 +983,14 @@ class TemplateMarkInterpreter {
963
983
  * @throws {Error} if the templateMark document is invalid
964
984
  */
965
985
  checkTypes(templateMark) {
966
- const modelManager = new ModelManager({ strict: true });
986
+ const modelManager = new ModelManager();
967
987
  modelManager.addCTOModel(ConcertoMetaModel.MODEL, "concertometamodel.cto");
968
988
  modelManager.addCTOModel(CommonMarkModel.MODEL, "commonmark.cto");
969
989
  modelManager.addCTOModel(TemplateMarkModel.MODEL, "templatemark.cto");
970
990
  const factory = new Factory(modelManager);
971
- const serializer = new Serializer(factory, modelManager);
991
+ const serializer = new Serializer(factory, modelManager, {});
972
992
  try {
973
- serializer.fromJSON(templateMark);
993
+ serializer.fromJSON(templateMark, {});
974
994
  } catch (err) {
975
995
  throw new Error(`Generated invalid agreement: ${err}: ${JSON.stringify(templateMark, null, 2)}`);
976
996
  }
@@ -1042,22 +1062,22 @@ class TemplateMarkInterpreter {
1042
1062
  return compiler.compile(templateMark);
1043
1063
  }
1044
1064
  validateCiceroMark(ciceroMark) {
1045
- const modelManager = new ModelManager({ strict: true });
1065
+ const modelManager = new ModelManager();
1046
1066
  modelManager.addCTOModel(ConcertoMetaModel.MODEL, "concertometamodel.cto");
1047
1067
  modelManager.addCTOModel(CommonMarkModel.MODEL, "commonmark.cto");
1048
1068
  modelManager.addCTOModel(CiceroMarkModel.MODEL, "ciceromark.cto");
1049
1069
  const factory = new Factory(modelManager);
1050
- const serializer = new Serializer(factory, modelManager);
1070
+ const serializer = new Serializer(factory, modelManager, {});
1051
1071
  try {
1052
- return serializer.fromJSON(ciceroMark);
1072
+ return serializer.fromJSON(ciceroMark, {});
1053
1073
  } catch (err) {
1054
1074
  throw new Error(`Generated invalid agreement: ${err}: ${JSON.stringify(ciceroMark, null, 2)}`);
1055
1075
  }
1056
1076
  }
1057
1077
  async generate(templateMark, data, options) {
1058
1078
  const factory = new Factory(this.modelManager);
1059
- const serializer = new Serializer(factory, this.modelManager);
1060
- const templateData = serializer.fromJSON(data);
1079
+ const serializer = new Serializer(factory, this.modelManager, {});
1080
+ const templateData = serializer.fromJSON(data, {});
1061
1081
  if (templateData.getFullyQualifiedType() !== this.templateClass.getFullyQualifiedName()) {
1062
1082
  throw new Error(`Template data must be of type '${this.templateClass.getFullyQualifiedName()}'.`);
1063
1083
  }
@@ -1106,7 +1126,7 @@ class TemplateArchiveProcessor {
1106
1126
  * @param {object} request - the request to send to the template logic
1107
1127
  * @param {object} state - the current state of the template
1108
1128
  * @param {[string]} currentTime - the current time, defaults to now
1109
- * @param {[number]} utcOffset - the UTC offer, defaults to zero
1129
+ * @param {[number]} utcOffset - the UTC offset, defaults to zero
1110
1130
  * @returns {Promise} the response and any events
1111
1131
  */
1112
1132
  async trigger(data, request, state, currentTime, utcOffset) {
@@ -1126,6 +1146,8 @@ class TemplateArchiveProcessor {
1126
1146
  const result = compiler.compile(code);
1127
1147
  compiledCode[tsFile.getIdentifier()] = result;
1128
1148
  }
1149
+ const resolvedTime = currentTime ?? (/* @__PURE__ */ new Date()).toISOString();
1150
+ const resolvedOffset = utcOffset ?? 0;
1129
1151
  const evaluator = new JavaScriptEvaluator();
1130
1152
  const evalResponse = await evaluator.evalDangerously({
1131
1153
  templateLogic: true,
@@ -1134,7 +1156,7 @@ class TemplateArchiveProcessor {
1134
1156
  code: compiledCode["logic/logic.ts"].code,
1135
1157
  // TODO DCS - how to find the code to run?
1136
1158
  argumentNames: ["data", "request", "state"],
1137
- arguments: [data, request, state, currentTime, utcOffset]
1159
+ arguments: [data, request, state, resolvedTime, resolvedOffset]
1138
1160
  });
1139
1161
  if (evalResponse.result) {
1140
1162
  return evalResponse.result;
@@ -1148,8 +1170,8 @@ class TemplateArchiveProcessor {
1148
1170
  /**
1149
1171
  * Init the logic of a template
1150
1172
  * @param {[string]} currentTime - the current time, defaults to now
1151
- * @param {[number]} utcOffset - the UTC offer, defaults to zero
1152
- * @returns {Promise} the response and any events
1173
+ * @param {[number]} utcOffset - the UTC offset, defaults to zero
1174
+ * @returns {Promise<InitResponse>} the new state
1153
1175
  */
1154
1176
  async init(data, currentTime, utcOffset) {
1155
1177
  const logicManager = this.template.getLogicManager();
@@ -1168,6 +1190,8 @@ class TemplateArchiveProcessor {
1168
1190
  const result = compiler.compile(code);
1169
1191
  compiledCode[tsFile.getIdentifier()] = result;
1170
1192
  }
1193
+ const resolvedTime = currentTime ?? (/* @__PURE__ */ new Date()).toISOString();
1194
+ const resolvedOffset = utcOffset ?? 0;
1171
1195
  const evaluator = new JavaScriptEvaluator();
1172
1196
  const evalResponse = await evaluator.evalDangerously({
1173
1197
  templateLogic: true,
@@ -1176,7 +1200,7 @@ class TemplateArchiveProcessor {
1176
1200
  code: compiledCode["logic/logic.ts"].code,
1177
1201
  // TODO DCS - how to find the code to run?
1178
1202
  argumentNames: ["data"],
1179
- arguments: [data, currentTime, utcOffset]
1203
+ arguments: [data, resolvedTime, resolvedOffset]
1180
1204
  });
1181
1205
  if (evalResponse.result) {
1182
1206
  return evalResponse.result;