Allows multiple claim methods

This commit is contained in:
Yarmo Mackenbach 2022-09-30 23:37:55 +02:00
parent 37913c9327
commit 8a1f8ad586
No known key found for this signature in database
GPG key ID: 37367F4AF4087AD1
2 changed files with 37 additions and 22 deletions

View file

@ -200,33 +200,50 @@ const run = async (proofData, claimData, fingerprint) => {
errors: [] errors: []
} }
const claimMethods = Array.isArray(claimData.claim)
? claimData.claim
: [claimData.claim]
switch (claimData.proof.request.format) { switch (claimData.proof.request.format) {
case E.ProofFormat.JSON: case E.ProofFormat.JSON:
try { for (let index = 0; index < claimMethods.length; index++) {
res.result = await runJSON( const claimMethod = claimMethods[index]
proofData, try {
claimData.claim.path, res.result = res.result || await runJSON(
fingerprint, proofData,
claimData.claim.format, claimMethod.path,
claimData.claim.relation fingerprint,
) claimMethod.format,
res.completed = true claimMethod.relation
} catch (error) { )
res.errors.push(error.message ? error.message : error) } catch (error) {
res.errors.push(error.message ? error.message : error)
}
} }
res.completed = true
break break
case E.ProofFormat.TEXT: case E.ProofFormat.TEXT:
try { for (let index = 0; index < claimMethods.length; index++) {
res.result = await containsProof(proofData, const claimMethod = claimMethods[index]
fingerprint, try {
claimData.claim.format) res.result = res.result || await containsProof(
res.completed = true proofData,
} catch (error) { fingerprint,
res.errors.push('err_unknown_text_verification') claimMethod.format
)
} catch (error) {
res.errors.push('err_unknown_text_verification')
}
} }
res.completed = true
break break
} }
// Reset the errors if one of the claim methods was successful
if (res.result) {
res.errors = []
}
return res return res
} }

View file

@ -49,10 +49,8 @@ const pattern = {
data: _.isObject, data: _.isObject,
}, },
}, },
claim: { claim: (x) => {
format: _.isInteger, return _.isObject(x) || _.isArray(x)
relation: _.isInteger,
path: _.isArray,
}, },
} }