Skip to content

Commit

Permalink
feat: show pd vaidation errors
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra committed Oct 24, 2024
1 parent 4768717 commit 4745645
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/types/Internal.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export class InternalPresentationDefinitionV2 implements PresentationDefinitionV
export interface DiscoveredVersion {
version?: PEVersion;
error?: string;
v1Errors?: any
v2Errors?: any
}

export type IPresentationDefinition = PresentationDefinitionV1 | PresentationDefinitionV2;
Expand Down
6 changes: 5 additions & 1 deletion lib/types/SSITypesBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ export class SSITypesBuilder {
static toInternalPresentationDefinition(presentationDefinition: IPresentationDefinition): IInternalPresentationDefinition {
const presentationDefinitionCopy: IPresentationDefinition = JSON.parse(JSON.stringify(presentationDefinition));
const versionResult: DiscoveredVersion = definitionVersionDiscovery(presentationDefinitionCopy);
if (versionResult.error) throw versionResult.error;
if (versionResult.error) {
throw new Error(
`${versionResult.error} \nv1 errors:\n${JSON.stringify(versionResult.v1Errors, null, 2)} \n\nv2 errors:\n${JSON.stringify(versionResult.v2Errors, null, 2)}`,
);
}
if (versionResult.version == PEVersion.v1) {
return SSITypesBuilder.modelEntityToInternalPresentationDefinitionV1(presentationDefinitionCopy as PresentationDefinitionV1);
}
Expand Down
14 changes: 13 additions & 1 deletion lib/utils/VCUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,26 @@ export function definitionVersionDiscovery(presentationDefinition: IPresentation
JsonPathUtils.changePropertyNameRecursively(presentationDefinitionCopy, '_enum', 'enum');
const data = { presentation_definition: presentationDefinitionCopy };
let result = validatePDv2(data);

if (result) {
return { version: PEVersion.v2 };
}
// Errors are added to the validation method, but not typed correctly
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const v2Errors = validatePDv2.errors;

result = validatePDv1(data);
if (result) {
return { version: PEVersion.v1 };
}
return { error: 'This is not a valid PresentationDefinition' };

// Errors are added to the validation method, but not typed correctly
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const v1Errors = validatePDv1.errors;

return { error: 'This is not a valid PresentationDefinition', v1Errors, v2Errors };
}

export function uniformDIDMethods(dids?: string[], opts?: { removePrefix: 'did:' }) {
Expand Down

0 comments on commit 4745645

Please sign in to comment.