-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'LN-1620-metadata-error-states-support' into 'dev'
fix: improve error handling and UI elements for governance actions See merge request voltaire/govtool-outcomes-pillar!52
- Loading branch information
Showing
40 changed files
with
1,040 additions
and
377 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,7 @@ | ||
export enum LoggerMessage { | ||
METADATA_VALIDATION_ERROR = "Metadata validation error", | ||
METADATA_DATA = "Metadata data", | ||
CANNOT_GET_METADATA_URL = "Cannot get metadata from URL", | ||
PARSED_METADATA_BODY = "Parsed metadata body", | ||
CANNOT_PARSE_METADATA_BODY = "Cannot parse metadata body", | ||
} |
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,6 @@ | ||
export enum MetadataValidationStatus { | ||
URL_NOT_FOUND = "URL_NOT_FOUND", | ||
INVALID_JSONLD = "INVALID_JSONLD", | ||
INVALID_HASH = "INVALID_HASH", | ||
INCORRECT_FORMAT = "INCORRECT_FORMAT", | ||
} |
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,12 @@ | ||
import { MetadataValidationStatus } from "src/enums/ValidationErrors"; | ||
|
||
export enum MetadataStandard { | ||
CIP108 = "CIP108", | ||
CIP119 = "CIP119", | ||
} | ||
|
||
export type ValidateMetadataResult = { | ||
metadataStatus?: MetadataValidationStatus; | ||
metadataValid: boolean; | ||
data?: any; | ||
}; |
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,22 @@ | ||
/** | ||
* Retrieves the value of a specified field from a given object. | ||
* | ||
* @param body - The object from which to retrieve the field value. | ||
* @param field - The name of the field to retrieve the value from. | ||
* @returns The value of the specified field, or undefined if the field does not exist. | ||
*/ | ||
export const getFieldValue = ( | ||
body: Record<string, unknown>, | ||
field: string | ||
): unknown => { | ||
const fieldValue = body[field]; | ||
if (fieldValue.hasOwnProperty("@value")) { | ||
return fieldValue["@value"]; | ||
} | ||
|
||
if (fieldValue) { | ||
return fieldValue; | ||
} | ||
|
||
return undefined; | ||
}; |
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,17 @@ | ||
import { MetadataStandard } from "src/types/validateMetadata"; | ||
/** | ||
* Retrieves the metadata standard from the given data. | ||
* @param data - The data containing the metadata. | ||
* @returns The metadata standard if found, otherwise undefined. | ||
*/ | ||
export const getStandard = ( | ||
data: Record<string, unknown> | ||
): MetadataStandard | undefined => { | ||
if (JSON.stringify(data).includes(MetadataStandard.CIP119)) { | ||
return MetadataStandard.CIP119; | ||
} | ||
if (JSON.stringify(data).includes(MetadataStandard.CIP108)) { | ||
return MetadataStandard.CIP108; | ||
} | ||
return undefined; | ||
}; |
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,23 @@ | ||
import { getFieldValue } from './getFieldValue'; | ||
|
||
/** | ||
* Parses the metadata from the given body object. | ||
* | ||
* @param body - The body object containing the metadata. | ||
* @returns An object with the parsed metadata. | ||
*/ | ||
export const parseMetadata = (body: Record<string, unknown>) => { | ||
const metadata = {}; | ||
|
||
Object.keys(body).forEach((key) => { | ||
if (key === 'references') { | ||
const parsedReferences = (body[key] as Record<string, unknown>[]).map( | ||
(reference) => parseMetadata(reference), | ||
); | ||
metadata[key] = parsedReferences; | ||
} else { | ||
metadata[key] = getFieldValue(body, key); | ||
} | ||
}); | ||
return metadata; | ||
}; |
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,24 @@ | ||
import { MetadataValidationStatus } from "src/enums/ValidationErrors"; | ||
import { getFieldValue } from "./getFieldValue"; | ||
|
||
/** | ||
* Validates the body of a CIP108 standard. | ||
* | ||
* @param body - The body of the metadata. | ||
* @returns True if the body is valid, otherwise throws an error. | ||
* @throws {MetadataValidationStatus} - Throws an error if the body is not in the correct format. | ||
*/ | ||
export const validateCIP108body = (body: Record<string, unknown>) => { | ||
const title = getFieldValue(body, "title"); | ||
const abstract = getFieldValue(body, "abstract"); | ||
const motivation = getFieldValue(body, "motivation"); | ||
const rationale = getFieldValue(body, "rationale"); | ||
if (!title || !abstract || !motivation || !rationale) { | ||
throw MetadataValidationStatus.INCORRECT_FORMAT; | ||
} | ||
if (String(title).length > 80 || String(abstract).length > 2500) { | ||
throw MetadataValidationStatus.INCORRECT_FORMAT; | ||
} | ||
|
||
return true; | ||
}; |
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,43 @@ | ||
import { Logger } from "@nestjs/common"; | ||
import { LoggerMessage } from "src/enums/LoggerMessage"; | ||
import { MetadataValidationStatus } from "src/enums/ValidationErrors"; | ||
import { MetadataStandard } from "src/types/validateMetadata"; | ||
import { getFieldValue } from "./getFieldValue"; | ||
import { validateCIP108body } from "./validateCIP108body"; | ||
|
||
/** | ||
* Validates the metadata against a specific standard. | ||
* @param body - The metadata body to be validated. | ||
* @param standard - The metadata standard to validate against. | ||
* @throws {MetadataValidationStatus.INCORRECT_FORMAT} - If the metadata does not conform to the specified standard. | ||
*/ | ||
export const validateMetadataStandard = async ( | ||
body: Record<string, unknown>, | ||
standard: MetadataStandard | ||
) => { | ||
try { | ||
switch (standard) { | ||
// givenName is the only compulsory field in CIP119 | ||
case MetadataStandard.CIP119: | ||
const givenName = getFieldValue(body, "givenName"); | ||
if (!givenName) { | ||
Logger.error( | ||
LoggerMessage.METADATA_VALIDATION_ERROR, | ||
MetadataValidationStatus.INCORRECT_FORMAT | ||
); | ||
throw MetadataValidationStatus.INCORRECT_FORMAT; | ||
} | ||
return true; | ||
case MetadataStandard.CIP108: | ||
return validateCIP108body(body); | ||
default: | ||
return true; | ||
} | ||
} catch (error) { | ||
Logger.error( | ||
LoggerMessage.METADATA_VALIDATION_ERROR, | ||
MetadataValidationStatus.INCORRECT_FORMAT | ||
); | ||
throw MetadataValidationStatus.INCORRECT_FORMAT; | ||
} | ||
}; |
Oops, something went wrong.