mirror of
https://codeberg.org/keyoxide/doipjs.git
synced 2025-01-10 22:49:29 -07:00
55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
JavaScript
|
const utils = require('./utils')
|
||
|
|
||
|
const runOnJson = (proofData, checkPath, checkClaim, checkRelation) => {
|
||
|
let isVerified = false, re
|
||
|
|
||
|
if (!proofData) {
|
||
|
return isVerified
|
||
|
}
|
||
|
|
||
|
if (checkPath.length == 0) {
|
||
|
switch (checkRelation) {
|
||
|
default:
|
||
|
case 'contains':
|
||
|
re = new RegExp(checkClaim, "gi")
|
||
|
return re.test(proofData.replace(/\r?\n|\r/, ''))
|
||
|
break
|
||
|
case 'equals':
|
||
|
return proofData.replace(/\r?\n|\r/, '').toLowerCase() == checkClaim.toLowerCase()
|
||
|
break
|
||
|
case 'oneOf':
|
||
|
re = new RegExp(checkClaim, "gi")
|
||
|
return re.test(proofData.join("|"))
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (Array.isArray(proofData)) {
|
||
|
proofData.forEach((item, i) => {
|
||
|
isVerified = isVerified || runOnJson(item, checkPath, checkClaim, checkRelation)
|
||
|
});
|
||
|
} else if (Array.isArray(proofData[checkPath[0]])) {
|
||
|
proofData[checkPath[0]].forEach((item, i) => {
|
||
|
isVerified = isVerified || runOnJson(item, checkPath.slice(1), checkClaim, checkRelation)
|
||
|
})
|
||
|
} else {
|
||
|
isVerified = isVerified || runOnJson(proofData[checkPath[0]], checkPath.slice(1), checkClaim, checkRelation)
|
||
|
}
|
||
|
|
||
|
return isVerified;
|
||
|
}
|
||
|
|
||
|
const run = (proofData, spData) => {
|
||
|
switch (spData.proof.format) {
|
||
|
case 'json':
|
||
|
return runOnJson(proofData, spData.claim.path, utils.generateClaim(spData), spData.claim.relation)
|
||
|
break
|
||
|
case 'text':
|
||
|
re = new RegExp(utils.generateClaim(spData), "gi")
|
||
|
return re.test(proofData.replace(/\r?\n|\r/, ''))
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
|
||
|
exports.run = run
|