Skip to content

Commit

Permalink
lint: refactor as isObject and hasObject
Browse files Browse the repository at this point in the history
  • Loading branch information
ee7 committed Feb 4, 2021
1 parent f06c578 commit 7e6f2ed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/lint/concept_exercises.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import std/[json, os, terminal]
import ".."/helpers
import "."/validators

proc isValidAuthorOrContributor(data: JsonNode, key: string, path: string): bool =
if isObject(data, "", path):
proc isValidAuthorOrContributor(data: JsonNode, context: string, path: string): bool =
if isObject(data, context, path):
result = true
checkString("github_username")
checkString("exercism_username", isRequired = false)

template checkFiles(data: JsonNode, context, path: string) =
if isObject(data, context, path):
if hasObject(data, context, path):
checkArrayOfStrings(context, "solution")
checkArrayOfStrings(context, "test")
checkArrayOfStrings(context, "exemplar")
else:
result = false

proc isValidConceptExerciseConfig(data: JsonNode, path: string): bool =
if isObject(data, "", path):
if isObject(data, "root", path):
result = true
checkArrayOf("authors", isValidAuthorOrContributor)
checkArrayOf("contributors", isValidAuthorOrContributor, isRequired = false)
Expand Down
12 changes: 7 additions & 5 deletions src/lint/validators.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ export strutils.strip
proc q(s: string): string =
"'" & s & "'"

proc isObject*(data: JsonNode, key: string, path: string,
proc isObject*(data: JsonNode; context, path: string): bool =
result = true
if data.kind != JObject:
writeError("Not an object: " & q(context), path)

proc hasObject*(data: JsonNode; key, path: string,
isRequired = true): bool =
result = true
if key.len == 0:
if data.kind != JObject:
writeError("JSON root is not an object", path)
elif data.hasKey(key):
if data.hasKey(key):
if data[key].kind != JObject:
writeError("Not an object: " & q(key), path)
elif isRequired:
Expand Down

0 comments on commit 7e6f2ed

Please sign in to comment.