diff --git a/Taskfile.yml b/Taskfile.yml index 4fadb79..acc2368 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -25,3 +25,7 @@ tasks: -c ajv-keywords \ -s schemas-yaml/character.yml \ -d {{.CLI_ARGS}} + test: + deps: [ schemas ] + cmds: + - vitest run diff --git a/package.json b/package.json index 639e1e3..85d65d1 100644 --- a/package.json +++ b/package.json @@ -15,9 +15,11 @@ "author": "Yanick Champoux (http://techblog.babyl.ca/)", "license": "ISC", "devDependencies": { - "prettier": "^2.6.2" + "prettier": "^2.6.2", + "vitest": "^0.10.0" }, "dependencies": { + "ajv": "^8.11.0", "ajv-cli": "^5.0.0", "ajv-keywords": "^5.1.0" } diff --git a/schemas-json/character.json b/schemas-json/character.json new file mode 100644 index 0000000..f97c88f --- /dev/null +++ b/schemas-json/character.json @@ -0,0 +1,50 @@ +{ + "$defs" : { + "statistic" : { + "maximum" : 20, + "minimum" : 1, + "type" : "number" + } + }, + "$id" : "https://hyperboria.babyl.ca/character.json", + "additionalProperties" : false, + "properties" : { + "name" : { + "type" : "string" + }, + "player" : { + "type" : "string" + }, + "statistics" : { + "allRequired" : true, + "properties" : { + "charisma" : { + "$ref" : "#/$defs/statistic" + }, + "constitution" : { + "$ref" : "#/$defs/statistic" + }, + "dexterity" : { + "$ref" : "#/$defs/statistic" + }, + "intelligence" : { + "$ref" : "#/$defs/statistic" + }, + "strength" : { + "$ref" : "#/$defs/statistic" + }, + "wisdom" : { + "$ref" : "#/$defs/statistic" + } + }, + "type" : "object" + } + }, + "required" : [ + "name", + "player", + "statistics" + ], + "title" : "Hyperboria character sheet", + "type" : "object" +} diff --git a/src/statistics.test.js b/src/statistics.test.js new file mode 100644 index 0000000..42efb1f --- /dev/null +++ b/src/statistics.test.js @@ -0,0 +1,20 @@ +import { test, expect } from "vitest"; + +import Ajv from "ajv"; + +import characterSchema from "../schemas-json/character.json"; + +const ajv = new Ajv(); +const validate = ajv.compile(characterSchema.$defs.statistic); + +test("good statistic", () => { + expect(validate(12)).toBeTruthy(); + expect(validate.errors).toBeNull(); +}); + +test("bad statistic", () => { + expect(validate(21)).toBeFalsy(); + expect(validate.errors[0]).toMatchObject({ + message: "must be <= 20", + }); +});