forked from ethereum-lists/chains
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check and validate JSON schema (ethereum-lists#1509)
* Validate json * wip: gh workflow test * wip: added workflow for JSON validation * Schema Improved. * Improved Workflow and Schema check for JSON files * Fixed JSON Schema * Fixed typo * Removed auto generated file * updated required fields in chainSchema * updated chain schema * removed `network` from README.md example * improved schemaCheck script * Matching ChainID with file name schema.
- Loading branch information
1 parent
17b77be
commit 7544a67
Showing
6 changed files
with
298 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: Check JSON Schema | ||
|
||
on: | ||
push: | ||
pull_request: | ||
|
||
jobs: | ||
validate_json: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Check out repo | ||
uses: actions/checkout@v2 | ||
- uses: actions/cache@v2 | ||
name: Configure npm caching | ||
with: | ||
path: ~/.npm | ||
key: ${{ runner.os }}-npm-${{ hashFiles('**/workflows/validate_json.yml') }} | ||
restore-keys: | | ||
${{ runner.os }}-npm- | ||
- name: Run JSON Validation | ||
working-directory: ./tools | ||
run: |- | ||
npm install | ||
node schemaCheck.js |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"devDependencies": { | ||
"ajv": "^8.11.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema#", | ||
"title": "EIP155 Chain Data", | ||
"type":"object", | ||
"required": ["name","shortName","chain","chainId","networkId","rpc","faucets","infoURL","nativeCurrency"], | ||
"properties": { | ||
"name":{ | ||
"type":"string", | ||
"description": "Name of the Network" | ||
}, | ||
"shortName":{ | ||
"type":"string" | ||
}, | ||
"title":{ | ||
"type":"string", | ||
"description": "Optional title for the Network" | ||
}, | ||
"chain":{ | ||
"type":"string", | ||
"description": "Name of the Network" | ||
}, | ||
"icon":{ | ||
"type":"string", | ||
"description": "Icon type" | ||
}, | ||
"rpc":{ | ||
"type":"array", | ||
"items":{ | ||
"type":"string" | ||
} | ||
}, | ||
"faucets":{ | ||
"type":"array", | ||
"items":{ | ||
"type":"string" | ||
} | ||
}, | ||
"nativeCurrency":{ | ||
"type":"object", | ||
"properties": { | ||
"name":{ | ||
"type":"string", | ||
"description":"Name of the Native Currency" | ||
}, | ||
"symbol":{ | ||
"type":"string", | ||
"description":"Symbol of the Native Currency" | ||
}, | ||
"decimals":{ | ||
"type":"number", | ||
"description":"Decimal points supported" | ||
} | ||
} | ||
}, | ||
"infoURL":{ | ||
"type":"string", | ||
"description": "infoURL" | ||
}, | ||
"chainId":{ | ||
"type":"number", | ||
"description": "Chain ID of the Network" | ||
}, | ||
"networkId":{ | ||
"type":"number", | ||
"description": "Network ID of the Network" | ||
}, | ||
"slip44":{ | ||
"type":"number", | ||
"description": "Slip44 of the Network" | ||
}, | ||
"ens":{ | ||
"type":"object", | ||
"properties": { | ||
"registry":{ | ||
"type":"string" | ||
} | ||
} | ||
}, | ||
"explorers":{ | ||
"type":"array", | ||
"items":{ | ||
"type":"object", | ||
"properties": { | ||
"name":{ | ||
"type":"string" | ||
}, | ||
"url":{ | ||
"type":"string" | ||
}, | ||
"standard":{ | ||
"type":"string" | ||
} | ||
} | ||
} | ||
}, | ||
"parent":{ | ||
"type":"object", | ||
"properties": { | ||
"type":{ | ||
"type":"string" | ||
}, | ||
"chain":{ | ||
"type":"string" | ||
}, | ||
"bridges":{ | ||
"type":"array", | ||
"items": { | ||
"type":"object", | ||
"properties":{ | ||
"url": { | ||
"type":"string" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"status":{ | ||
"type":"string" | ||
} | ||
}, | ||
"additionalProperties": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const fs = require('fs'); | ||
const Ajv = require("ajv") | ||
const ajv = new Ajv() | ||
const schema = require('./schema/chainSchema.json') | ||
const chainFiles = fs.readdirSync('../_data/chains/'); | ||
|
||
const filesWithErrors = [] | ||
for(const chainFile of chainFiles){ | ||
const fileLocation = `../_data/chains/${chainFile}` | ||
const fileData = fs.readFileSync(fileLocation,'utf8') | ||
const fileDataJson = JSON.parse(fileData) | ||
const chainIdFromFileName = chainFile.match(/eip155-(\d+)\.json/)[1] | ||
if(chainIdFromFileName != fileDataJson.chainId){ | ||
throw new Error(`File Name does not match with ChainID in ${chainFile}`) | ||
} | ||
const valid = ajv.validate(schema, fileDataJson) | ||
if(!valid) { | ||
console.error(ajv.errors) | ||
filesWithErrors.push(chainFile) | ||
} | ||
} | ||
|
||
if(filesWithErrors.length > 0){ | ||
throw new Error(`Invalid JSON Schema in ${filesWithErrors.length} files at ${filesWithErrors.join(",")}`) | ||
} |