From f5bdffbfa29eb9bbbfaf7e2f8e37dbab13c18eab Mon Sep 17 00:00:00 2001 From: artembuslaev <68849616+artembuslaev@users.noreply.github.com> Date: Mon, 30 May 2022 21:02:30 +0400 Subject: [PATCH] 931: fix validate (#940) Co-authored-by: Stepan Kiryakov --- .../policy-engine/blocks/aggregate-block.ts | 2 +- .../src/policy-engine/blocks/report-block.ts | 19 ++++++++++++------- .../src/policy-engine/helpers/utils.ts | 14 +++++++++++--- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/guardian-service/src/policy-engine/blocks/aggregate-block.ts b/guardian-service/src/policy-engine/blocks/aggregate-block.ts index bfba698cd2..30109497e9 100644 --- a/guardian-service/src/policy-engine/blocks/aggregate-block.ts +++ b/guardian-service/src/policy-engine/blocks/aggregate-block.ts @@ -201,7 +201,7 @@ export class AggregateBlock { for (let i = 0; i < vars.length; i++) { const varName = vars[i]; if (!variables[varName]) { - resultsContainer.addBlockError(ref.uuid, `Variable ${varName} not defined`); + resultsContainer.addBlockError(ref.uuid, `Variable '${varName}' not defined`); } } } diff --git a/guardian-service/src/policy-engine/blocks/report-block.ts b/guardian-service/src/policy-engine/blocks/report-block.ts index c5132f0c47..f346a5bc48 100644 --- a/guardian-service/src/policy-engine/blocks/report-block.ts +++ b/guardian-service/src/policy-engine/blocks/report-block.ts @@ -110,7 +110,7 @@ export class ReportBlock { documents: documents } - const vp = await getMongoRepository(VpDocument).findOne({hash, policyId: ref.policyId}); + const vp = await getMongoRepository(VpDocument).findOne({ hash, policyId: ref.policyId }); if (vp) { const vpDocument: IVPReport = { @@ -150,7 +150,7 @@ export class ReportBlock { variables.documentId = doc.id; variables.documentSubjectId = doc.credentialSubject[0].id; } else { - const vc = await getMongoRepository(VcDocument).findOne({hash, policyId: ref.policyId}) + const vc = await getMongoRepository(VcDocument).findOne({ hash, policyId: ref.policyId }) if (vc) { const vcDocument: IVCReport = { @@ -214,7 +214,7 @@ export class ReportBlock { await this.reportUserMap(report); - const schemes = await getMongoRepository(Schema).find({status: SchemaStatus.PUBLISHED}); + const schemes = await getMongoRepository(Schema).find({ status: SchemaStatus.PUBLISHED }); return { hash: hash, @@ -228,9 +228,14 @@ export class ReportBlock { } async setData(user: IAuthUser, data: any) { - const value = data.filterValue; - const blockState = this.state[user.did] || {}; - blockState.lastValue = value; - this.state[user.did] = blockState; + const ref = PolicyComponentsUtils.GetBlockRef(this); + try { + const value = data.filterValue; + const blockState = this.state[user.did] || {}; + blockState.lastValue = value; + this.state[user.did] = blockState; + } catch (error) { + throw new BlockActionError(error, ref.blockType, ref.uuid); + } } } diff --git a/guardian-service/src/policy-engine/helpers/utils.ts b/guardian-service/src/policy-engine/helpers/utils.ts index 1306726bcf..0472d689b0 100644 --- a/guardian-service/src/policy-engine/helpers/utils.ts +++ b/guardian-service/src/policy-engine/helpers/utils.ts @@ -20,9 +20,17 @@ export enum DataTypes { export class PolicyUtils { public static variables(formula: string): string[] { - return mathjs.parse(formula) - .filter((node: any) => node.isSymbolNode) - .map((node: any) => node.name); + const variables = []; + try { + mathjs.parse(formula).traverse((node: any) => { + if (node.isSymbolNode && !mathjs[node.name]) { + variables.push(node.name); + } + }); + return variables; + } catch (error) { + return variables; + } } public static evaluate(formula: string, scope: any) {