-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: applied validations on schema url attributes for jsonld (#792)
Signed-off-by: bhavanakarwade <bhavana.karwade@ayanworks.com> Signed-off-by: KulkarniShashank <shashank.kulkarni@ayanworks.com>
- Loading branch information
1 parent
d23b4a5
commit fb397c3
Showing
4 changed files
with
171 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { BadRequestException } from '@nestjs/common'; | ||
import { IIssuanceAttributes, ISchemaAttributes } from 'apps/issuance/interfaces/issuance.interfaces'; | ||
|
||
export function validateW3CSchemaAttributes( | ||
filteredIssuanceAttributes: IIssuanceAttributes, | ||
schemaUrlAttributes: ISchemaAttributes[] | ||
): void { | ||
const mismatchedAttributes: string[] = []; | ||
const missingAttributes: string[] = []; | ||
const extraAttributes: string[] = []; | ||
|
||
const schemaAttributesSet = new Set(schemaUrlAttributes.map((attr) => attr.attributeName)); | ||
|
||
for (const schemaAttribute of schemaUrlAttributes) { | ||
const { attributeName, schemaDataType, isRequired } = schemaAttribute; | ||
const attributeValue = filteredIssuanceAttributes[attributeName]; | ||
|
||
if (isRequired && attributeValue === undefined) { | ||
missingAttributes.push(`Attribute ${attributeName} is missing`); | ||
continue; | ||
} | ||
|
||
if (isRequired && !attributeValue) { | ||
mismatchedAttributes.push(`Attribute ${attributeName} must have a non-empty value`); | ||
continue; | ||
} | ||
|
||
if (attributeValue !== undefined) { | ||
const actualType = typeof attributeValue; | ||
if (actualType !== schemaDataType) { | ||
mismatchedAttributes.push( | ||
`Attribute ${attributeName} has type ${actualType} but expected type ${schemaDataType}` | ||
); | ||
} | ||
} | ||
} | ||
|
||
for (const attributeName in filteredIssuanceAttributes) { | ||
if (!schemaAttributesSet.has(attributeName)) { | ||
extraAttributes.push(`Attribute ${attributeName} is not defined in the schema`); | ||
} | ||
} | ||
|
||
if (0 < missingAttributes.length) { | ||
throw new BadRequestException(`Validation failed: ${missingAttributes.join(', ')}`); | ||
} | ||
|
||
if (0 < mismatchedAttributes.length) { | ||
throw new BadRequestException(`Validation failed: ${mismatchedAttributes.join(', ')}`); | ||
} | ||
|
||
if (0 < extraAttributes.length) { | ||
throw new BadRequestException(`Validation failed: ${extraAttributes.join(', ')}`); | ||
} | ||
} |
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