diff --git a/internal/validateTokenFiles.ts b/internal/validateTokenFiles.ts index a925b5d..b233a8f 100644 --- a/internal/validateTokenFiles.ts +++ b/internal/validateTokenFiles.ts @@ -4,7 +4,7 @@ import * as fs from "node:fs"; import { load } from "js-yaml"; import { execSync } from "node:child_process"; -import { DEFAULT_TOKEN_DIR, FILE_REGEX } from "@/const"; +import { DEFAULT_TOKEN_DIR } from "@/const"; import type { TokenMetadata } from "@/types"; import { tokenSchema } from "@/token-schema"; @@ -17,7 +17,8 @@ async function validateTokenFiles(files: string[]) { if (!file.includes("src/tokens")) { continue; } - const fileName = file.replace(FILE_REGEX, ""); + const fileComponents = file.split("/"); + const fileName = fileComponents[fileComponents.length - 1]; const filePath = path.join(TOKEN_DIR, `${fileName}`); const tokenFileData = fs.readFileSync(filePath, "utf-8"); const tokenData: TokenMetadata = { diff --git a/src/const.ts b/src/const.ts index 1c4c25f..10e5e5e 100644 --- a/src/const.ts +++ b/src/const.ts @@ -1,5 +1,5 @@ export const DEFAULT_TOKEN_DIR = "tokens"; export const DEFAULT_FETCH_TIMEOUT = 20_000; -export const URI_REGEX = /^https:(?:\/\/(?:((?:[a-z0-9-._~!$&'()*+,;=:]|%[0-9A-F]{2})*)@)?((?:[a-z0-9-._~!$&'()*+,;=]|%[0-9A-F]{2})*)(?::(\d*))?(\/(?:[a-z0-9-._~!$&'()*+,;=:@/]|%[0-9A-F]{2})*)?|(\/?(?:[a-z0-9-._~!$&'()*+,;=:@]|%[0-9A-F]{2})+(?:[a-z0-9-._~!$&'()*+,;=:@/]|%[0-9A-F]{2})*)?)(?:\?((?:[a-z0-9-._~!$&'()*+,;=:/?@]|%[0-9A-F]{2})*))?(?:#((?:[a-z0-9-._~!$&'()*+,;=:/?@]|%[0-9A-F]{2})*))?$/ -export const ADDRESS_REGEX = /^(addr|stake)[0-9a-z]+/ -export const FILE_REGEX = /^.*[/]/; +export const URL_REGEX = "^https:\/\/.*$"; +export const ADDRESS_REGEX = "^(addr|stake)[0-9a-zA-Z]+"; +export const ASSET_ID_REGEX = "^[a-fA-F0-9]{56}[a-fA-F0-9]*"; diff --git a/src/token-schema.ts b/src/token-schema.ts index fcf55b3..3152a41 100644 --- a/src/token-schema.ts +++ b/src/token-schema.ts @@ -1,18 +1,7 @@ -import { Ajv, type JSONSchemaType } from "ajv"; -import addFormats from "ajv-formats"; +import type { JSONSchemaType } from "ajv"; import type { TokenMetadata } from "./types"; -import { ADDRESS_REGEX, URI_REGEX } from "./const"; - -const ajv = new Ajv({ - validateFormats: true, - validateSchema: true -}); - -addFormats(ajv); - -// ajv.addFormat("uri", /^https:\/\/.*/); -// ajv.addFormat("address", /^(addr1|stake1)[a-z0-9]+/); +import { ADDRESS_REGEX, URL_REGEX, ASSET_ID_REGEX } from "./const"; export const tokenSchema: JSONSchemaType = { type: "object", @@ -49,15 +38,15 @@ export const tokenSchema: JSONSchemaType = { socialLinks: { type: "object", properties: { - website: { type: "string", nullable: true }, - twitter: { type: "string", nullable: true }, - discord: { type: "string", nullable: true }, - telegram: { type: "string", nullable: true }, - coinMarketCap: { type: "string", nullable: true }, - coinGecko: { type: "string", nullable: true }, + website: { type: "string", nullable: true, pattern: URL_REGEX }, + twitter: { type: "string", nullable: true, pattern: URL_REGEX }, + discord: { type: "string", nullable: true, pattern: URL_REGEX }, + telegram: { type: "string", nullable: true, pattern: URL_REGEX }, + coinMarketCap: { type: "string", nullable: true, pattern: URL_REGEX }, + coinGecko: { type: "string", nullable: true, pattern: URL_REGEX }, }, nullable: true, - additionalProperties: false + additionalProperties: false, }, verified: { type: "boolean", default: true }, maxSupply: { @@ -67,20 +56,97 @@ export const tokenSchema: JSONSchemaType = { }, nullable: true, }, + // $defs: { + // name: { + // type: "array", + // items: { + // oneOf: [ + // { + // type: "string", + // pattern: ADDRESS_REGEX, + // }, + // { + // type: "string", + // pattern: URL_REGEX, + // }, + // { + // type: "string", + // pattern: ASSET_ID_REGEX, + // }, + // { + // type: "number", + // }, + // ], + // }, + // nullable: true, + // }, + // }, treasury: { type: "array", - items: { type: ["string", "number"] }, + items: { + oneOf: [ + { + type: "string", + pattern: ADDRESS_REGEX, + }, + { + type: "string", + pattern: URL_REGEX, + }, + { + type: "string", + pattern: ASSET_ID_REGEX, + }, + { + type: "number", + }, + ], + }, nullable: true, }, burn: { type: "array", - items: { type: ["string", "number"] }, + items: { + oneOf: [ + { + type: "string", + pattern: ADDRESS_REGEX, + }, + { + type: "string", + pattern: URL_REGEX, + }, + { + type: "string", + pattern: ASSET_ID_REGEX, + }, + { + type: "number", + }, + ], + }, nullable: true, }, circulatingOnChain: { type: "array", items: { - type: ["string", "number"], + oneOf: [ + { + type: "string", + pattern: ADDRESS_REGEX, + }, + { + type: "string", + pattern: URL_REGEX, + }, + { + type: "string", + pattern: ASSET_ID_REGEX, + }, + { + type: "number", + }, + ], }, nullable: true, }, @@ -92,3 +158,5 @@ export const tokenSchema: JSONSchemaType = { additionalProperties: false, required: ["tokenId", "project", "categories", "decimals", "verified"], }; + +console.log();