Skip to content

Commit

Permalink
Validate token list schema (#30)
Browse files Browse the repository at this point in the history
* Add code to validate schema of the tokenlist

* Update gh actions to use reusable workflow

* Add error code on exit

* Move schema validation to pretest script

* Keep data when throwing errors

* Use hash for js-checks workflow
  • Loading branch information
gndelia authored Jan 24, 2025
1 parent 9ee0735 commit 27d7b89
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 26 deletions.
26 changes: 1 addition & 25 deletions .github/workflows/js-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,6 @@ concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
cancel-in-progress: true

# The reusable workflow at bloq/actions/.github/workflows/js-checks.yml@v1
# cannot be used as environment variables must be sent to the test command to
# overwrite the EVM RPC URL of Hemi mainnet. When this is not needed anymore,
# the custom workflow should be replaced by the reusable one.

jobs:
js-checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: bloq/actions/setup-node-env@v1
with:
cache: npm
- run: npm run --if-present format:check
- run: npm run --if-present lint
- run: npm run --if-present deps:check
run-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: bloq/actions/setup-node-env@v1
with:
cache: npm
- run: npm run test
env:
EVM_RPC_URL_43111: ${{ secrets.EVM_RPC_URL_43111 }}
EVM_RPC_URL_743111: ${{ secrets.WEB3_RPC_743111 }}
uses: hemilabs/actions/.github/workflows/js-checks.yml@01a748b9e1b966ae64d2a7b146ff7e4700dc1da5
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
"format:check": "prettier --check .",
"lint": "eslint --cache .",
"prepare": "husky",
"schema:validate": "node scripts/validate-list.js",
"pretest": "npm run schema:validate",
"test": "node --test",
"version": "node scripts/sync-version.js && git add ."
},
"devDependencies": {
"@commitlint/cli": "19.5.0",
"ajv": "8.17.1",
"ajv-formats": "3.0.1",
"better-sort-package-json": "1.1.0",
"commitlint-config-bloq": "1.1.0",
"eslint": "8.57.1",
Expand Down
33 changes: 33 additions & 0 deletions scripts/validate-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Ajv from "ajv";
import addFormats from "ajv-formats";
import fs from "node:fs";
import { exit } from "node:process";

// eslint fails to parse "with { type: "json" }"
// See https://github.com/eslint/eslint/discussions/15305
const tokenList = JSON.parse(fs.readFileSync("./src/hemi.tokenlist.json"));

const schemaUrl =
"https://raw.githubusercontent.com/Uniswap/token-lists/main/src/tokenlist.schema.json";

// See https://github.com/uniswap/token-lists?tab=readme-ov-file#validating-token-lists
async function validate() {
const ajv = new Ajv({ allErrors: true, verbose: true });
addFormats(ajv);
const schema = await fetch(schemaUrl).then((r) => r.json());
const validator = ajv.compile(schema);
const valid = validator(tokenList);
if (valid) {
return;
}
if (validator.errors) {
throw validator.errors;
}
}

validate()
.then(() => console.info("Schema is valid"))
.catch(function (err) {
console.error(err);
exit(1);
});
2 changes: 1 addition & 1 deletion src/hemi.tokenlist.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Hemi Token List",
"timestamp": "2025-01-16T15:25:35.973Z",
"timestamp": "2025-01-22T20:42:04.584Z",
"version": {
"major": 1,
"minor": 5,
Expand Down

0 comments on commit 27d7b89

Please sign in to comment.