Skip to content

Commit

Permalink
lint(track_config): check key_features
Browse files Browse the repository at this point in the history
This commit implements the below rules for a track's `config.json` file.
- The `"key_features"` key is optional
- The `"key_features"` value must be an array with length = 6
- The `"key_features[].title"` key is required
- The `"key_features[].title"` value must be a non-empty, non-blank
  string with length <= 25
- The `"key_features[].content"` key is required
- The `"key_features[].content"` value must be a non-empty, non-blank
  string with length <= 100

These rules are currently disabled until we have a list of valid icons:
- The `"key_features[].icon"` key is required
- The `"key_features[].icon"` value must be a string that matches one of
  the pre-defined icon values

See the spec:
- https://github.com/exercism/docs/blob/main/building/configlet/lint.md#rule-configjson-file-is-valid
  • Loading branch information
ee7 committed Mar 25, 2021
1 parent f9aacb6 commit c9acd22
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/lint/track_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,43 @@ proc hasValidOnlineEditor(data: JsonNode, path: string): bool =
else:
result = false

proc isValidKeyFeature(data: JsonNode, context: string, path: string): bool =
if isObject(data, context, path):
result = true

# TODO: Enable the `icon` checks when we have a list of valid icons.
if false:
const icons = [
"todo",
].toHashSet()

if checkString(data, "icon", path):
let s = data["icon"].getStr()

if s notin icons:
let msg = "The value of `key_features.icon` is `" & s &
"` which is not one of the valid values"
result.setFalseAndPrint(msg, path)

if not checkString(data, "title", path, maxLen = 25):
result = false
if not checkString(data, "content", path, maxLen = 100):
result = false

proc hasValidKeyFeatures(data: JsonNode, path: string): bool =
result = true
if data.hasKey("key_features"):
let d = data["key_features"]
if isArrayOf(d, "key_features", path, isValidKeyFeature,
isRequired = false):
let arrayLen = d.len
if arrayLen != 0 and arrayLen != 6:
let msg = "The `key_features` array has length " & $arrayLen &
", but must have length 6"
result.setFalseAndPrint(msg, path)
else:
result = false

proc isValidTrackConfig(data: JsonNode, path: string): bool =
if isObject(data, "", path):
result = true
Expand All @@ -115,6 +152,8 @@ proc isValidTrackConfig(data: JsonNode, path: string): bool =
result = false
if not hasValidOnlineEditor(data, path):
result = false
if not hasValidKeyFeatures(data, path):
result = false

if not hasArrayOf(data, "tags", path, isValidTag):
result = false
Expand Down

0 comments on commit c9acd22

Please sign in to comment.