-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from ashwin275/dev/cred-validation
Return Consistent Error Responses to Prevent Server Crashes
- Loading branch information
Showing
4 changed files
with
79 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export function extractCredentialFields(data: any): { | ||
schemaId: string; | ||
properties: any; | ||
} { | ||
let schemaId: string; | ||
let properties: any; | ||
|
||
if (data.credential) { | ||
schemaId = data.credential.cordSchemaId || data.credential.schemaId; | ||
properties = data.credential.credentialSubject; | ||
} else { | ||
schemaId = data.schemaId; | ||
properties = data.properties; | ||
} | ||
|
||
return { | ||
schemaId, | ||
properties, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
export function validateCredential(data: any): string | null { | ||
// If 'credential' is present, validate inside the 'credential' | ||
if (data.credential) { | ||
if (!data.credential.credentialSubject) { | ||
return "'credential.credentialSubject' is required inside 'credential'."; | ||
} | ||
|
||
if (!data.credential.cordSchemaId && !data.credential.schemaId) { | ||
return "'cordSchemaId' or 'schemaId' is required inside 'credential'."; | ||
} | ||
|
||
if ( | ||
!data.credential.credentialSubject || | ||
typeof data.credential.credentialSubject !== 'object' | ||
) { | ||
return "'credential.credentialSubject' must be an object."; | ||
} | ||
|
||
if (Object.keys(data.credential.credentialSubject).length === 0) { | ||
return "'credential.credentialSubject' must contain at least one key-value pair."; | ||
} | ||
} else { | ||
// If 'credential' is not present, validate the 'schemaId' and 'properties' directly | ||
if (!data.schemaId || typeof data.schemaId !== 'string') { | ||
return "'schemaId' is required and must be a string."; | ||
} | ||
|
||
if (!data.properties || typeof data.properties !== 'object') { | ||
return "'properties' is required and must be an object."; | ||
} | ||
|
||
if (Object.keys(data.properties).length === 0) { | ||
return "'properties' must contain at least one key-value pair."; | ||
} | ||
} | ||
|
||
return null; | ||
} |