Skip to content

Commit

Permalink
Merge pull request #98 from 0xPolygonID/fix/support-boolean
Browse files Browse the repository at this point in the history
Advanced boolean search credential support.
  • Loading branch information
vmidyllic authored Jul 13, 2023
2 parents 3e8722e + 0198a4a commit 2030824
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 211 deletions.
114 changes: 57 additions & 57 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@0xpolygonid/js-sdk",
"version": "1.0.0-beta.17",
"version": "1.0.0-beta.18",
"description": "SDK to work with Polygon ID",
"main": "dist/cjs/index.js",
"module": "dist/esm_esbuild/index.js",
Expand Down
18 changes: 16 additions & 2 deletions src/storage/filters/jsonQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,29 @@ export interface IFilterQuery {
execute(credential: W3CCredential): boolean;
}

const truthyValues = [true, 1, 'true'];
const falsyValues = [false, 0, 'false'];

const equalsComparator = (a, b) => {
if (truthyValues.includes(a) && truthyValues.includes(b)) {
return true;
}
if (falsyValues.includes(a) && falsyValues.includes(b)) {
return true;
}

return a === b;
};

export /** @type {*} - filter operators and their functions */
const comparatorOptions: { [v in FilterOperatorMethod]: FilterOperatorFunction } = {
$noop: () => true,
$eq: (a, b) => a === b,
$eq: (a, b) => equalsComparator(a, b),
$in: (a: string, b: string[]) => b.includes(a),
$nin: (a: string, b: string[]) => !b.includes(a),
$gt: (a: number, b: number) => a > b,
$lt: (a: number, b: number) => a < b,
$ne: (a, b) => a !== b
$ne: (a, b) => !equalsComparator(a, b)
};

/**
Expand Down
81 changes: 81 additions & 0 deletions tests/credentials/credential-wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,87 @@ const credentialFlow = async (storage: IDataStorage) => {
}
},
expected: [cred4]
},
{
query: {
allowedIssuers: ['*'],
credentialSubject: {
'country.insured': { $eq: 'true' }
}
},
expected: [cred4]
},
{
query: {
allowedIssuers: ['*'],
credentialSubject: {
'country.insured': { $eq: 1 }
}
},
expected: [cred4]
},
{
query: {
allowedIssuers: ['*'],
credentialSubject: {
'country.insured': { $eq: true }
}
},
expected: [cred4]
},
{
query: {
allowedIssuers: ['*'],
credentialSubject: {
'country.insured': { $ne: 'false' }
}
},
expected: [cred4]
},
{
query: {
allowedIssuers: ['*'],
credentialSubject: {
'country.insured': { $ne: 0 }
}
},
expected: [cred4]
},
{
query: {
allowedIssuers: ['*'],
credentialSubject: {
'country.insured': { $ne: false }
}
},
expected: [cred4]
},
{
query: {
allowedIssuers: ['*'],
credentialSubject: {
'country.hasOwnPackage': { $eq: 'false' }
}
},
expected: [cred4]
},
{
query: {
allowedIssuers: ['*'],
credentialSubject: {
'country.hasOwnPackage': { $eq: 0 }
}
},
expected: [cred4]
},
{
query: {
allowedIssuers: ['*'],
credentialSubject: {
'country.hasOwnPackage': { $eq: false }
}
},
expected: [cred4]
}
];

Expand Down
4 changes: 3 additions & 1 deletion tests/credentials/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ export const cred4 = createTestCredential({
countOfFines: 0,
country: {
name: 'Spain',
code: 'ES'
code: 'ES',
insured: true,
hasOwnPackage: 'false'
}
},
expirationDate: '2023-11-11',
Expand Down
Loading

0 comments on commit 2030824

Please sign in to comment.