Skip to content

Commit

Permalink
Merge pull request #28 from docknetwork/fix/nested-credential-subject
Browse files Browse the repository at this point in the history
fix: fixing issues with bbs+ nested subjects
  • Loading branch information
maycon-mello authored Apr 17, 2023
2 parents f3b5d2d + 41f77e5 commit 548aa2b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@docknetwork/prettyvc",
"description": "Render pretty verifiable credentials",
"version": "1.3.14",
"version": "1.3.15",
"main": "lib/index.js",
"license": "MIT",
"repository": {
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,19 @@ export function guessCredentialTemplate({ type }, customTemplateMap = {}) {
return customTemplateMap[lastType] || typeToTemplateMap[lastType] || 'credential';
}

export function objectToAttributesArray(object, result = [], parentName = '') {
export function objectToAttributesArray(object, result = [], parentName = '', parentProperty = '') {
const keys = Object.keys(object);

for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const value = object[key];
if (value) {
if (typeof value === 'object') {
objectToAttributesArray(value, result, `${key} `);
objectToAttributesArray(value, result, `${key} `, `${parentProperty ? `${parentProperty}` : ''}${key}.`);
} else {
result.push({
name: humanizeCamelCase(parentName + key),
property: parentName + key,
property: parentProperty + key,
value,
});
}
Expand Down
64 changes: 53 additions & 11 deletions tests/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import { guessCredentialTemplate, renderVCHTML } from '../src/index';
import {
guessCredentialTemplate,
objectToAttributesArray,
renderVCHTML,
} from '../src/index';

describe('Index', () => {
test('guessCredentialTemplate baseline', async () => {
const template = guessCredentialTemplate({
type: ['VerifiableCredential']
type: ['VerifiableCredential'],
});
expect(template).toEqual('credential');
});

test('guessCredentialTemplate diploma', async () => {
const template = guessCredentialTemplate({
type: ['VerifiableCredential', 'UniversityDegreeCredential']
type: ['VerifiableCredential', 'UniversityDegreeCredential'],
});
expect(template).toEqual('diploma');
});

test('guessCredentialTemplate custom', async () => {
const template = guessCredentialTemplate({
type: ['VerifiableCredential', 'DebugCredential']
}, {
DebugCredential: 'debugtemplate'
});
const template = guessCredentialTemplate(
{
type: ['VerifiableCredential', 'DebugCredential'],
},
{
DebugCredential: 'debugtemplate',
},
);
expect(template).toEqual('debugtemplate');
});

Expand All @@ -29,10 +36,45 @@ describe('Index', () => {
credentialSubject: { name: 'test' },
prettyVC: {
type: 'liquid',
proof: '<div style="display:flex">{{credentialSubject.name}}</div>'
}
proof: '<div style="display:flex">{{credentialSubject.name}}</div>',
},
});
expect(html).toEqual('<div style="display:flex">test</div>');
});
});

test('objectToAttributesArray', async () => {
const subject = {
name: 'testing',
level1: {
name: 'Internet Identity Workshop IIWXXXVI #36 2023A',
level2: {
name: 'Computer History Museum',
level3: {
name: 'level3',
},
},
},
};

const result = objectToAttributesArray(subject);

expect(result).toEqual([
{ name: 'Name', property: 'name', value: subject.name },
{
name: 'Level1 name',
property: 'level1.name',
value: subject.level1.name,
},
{
name: 'Level2 name',
property: 'level1.level2.name',
value: subject.level1.level2.name,
},
{
name: 'Level3 name',
property: 'level1.level2.level3.name',
value: subject.level1.level2.level3.name,
},
]);
});
});

0 comments on commit 548aa2b

Please sign in to comment.