diff --git a/client/package.json b/client/package.json index e2ac1b7..5ff6838 100644 --- a/client/package.json +++ b/client/package.json @@ -13,7 +13,7 @@ "start": "next start" }, "dependencies": { - "@ada-anvil/metadraft-validator": "^0.1.2", + "@ada-anvil/metadraft-validator": "^0.1.3", "@radix-ui/react-label": "^2.1.0", "@radix-ui/react-slot": "^1.1.0", "@t3-oss/env-nextjs": "^0.10.1", diff --git a/core/cli/src/mod.ts b/core/cli/src/mod.ts index 214892b..1b24508 100644 --- a/core/cli/src/mod.ts +++ b/core/cli/src/mod.ts @@ -12,7 +12,7 @@ yargs(Deno.args) .command( "validate", "Validate metadata using a template", - (yargs) => { + (yargs: any) => { return yargs .option("m", { alias: "metadata-to-validate", diff --git a/core/cli/src/types.ts b/core/cli/src/types.ts new file mode 100644 index 0000000..c2826f2 --- /dev/null +++ b/core/cli/src/types.ts @@ -0,0 +1,44 @@ +/** + * A union type representing validation states: success, warning, or error. + */ +export type State = "success" | "warning" | "error"; + +/** + * Type representing a validation result containing state, message, input, output (optional), assetName, and validatorId. + */ +export interface Result { + /** + * The current validation state. + */ + state: State; + + /** + * An optional message or object associated with the validation result. Defaults to `undefined`. + */ + message: string | object | undefined; + + /** + * The input data that was validated. + */ + input: unknown; + + /** + * The output data resulting from successful validation (optional). Set to `undefined` when there is an error/warning. + */ + output: unknown | undefined; + + /** + * The name of the asset being validated. + */ + assetName: string; + + /** + * The identifier for the validator used in this result. + */ + validatorId: string; +} + +/** + * Type representing a record containing data keyed by string. + */ +export type DataRead = Record; diff --git a/core/cli/src/validate.ts b/core/cli/src/validate.ts index 763118f..cdb9457 100644 --- a/core/cli/src/validate.ts +++ b/core/cli/src/validate.ts @@ -9,6 +9,7 @@ import { loadTemplates } from "./load-rules.ts"; import { Message, summarize } from "./report.ts"; import { DIVIDER } from "./contstant.ts"; import { extractOptions } from "./utils.ts"; +import { DataRead, Result } from "./types.ts"; export async function validate( metadataPath: string, @@ -37,7 +38,12 @@ export async function validate( const reader = ReaderFactory.createReader( metadataPath.substring(metadataPath.lastIndexOf(".") + 1).toLowerCase(), ); - const metadatas: object[] = await reader.Load(metadataPath); + reader.Load(metadataPath); + const metadatas: DataRead[] | null = reader.Read(); + + if (!metadatas) { + throw new Error("No metadatas loaded"); + } // 4. Run the validation on each asset in the metadata input for (const metadata of metadatas) { @@ -48,7 +54,7 @@ export async function validate( metadatas, ); } - const result = main.GetResults(); + const result: Result[] = main.GetResults(); // 4. Save the report on the local FS fs.writeFileSync( diff --git a/core/validator/deno.json b/core/validator/deno.json index caa826e..b5e0c9c 100644 --- a/core/validator/deno.json +++ b/core/validator/deno.json @@ -1,6 +1,6 @@ { "name": "@ada-anvil/metadraft-validator", - "version": "0.1.2", + "version": "0.1.3", "exports": "./src/mod.ts", "imports": { "@deno/dnt": "jsr:@deno/dnt@^0.41.2", diff --git a/core/validator/src/reader/index.ts b/core/validator/src/reader/index.ts index fca7446..7c045da 100644 --- a/core/validator/src/reader/index.ts +++ b/core/validator/src/reader/index.ts @@ -20,10 +20,10 @@ export abstract class BaseReader implements IReader { /** * Loads data from a specified path or uses provided data. * - * @param {string | PromiseLike} _pathOrData - The file path or data to load. - * @return {Promise | object} The loaded data. + * @param {string} _pathOrData - The file path or data to load. + * @return {DataRead[]} The loaded data. */ - Load(_pathOrData: string): Promise | object { + Load(_pathOrData: string): DataRead[] { throw new Error("Method not implemented."); } /** diff --git a/core/validator/src/reader/readers/csv.ts b/core/validator/src/reader/readers/csv.ts index cb1fe50..6e4e605 100644 --- a/core/validator/src/reader/readers/csv.ts +++ b/core/validator/src/reader/readers/csv.ts @@ -42,10 +42,10 @@ export class CsvReader extends BaseReader { /** * Loads CSV data from a specified path or uses provided CSV data string. * - * @param {string | PromiseLike} pathOrData - The file path containing CSV data or the CSV data as a string. - * @return {object[]} An array of objects representing the parsed CSV data. + * @param {string} pathOrData - The file path containing CSV data or the CSV data as a string. + * @return {DataRead[]} An array of objects representing the parsed CSV data. */ - Load(pathOrData: string): object { + Load(pathOrData: string): DataRead[] { const reader = csvToJson .formatValueByType(this.options.valueByType) .fieldDelimiter(this.options.delimiter) diff --git a/core/validator/src/reader/readers/json.ts b/core/validator/src/reader/readers/json.ts index 5767e83..4735279 100644 --- a/core/validator/src/reader/readers/json.ts +++ b/core/validator/src/reader/readers/json.ts @@ -20,10 +20,10 @@ export class JsonReader extends BaseReader { /** * Loads JSON data from a specified path or uses provided JSON data string. * - * @param {string | PromiseLike} pathOrData - The file path containing JSON data or the JSON data as a string. - * @return {object} The loaded JSON data as an object. + * @param {string} pathOrData - The file path containing JSON data or the JSON data as a string. + * @return {DataRead[]} The loaded JSON data as an object. */ - Load(pathOrData: string): object { + Load(pathOrData: string): DataRead[] { if (isValidPath(pathOrData)) { this.data = JSON.parse(readFileSync(pathOrData, "utf8")); } else { diff --git a/core/validator/src/rules/has-required-keys.ts b/core/validator/src/rules/has-required-keys.ts index e8f3902..382493d 100644 --- a/core/validator/src/rules/has-required-keys.ts +++ b/core/validator/src/rules/has-required-keys.ts @@ -53,16 +53,16 @@ export class HasRequiredKeysValidator extends BaseValidator { const keys = Object.keys(metadata as object); - const policyId = ["name", "image"]; + const requiredKeys = ["name", "image"]; - const hasAllRequiredKeys = policyId.every((requiredKey) => + const hasAllRequiredKeys = requiredKeys.every((requiredKey) => keys.some((key) => key === requiredKey), ); return getStates( { state: hasAllRequiredKeys ? "success" : "error", - message: `Required keys missing: ["name", "description", "image", "mediaType"]. Keys received: ${keys.join(", ")}`, + message: `Required keys missing: ["name", "image"]. Keys received: ${keys.join(", ")}`, }, "All required keys are present.", assetName, diff --git a/core/validator/src/utils/types.ts b/core/validator/src/utils/types.ts index 22b7fed..59902e2 100644 --- a/core/validator/src/utils/types.ts +++ b/core/validator/src/utils/types.ts @@ -56,7 +56,7 @@ export interface Result { /** * An optional message or object associated with the validation result. Defaults to `undefined`. */ - message?: string | object | undefined; + message: string | object | undefined; /** * The input data that was validated. @@ -66,7 +66,7 @@ export interface Result { /** * The output data resulting from successful validation (optional). Set to `undefined` when there is an error/warning. */ - output?: unknown | undefined; + output: unknown | undefined; /** * The name of the asset being validated.