Last active
December 29, 2023 17:09
-
-
Save Checksum/72d927471c76c76c46418b3ee88e0a7c to your computer and use it in GitHub Desktop.
Revisions
-
Checksum revised this gist
Jul 17, 2022 . 2 changed files with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,17 +1,17 @@ # A simple assertion library for jq (https://github.com/stedolan/jq) # Author: Srinath Sankar def assert(level; expr; msg): if expr then . else . |= . + [{ level: level, message: msg }] end; def assert(expr; msg): assert("error"; expr; msg); def validate: . as $errors | ($errors | map("\(.level | ascii_upcase): \(.message)") | join("\n")) as $output | if $errors | map(select(.level == "error")) | length > 0 then This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,6 +8,6 @@ def check_authors: # Assertions | assert($response | type == "object"; "Response is not an object") | assert($response.authors | type == "array"; "Authors not an array") | assert("warn"; $response.authors.length > 0; "No authors in response") # Output | validate; -
Checksum revised this gist
Sep 7, 2020 . No changes.There are no files selected for viewing
-
Checksum revised this gist
Sep 7, 2020 . 1 changed file with 13 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ # Usage # echo '{"authors": [ { "name": "Srinath", "id": "Checksum" } ]}' | jq -L assert.jq -M 'include "assert"; check_authors' def check_authors: # Declaration . as $response # Empty array to collect responses | [] # Assertions | assert($response | type == "object"; "Response is not an object") | assert($response.authors | type == "array"; "Authors not an array") | assert($response.authors.length > 0; "No authors in response"; "warn") # Output | print_assertion_output; -
Checksum created this gist
Sep 7, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ # A simple assertion library for jq (https://github.com/stedolan/jq) # Author: Srinath Sankar def assert(expr; msg; level): if expr then . else . |= . + [{ level: level, message: msg }] end; def assert(expr; msg): assert(expr; msg; "error"); def print_assertion_output: . as $errors | ($errors | map("\(.level | ascii_upcase): \(.message)") | join("\n")) as $output | if $errors | map(select(.level == "error")) | length > 0 then error("\n" + $output) elif $errors | length > 0 then $output else true end;