Skip to content

Commit

Permalink
feat(core): Fully define state baseline schema
Browse files Browse the repository at this point in the history
  • Loading branch information
oliversalzburg committed Nov 20, 2023
1 parent 0b5974c commit 91ef12e
Show file tree
Hide file tree
Showing 45 changed files with 3,452 additions and 73 deletions.
1 change: 1 addition & 0 deletions packages/documentation/docs/sections/time-control.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Allows you to configure when KS should automatically combust TCs to skip years.

For a skip to be triggered, all of these have to be true:

1. We have **Trigger** amount of TCs available.
1. We are within an enabled **Cycle**.
1. We are within an enabled **Season**.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
//"$schema": "https://github.com/kitten-science/kitten-scientists/schemas/draft-01/engine-state.schema.json",
// Exported snapshots should likely always be baselines, as they contain everything.
"$schema": "https://github.com/kitten-science/kitten-scientists/raw/main/schemas/draft-01/baseline/engine-state.schema.json",
"v": "2.0.0-beta.8-live",
"engine": {
"enabled": true,
Expand Down
121 changes: 121 additions & 0 deletions schemas/.scripts/check-baseline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/usr/bin/env node

/**
* This script ensures that a JSON schema draft sufficiently describes
* a KS engine state "baseline". Meaning that it defines every single
* setting possible.
*/

const { readFileSync } = require("node:fs");
const { argv } = require("node:process");

const assertions = new Map();

const internalAssert = (value, file, message) => {
if (!value) {
if (!assertions.has(file)) {
assertions.set(file, []);
}
assertions.get(file).push(message);
}
};

const checkSchemaProperties = (filename, schema, path) => {
if (!schema.properties) {
return;
}

for (const [property, schemaProperty] of Object.entries(schema.properties)) {
if (Array.isArray(schema.required)) {
internalAssert(
schema.required.includes(property),
filename,
`${path}.properties.${property} is not required`,
);
}

if ("$ref" in schemaProperty) {
continue;
}

internalAssert(
typeof schemaProperty.type === "string" || Array.isArray(schemaProperty.enum),
filename,
`${path}.properties.${property} has no type`,
);

internalAssert(
typeof schemaProperty.description === "string",
filename,
`${path}.properties.${property} has no description`,
);

if (property === "trigger") {
internalAssert(
typeof schemaProperty.minimum === "number",
filename,
`${path}.properties.${property} does not set a minimum`,
);

internalAssert(
typeof schemaProperty.minimum === "number",
filename,
`${path}.properties.${property} does not set a maximum`,
);
}

if (schemaProperty.type === "object") {
internalAssert(
typeof schemaProperty.properties === "object",
filename,
`${path}.properties.${property} has no properties`,
);

internalAssert(
typeof schemaProperty.additionalProperties === "boolean" &&
schemaProperty.additionalProperties === false,
filename,
`${path}.properties.${property} allows additionalProperties`,
);

internalAssert(
Array.isArray(schemaProperty.required),
filename,
`${path}.properties.${property} does not declare required properties`,
);

checkSchemaProperties(filename, schemaProperty, `${path}.properties.${property}`);
}
}
};

const checkSchemaRoot = (filename, schema) => {
internalAssert(
schema.$schema === "http://json-schema.org/draft-07/schema#",
"Root schema does not match 'http://json-schema.org/draft-07/schema#'.",
);
internalAssert(typeof schema.$id === "string", "Root schema doesn't set a valid $id");
internalAssert(
schema.$id.endsWith(".schema.json"),
"Root schema $id doesn't end with '.schema.json'",
);
checkSchemaProperties(filename, schema, "root");
};

const checkFile = filename => {
const fileContent = readFileSync(filename, { encoding: "utf-8" });
const fileBody = JSON.parse(fileContent);
return checkSchemaRoot(filename, fileBody);
};

const targets = argv.slice(2);

for (const target of targets) {
try {
checkFile(target);
} catch (error) {
console.error(target, error);
}
}

console.dir(assertions);
19 changes: 19 additions & 0 deletions schemas/draft-01/baseline/building-bonfire.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "/schemas/draft-01/baseline/building-bonfire.schema.json",
"title": "Bonfire Building",
"description": "Kitten Scientists Bonfire Building",
"additionalProperties": false,
"properties": {
"enabled": {
"description": "Should we build this building?",
"type": "boolean"
},
"max": {
"description": "The limit for how many times to build this.\n-1 means unlimited",
"type": "number"
}
},
"required": ["enabled", "max"],
"type": "object"
}
19 changes: 19 additions & 0 deletions schemas/draft-01/baseline/building-religion.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "/schemas/draft-01/baseline/building-religion.schema.json",
"title": "Religion Building",
"description": "Kitten Scientists Religion Building",
"additionalProperties": false,
"properties": {
"enabled": {
"description": "Should we build this building?",
"type": "boolean"
},
"max": {
"description": "The limit for how many times to build this.\n-1 means unlimited",
"type": "number"
}
},
"required": ["enabled", "max"],
"type": "object"
}
19 changes: 19 additions & 0 deletions schemas/draft-01/baseline/building-space.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "/schemas/draft-01/baseline/building-space.schema.json",
"title": "Space Building",
"description": "Kitten Scientists Space Building",
"additionalProperties": false,
"properties": {
"enabled": {
"description": "Should we build this building?",
"type": "boolean"
},
"max": {
"description": "The limit for how many times to build this.\n-1 means unlimited",
"type": "number"
}
},
"required": ["enabled", "max"],
"type": "object"
}
19 changes: 19 additions & 0 deletions schemas/draft-01/baseline/building-time.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "/schemas/draft-01/baseline/building-time.schema.json",
"title": "Time Building",
"description": "Kitten Scientists Time Building",
"additionalProperties": false,
"properties": {
"enabled": {
"description": "Should we build this building?",
"type": "boolean"
},
"max": {
"description": "The limit for how many times to build this.\n-1 means unlimited",
"type": "number"
}
},
"required": ["enabled", "max"],
"type": "object"
}
Loading

0 comments on commit 91ef12e

Please sign in to comment.