Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rinchan01 committed Nov 26, 2024
1 parent a2c85b8 commit 03147d9
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 28 deletions.
5 changes: 3 additions & 2 deletions internal/validateTokenFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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 = {
Expand Down
6 changes: 3 additions & 3 deletions src/const.ts
Original file line number Diff line number Diff line change
@@ -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]*";
114 changes: 91 additions & 23 deletions src/token-schema.ts
Original file line number Diff line number Diff line change
@@ -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<TokenMetadata> = {
type: "object",
Expand Down Expand Up @@ -49,15 +38,15 @@ export const tokenSchema: JSONSchemaType<TokenMetadata> = {
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: {
Expand All @@ -67,20 +56,97 @@ export const tokenSchema: JSONSchemaType<TokenMetadata> = {
},
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,
},
Expand All @@ -92,3 +158,5 @@ export const tokenSchema: JSONSchemaType<TokenMetadata> = {
additionalProperties: false,
required: ["tokenId", "project", "categories", "decimals", "verified"],
};

console.log();

0 comments on commit 03147d9

Please sign in to comment.