aboutsummaryrefslogtreecommitdiff
path: root/src/main/input-output/input/json-parsing/rules_parser.cpp
diff options
context:
space:
mode:
authorCharlie Blake2018-04-04 00:03:37 +0100
committerGitHub2018-04-04 00:03:37 +0100
commitbb243681fa6f9d05e4e19757431310f5cbb61831 (patch)
tree1ecfdbe1883df6325007e21af84ffed92d53888a /src/main/input-output/input/json-parsing/rules_parser.cpp
parentMerge pull request #46 from thecharlesblake/zero-seed-bugfix (diff)
parentRe-add schemas for rule and deal parsing (diff)
downloadmetasol-bb243681fa6f9d05e4e19757431310f5cbb61831.tar.gz
metasol-bb243681fa6f9d05e4e19757431310f5cbb61831.zip

Merge pull request #47 from thecharlesblake/re-add-schema

Re-add schemas for rule and deal parsing

Diffstat (limited to 'src/main/input-output/input/json-parsing/rules_parser.cpp')
-rw-r--r--src/main/input-output/input/json-parsing/rules_parser.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/main/input-output/input/json-parsing/rules_parser.cpp b/src/main/input-output/input/json-parsing/rules_parser.cpp
index 6fa89bc..800f90e 100644
--- a/src/main/input-output/input/json-parsing/rules_parser.cpp
+++ b/src/main/input-output/input/json-parsing/rules_parser.cpp
@@ -56,6 +56,12 @@ sol_rules rules_parser::get_default() {
}
void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
+ // There are two stages to reading in a rules document. It is both put
+ // through the rule schema validator, and also parsed into
+ // the game rules object (with some further checking done). The parsing is
+ // done first because the errors it detects tend to give clearer error
+ // messages than the schema validator
+
if (!d.IsObject()) {
json_helper::json_parse_err("JSON doc must be object");
}
@@ -265,4 +271,52 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
json_helper::json_parse_err("one and only one of [hole] and [foundations] "
"must be true");
}
+
+ apply_rules_schema(d); // Throws an error if it fails
+}
+
+void rules_parser::apply_rules_schema(const rapidjson::Document &doc) {
+ const char* schema_json = R"(
+{
+ "$schema": "http://json-schema.org/draft-04/schema#",
+ "description": "JSON Schema representing a generic solitaire game",
+
+ "type": "object", "properties": {
+
+ "tableau piles": {
+ "type": "object", "properties": {
+ "count": {"type": "integer", "minimum": 0},
+ "build policy": {"type": "string", "enum": ["any-suit", "red-black", "same-suit", "no-build"]},
+ "spaces policy": {"type": "string", "enum": ["any", "no-build"]},
+ "diagonal deal": {"type": "boolean"},
+ "move built group": {"type": "boolean"},
+ "move built group policy": {"type": "string", "enum": ["same-as-build", "any-suit", "red-black", "same-suit", "no-build"]}
+ }, "additionalProperties": false
+ },
+ "max rank": {"type": "integer", "minimum": 1, "maximum": 13},
+ "two decks": {"type": "boolean"},
+ "hole": {"type": "boolean"},
+ "foundations": {"type": "boolean"},
+ "foundations initial card": {"type": "boolean"},
+ "foundations removable": {"type": "boolean"},
+ "foundations complete piles": {"type": "boolean"},
+ "cells": {"type": "integer", "minimum": 0},
+ "stock size": {"type": "integer", "minimum": 0},
+ "stock deal type": {"type": "string", "enum": ["waste", "tableau piles"]},
+ "reserve size": {"type": "integer", "minimum": 0},
+ "reserve stacked": {"type": "boolean"}
+
+ }, "additionalProperties": false
+}
+)";
+
+ Document d;
+ d.Parse(schema_json);
+ assert(!d.HasParseError());
+ SchemaDocument schema(d);
+ SchemaValidator validator(schema);
+
+ if(!doc.Accept(validator)) {
+ throw runtime_error(json_helper::schema_err_str(validator));
+ }
}