Skip to content

Commit

Permalink
Merge pull request #35 from Cardano-Forge/fix/types-cli-compile
Browse files Browse the repository at this point in the history
fix: types in the CLI part
  • Loading branch information
mrabdibdi-anvil authored Aug 19, 2024
2 parents 4588485 + cf6b729 commit 0890b5e
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 19 deletions.
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion core/cli/src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
44 changes: 44 additions & 0 deletions core/cli/src/types.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>;
10 changes: 8 additions & 2 deletions core/cli/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion core/validator/deno.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 3 additions & 3 deletions core/validator/src/reader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export abstract class BaseReader implements IReader {
/**
* Loads data from a specified path or uses provided data.
*
* @param {string | PromiseLike<string>} _pathOrData - The file path or data to load.
* @return {Promise<object> | object} The loaded data.
* @param {string} _pathOrData - The file path or data to load.
* @return {DataRead[]} The loaded data.
*/
Load(_pathOrData: string): Promise<object> | object {
Load(_pathOrData: string): DataRead[] {
throw new Error("Method not implemented.");
}
/**
Expand Down
6 changes: 3 additions & 3 deletions core/validator/src/reader/readers/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>} 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)
Expand Down
6 changes: 3 additions & 3 deletions core/validator/src/reader/readers/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>} 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 {
Expand Down
6 changes: 3 additions & 3 deletions core/validator/src/rules/has-required-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions core/validator/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit 0890b5e

Please sign in to comment.