Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partially fix spec validation #255

Open
wants to merge 7 commits into
base: v0.8.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ on:

jobs:
validate_all:

runs-on: ubuntu-latest

strategy:
Expand All @@ -28,3 +27,4 @@ jobs:
cache: 'npm'
- run: npm ci
- run: npm run validate_all
- run: npm run check_component_uniqueness
10 changes: 2 additions & 8 deletions api/starknet_write_api.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
"$ref": "#/components/errors/NON_ACCOUNT"
},
{
"$ref": "#/components/errors/CLASS_HASH_NOT_FOUND"
"$ref": "./api/starknet_api_openrpc.json#/components/errors/CLASS_HASH_NOT_FOUND"
},
{
"$ref": "#/components/errors/DUPLICATE_TX"
Expand All @@ -201,9 +201,7 @@
"contentDescriptors": {},
"schemas": {
"NUM_AS_HEX": {
"title": "An integer number in hex format (0x...)",
"type": "string",
"pattern": "^0x[a-fA-F0-9]+$"
"$ref": "./api/starknet_api_openrpc.json#/components/schemas/NUM_AS_HEX"
},
"SIGNATURE": {
"$ref": "./api/starknet_api_openrpc.json#/components/schemas/SIGNATURE"
Expand All @@ -228,10 +226,6 @@
}
},
"errors": {
"CLASS_HASH_NOT_FOUND": {
"code": 28,
"message": "Class hash not found"
},
"CLASS_ALREADY_DECLARED": {
"code": 51,
"message": "Class already declared"
Expand Down
67 changes: 67 additions & 0 deletions check_component_uniqueness.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env node

const fs = require("fs");
const path = require("path");

/**
* Updates one object with contents of the other.
* Object expected to have nesting: { components: { schemas: { ... }, errors: { ... } } }
* @param {*} globalComponents the object to be updated
* @param {*} newComponents the object to be added to `globalComponents
* @returns Number of encountered duplicates.
*/
function addComponents(globalComponents, newComponents, newComponentsOrigin) {
let duplicates = 0;
for (const componentType in newComponents) {
if (!(componentType in globalComponents)) {
globalComponents[componentType] = {};
for (const componentName in newComponents[componentType]) {
globalComponents[componentType][componentName] = newComponentsOrigin;
}
continue;
}

for (const componentName in newComponents[componentType]) {
const newComponent = newComponents[componentType][componentName];
const isReference = "$ref" in newComponent;
if (componentName in globalComponents[componentType] && !isReference) {
const previousLocation = globalComponents[componentType][componentName];
duplicates++;
console.error(
`Duplicate entry in ${componentType}: ${componentName} defined in ${previousLocation} and ${newComponentsOrigin}`
);
} else if (!isReference) {
globalComponents[componentType][componentName] = newComponentsOrigin;
}
}
}

return duplicates;
}

function main() {
const globalComponents = {};
const specDir = "api";
let duplicates = 0;

console.error(
"Temporarily ignoring starknet_metadata.json in uniqueness check. Perhaps remove it or use it better."
);
for (const fileName of fs.readdirSync(specDir)) {
if (fileName.endsWith(".json") && fileName !== "starknet_metadata.json") {
const filePath = path.join(specDir, fileName);
const rawSpec = fs.readFileSync(filePath);
const specContent = JSON.parse(rawSpec);
const newComponents = specContent["components"];
duplicates += addComponents(globalComponents, newComponents, filePath);
}
}

if (duplicates > 0) {
process.exit(duplicates);
} else {
console.log("No duplicate component definitions!");
}
}

main();
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"url": "https://github.com/starkware-libs/starknet-specs.git"
},
"scripts": {
"validate_all": "node validate.js api/starknet_api_openrpc.json && node validate.js api/starknet_write_api.json && node validate.js api/starknet_trace_api_openrpc.json && node validate.js ./api/starknet_ws_api.json && node ./api/starknet_executables.json && node validate.js ./wallet-api/wallet_rpc.json"
"validate_all": "find api/ -name '*.json' -not -name 'starknet_metadata.json' | xargs -n1 node validate.js",
"check_component_uniqueness": "node check_component_uniqueness.js"
},
"author": "Lior Schejter",
"license": "MIT",
Expand All @@ -16,4 +17,4 @@
"@open-rpc/schema-utils-js": "^2.0.3",
"fs-extra": "10.1.0"
}
}
}
16 changes: 10 additions & 6 deletions validate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

const { parseOpenRPCDocument, validateOpenRPCDocument, dereferenceDocument } = require("@open-rpc/schema-utils-js");
const { parseOpenRPCDocument, validateOpenRPCDocument } = require("@open-rpc/schema-utils-js");
const Dereferencer = require("@json-schema-tools/dereferencer");
const fs = require("fs-extra");

Expand All @@ -12,16 +12,20 @@ async function runValidation(filename) {

let doc = await parseOpenRPCDocument(dereffedDoc, { dereference: true });

const errors = validateOpenRPCDocument(doc);
if (errors === true) {
console.log("Ok!");
} else {
const validation = validateOpenRPCDocument(doc);
if (validation !== true) {
const errors = validation;
console.error(errors.name);
console.error(errors.message);
process.exit(1);
}
} catch (exn) {
console.error(exn && exn.message);
console.error("Error in", filename);
console.error("\t", exn && exn.message);
process.exit(2);
}

console.log("Ok!");
}

/**
Expand Down