From 8a1f8ad5867272a94c5b93020db5a6a0fd1ade91 Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Fri, 30 Sep 2022 23:37:55 +0200 Subject: [PATCH] Allows multiple claim methods --- src/verifications.js | 53 +++++++++++++++++++++++------------ test/claimDefinitions.test.js | 6 ++-- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/verifications.js b/src/verifications.js index c5ce662..41f9637 100644 --- a/src/verifications.js +++ b/src/verifications.js @@ -200,33 +200,50 @@ const run = async (proofData, claimData, fingerprint) => { errors: [] } + const claimMethods = Array.isArray(claimData.claim) + ? claimData.claim + : [claimData.claim] + switch (claimData.proof.request.format) { case E.ProofFormat.JSON: - try { - res.result = await runJSON( - proofData, - claimData.claim.path, - fingerprint, - claimData.claim.format, - claimData.claim.relation - ) - res.completed = true - } catch (error) { - res.errors.push(error.message ? error.message : error) + for (let index = 0; index < claimMethods.length; index++) { + const claimMethod = claimMethods[index] + try { + res.result = res.result || await runJSON( + proofData, + claimMethod.path, + fingerprint, + claimMethod.format, + claimMethod.relation + ) + } catch (error) { + res.errors.push(error.message ? error.message : error) + } } + res.completed = true break case E.ProofFormat.TEXT: - try { - res.result = await containsProof(proofData, - fingerprint, - claimData.claim.format) - res.completed = true - } catch (error) { - res.errors.push('err_unknown_text_verification') + for (let index = 0; index < claimMethods.length; index++) { + const claimMethod = claimMethods[index] + try { + res.result = res.result || await containsProof( + proofData, + fingerprint, + claimMethod.format + ) + } catch (error) { + res.errors.push('err_unknown_text_verification') + } } + res.completed = true break } + // Reset the errors if one of the claim methods was successful + if (res.result) { + res.errors = [] + } + return res } diff --git a/test/claimDefinitions.test.js b/test/claimDefinitions.test.js index 3f494ae..e580ff0 100644 --- a/test/claimDefinitions.test.js +++ b/test/claimDefinitions.test.js @@ -49,10 +49,8 @@ const pattern = { data: _.isObject, }, }, - claim: { - format: _.isInteger, - relation: _.isInteger, - path: _.isArray, + claim: (x) => { + return _.isObject(x) || _.isArray(x) }, }