Skip to content

Commit

Permalink
Revise, revamp, and increase testing of the configuration schema (#53)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Nov 15, 2024
1 parent e9693f1 commit f65a35c
Show file tree
Hide file tree
Showing 12 changed files with 765 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:

# Dependencies
- run: pipx install clang-format==19.1.0
- uses: sourcemeta/[email protected].1
- uses: sourcemeta/[email protected].2
- name: Install Hurl
run: |
curl --location --remote-name https://github.com/Orange-OpenSource/hurl/releases/download/${{ env.HURL_VERSION }}/hurl_${{ env.HURL_VERSION }}_amd64.deb
Expand Down
6 changes: 3 additions & 3 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ example:
{
"url": "http://localhost:8000",
"port": 8000,
"collections": {
"schemas": {
"example/schemas": {
"base": "https://example.com/schemas",
"path": "./schemas/example/folder"
Expand All @@ -29,8 +29,8 @@ example:

The `url` property defines the base URL of the instance and will be used for
reidentifying schemas at runtime, so make sure its correct. The `port` property
is optional. The `collections` property mounts a given directory of schemas
into a certain relative path.
is optional. The `schemas` property mounts a given directory of schemas into a
certain relative path.

Deployment
----------
Expand Down
80 changes: 64 additions & 16 deletions schemas/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,42 @@
"required": [
"url",
"port",
"collections"
"schemas"
],
"properties": {
"title": {
"type": "string"
"$ref": "#/definitions/text"
},
"description": {
"type": "string"
"$ref": "#/definitions/text"
},
"collections": {
"pages": {
"type": "object",
"minProperties": 1,
"propertyNames": {
"$ref": "#/definitions/entry-path"
},
"additionalProperties": {
"type": "object",
"required": [
"base",
"path"
],
"minProperties": 1,
"properties": {
"title": {
"type": "string"
"$ref": "#/definitions/text"
},
"description": {
"type": "string"
"$ref": "#/definitions/text"
},
"base": {
"email": {
"type": "string",
"format": "url"
"pattern": "^[a-z0-9._+-]+@[a-z0-9.-]+\\.[a-z]{2,}$",
"format": "email"
},
"path": {
"type": "string"
"github": {
"type": "string",
"pattern": "^[a-z0-9-]+$"
},
"website": {
"$ref": "#/definitions/http-url"
}
},
"additionalProperties": false
Expand All @@ -45,10 +50,53 @@
"type": "integer",
"minimum": 0
},
"schemas": {
"type": "object",
"minProperties": 1,
"propertyNames": {
"$ref": "#/definitions/entry-path"
},
"additionalProperties": {
"type": "object",
"required": [
"base",
"path"
],
"properties": {
"base": {
"type": "string",
"pattern": "^\\S.*\\S$",
"format": "uri",
"minLength": 1
},
"path": {
"type": "string",
"pattern": "^\\S.*\\S$",
"minLength": 1
}
},
"additionalProperties": false
}
},
"url": {
"$ref": "#/definitions/http-url"
}
},
"additionalProperties": false,
"definitions": {
"entry-path": {
"type": "string",
"pattern": "^[a-z0-9][a-z0-9-/]*$"
},
"http-url": {
"type": "string",
"pattern": "^https?:\\/\\/[a-z0-9-.:/]+$",
"format": "url"
},
"text": {
"type": "string",
"pattern": "^\\S.*\\S$",
"minLength": 1
}
},
"additionalProperties": false
}
}
28 changes: 12 additions & 16 deletions src/enterprise/enterprise_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ auto generate_toc(const sourcemeta::jsontoolkit::JSON &configuration,
auto entries{sourcemeta::jsontoolkit::JSON::make_array()};

assert(configuration.is_object());
assert(configuration.defines("collections"));
assert(configuration.at("collections").is_object());
assert(configuration.defines("schemas"));
assert(configuration.at("schemas").is_object());

for (const auto &entry : std::filesystem::directory_iterator{directory}) {
auto entry_json{sourcemeta::jsontoolkit::JSON::make_object()};
Expand All @@ -44,37 +44,33 @@ auto generate_toc(const sourcemeta::jsontoolkit::JSON &configuration,
entry.path().string().substr(base.string().size())});

const auto collection_entry_name{entry_relative_path.substr(1)};
if (configuration.at("collections").defines(collection_entry_name) &&
configuration.at("collections")
.at(collection_entry_name)
.is_object() &&
configuration.at("collections")
if (configuration.at("schemas").defines(collection_entry_name) &&
configuration.at("schemas").at(collection_entry_name).is_object() &&
configuration.at("schemas")
.at(collection_entry_name)
.defines("title") &&
configuration.at("collections")
configuration.at("schemas")
.at(collection_entry_name)
.at("title")
.is_string()) {
entry_json.assign("title", sourcemeta::jsontoolkit::JSON{
configuration.at("collections")
configuration.at("schemas")
.at(collection_entry_name)
.at("title")
.to_string()});
}

if (configuration.at("collections").defines(collection_entry_name) &&
configuration.at("collections")
.at(collection_entry_name)
.is_object() &&
configuration.at("collections")
if (configuration.at("schemas").defines(collection_entry_name) &&
configuration.at("schemas").at(collection_entry_name).is_object() &&
configuration.at("schemas")
.at(collection_entry_name)
.defines("description") &&
configuration.at("collections")
configuration.at("schemas")
.at(collection_entry_name)
.at("description")
.is_string()) {
entry_json.assign("description", sourcemeta::jsontoolkit::JSON{
configuration.at("collections")
configuration.at("schemas")
.at(collection_entry_name)
.at("description")
.to_string()});
Expand Down
9 changes: 4 additions & 5 deletions src/index/index.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ static auto index(const sourcemeta::jsontoolkit::JSON &configuration,
assert(std::filesystem::exists(base));
assert(std::filesystem::exists(output));
assert(configuration.is_object());
assert(configuration.defines("collections"));
assert(configuration.at("collections").is_object());
assert(configuration.defines("schemas"));
assert(configuration.at("schemas").is_object());

for (const auto &[name, options] :
configuration.at("collections").as_object()) {
for (const auto &[name, options] : configuration.at("schemas").as_object()) {
assert(options.is_object());
assert(options.defines("path"));
assert(options.at("path").is_string());
Expand Down Expand Up @@ -162,7 +161,7 @@ static auto index_main(const std::string_view &program,

// Save the configuration file too
auto configuration_copy = configuration;
configuration_copy.erase("collections");
configuration_copy.erase("schemas");

// TODO: Perform these with a Blaze helper function that applies schema
// "default"s to an instance
Expand Down
2 changes: 1 addition & 1 deletion test/cli/ce/index/no-output.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cat << EOF > "$TMP/configuration.json"
{
"url": "http://localhost:8000",
"port": 8000,
"collections": {
"schemas": {
"example/schemas": {
"base": "https://example.com/schemas",
"path": "./schemas/example/folder"
Expand Down
4 changes: 2 additions & 2 deletions test/cli/common/index/invalid-configuration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trap clean EXIT
cat << EOF > "$TMP/configuration.json"
{
"port": 8000,
"collections": {
"schemas": {
"example/schemas": {}
}
}
Expand All @@ -23,7 +23,7 @@ cat << EOF > "$TMP/expected.txt"
-- Using configuration: $(realpath "$TMP")/configuration.json
-- Writing output to: $(realpath "$TMP")/output
error: Invalid configuration
The object value was expected to define properties "url", "port", and "collections" but did not define the property "url"
The object value was expected to define properties "url", "port", and "schemas" but did not define the property "url"
at instance location ""
at evaluate path "/required"
EOF
Expand Down
2 changes: 1 addition & 1 deletion test/cli/ee/index/no-output.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cat << EOF > "$TMP/configuration.json"
{
"url": "http://localhost:8000",
"port": 8000,
"collections": {
"schemas": {
"example/schemas": {
"base": "https://example.com/schemas",
"path": "./schemas/example/folder"
Expand Down
12 changes: 8 additions & 4 deletions test/sandbox/configuration.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"url": "http://localhost:8000",
"port": 8000,
"collections": {
"pages": {
"doc": {
"title": "A sample schema folder",
"description": "For testing purposes"
}
},
"schemas": {
"example/extension": {
"base": "https://example.com/extension",
"path": "./schemas/example/extension"
Expand All @@ -10,7 +16,7 @@
"base": "https://example.com/schemas",
"path": "./schemas/example/folder"
},
"EXAMPLE/v2.0": {
"example/v2.0": {
"base": "https://example.com/schemas",
"path": "./schemas/example/v2.0"
},
Expand All @@ -19,8 +25,6 @@
"path": "./schemas/example/bundling"
},
"doc": {
"title": "A sample schema folder",
"description": "For testing purposes",
"base": "https://example.com/doc",
"path": "./schemas/doc"
}
Expand Down
17 changes: 0 additions & 17 deletions test/schemas/configuration.test.json

This file was deleted.

Loading

0 comments on commit f65a35c

Please sign in to comment.