Skip to content

Commit

Permalink
lint(track_config): check key_features
Browse files Browse the repository at this point in the history
  • Loading branch information
ee7 committed Mar 24, 2021
1 parent 3d19f64 commit 323ebee
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
34 changes: 34 additions & 0 deletions src/lint/track_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,29 @@ proc hasValidOnlineEditor(data: JsonNode, path: string): bool =
else:
result = false

proc isValidKeyFeature(data: JsonNode, context: string, path: string): bool =
const icons = [
"todo",
].toHashSet()

if isObject(data, context, path):
result = true

if checkString(data, "icon", path):
let s = data["icon"].getStr()
if false and s notin icons: # TODO
let msg = "The value of `key_features.icon` is `" & s &
"` which is not one of the valid values"
result.setFalseAndPrint(msg, path)
else:
result = false

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

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

proc isValidTrackConfig(data: JsonNode, path: string): bool =
if isObject(data, "", path):
result = true
Expand Down Expand Up @@ -116,6 +139,17 @@ proc isValidTrackConfig(data: JsonNode, path: string): bool =
if not hasValidOnlineEditor(data, path):
result = false

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

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

Expand Down
16 changes: 13 additions & 3 deletions src/lint/validators.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,24 @@ proc hasObject*(data: JsonNode; key, path: string; isRequired = true): bool =
elif isRequired:
result.setFalseAndPrint("Missing key: " & q(key), path)

proc checkString*(data: JsonNode; key, path: string; isRequired = true): bool =
proc checkString*(data: JsonNode; key, path: string; isRequired = true,
maxLen = int.high): bool =
result = true
if data.hasKey(key):
case data[key].kind
of JString:
let s = data[key].getStr()
if s.len > 0:
if s.strip().len == 0:
let sLen = s.len
if sLen > 0:
if s.strip().len > 0:
if sLen > maxLen:
const truncLen = 25
let sTrunc = if sLen > truncLen: s[0..truncLen] else: s
let msg = "The value of `" & key & "` that starts with `" & sTrunc &
"...` is " & $sLen & " characters, but must not exceed " &
$maxLen & " characters"
result.setFalseAndPrint(msg, path)
else:
result.setFalseAndPrint("String is whitespace-only: " & q(key), path)
else:
result.setFalseAndPrint("String is zero-length: " & q(key), path)
Expand Down

0 comments on commit 323ebee

Please sign in to comment.