Skip to content

Commit

Permalink
add scripts for computing stats of json schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoskal committed Oct 21, 2024
1 parent d48dd96 commit f3a4d43
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions scripts/jsonschema-stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const ignored = [
"$schema",
"$id",
"id",
"$comment",
"title",
"description",
"default",
"examples",
"required",
]

const defs = [
"definitions",
"$defs",
]

let numFields = 0;

function typeStats(obj) {
if (typeof obj === "boolean")
return;

if (typeof obj === "object" && Object.keys(obj).length == 0)
return;

numFields++;

if (Array.isArray(obj)) {
for (const v of obj) {
typeStats(v);
}
return;
}

if (typeof obj !== "object") {
console.log("not object", obj);
return;
}

const obj0 = { ...obj }

const tp = obj.type;

if (!tp) {
if (obj["$ref"] && obj["$ref"].startsWith("#/"))
return; // OK
console.log("no type", obj);
return;
}


if (tp == "object") {
const props = obj.properties || {};
Object.values(props).forEach(typeStats);
delete obj.properties;
}

if (tp == "array") {
typeStats(obj.items);
delete obj.items;
}

if (obj.additionalProperties !== undefined) {
typeStats(obj.additionalProperties);
delete obj.additionalProperties;
}

for (const def of defs) {
if (obj[def]) {
Object.values(obj[def]).forEach(typeStats);
delete obj[def];
}
}

for (const ign of ignored) {
delete obj[ign];
}

if (Object.keys(obj).length > 1) {
console.log("left-over", JSON.stringify(obj));
}
}


const fs = require('fs');

const schema = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));

typeStats(schema);
console.log({ numFields });

0 comments on commit f3a4d43

Please sign in to comment.