-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Fully define state baseline schema
- Loading branch information
1 parent
0b5974c
commit 91ef12e
Showing
45 changed files
with
3,452 additions
and
73 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
3 changes: 2 additions & 1 deletion
3
...kitten-scientists/test.engine-state.jsonc → ...en-scientists/snapshot.engine-state.jsonc
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,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); |
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,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" | ||
} |
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,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" | ||
} |
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,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" | ||
} |
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,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" | ||
} |
Oops, something went wrong.