Skip to content

Instantly share code, notes, and snippets.

@Checksum
Last active December 29, 2023 17:09
Show Gist options
  • Select an option

  • Save Checksum/72d927471c76c76c46418b3ee88e0a7c to your computer and use it in GitHub Desktop.

Select an option

Save Checksum/72d927471c76c76c46418b3ee88e0a7c to your computer and use it in GitHub Desktop.

Revisions

  1. Checksum revised this gist Jul 17, 2022. 2 changed files with 5 additions and 5 deletions.
    6 changes: 3 additions & 3 deletions assert.jq
    Original 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(expr; msg; level):
    def assert(level; expr; msg):
    if expr then
    .
    else
    . |= . + [{ level: level, message: msg }]
    end;

    def assert(expr; msg):
    assert(expr; msg; "error");
    assert("error"; expr; msg);

    def print_assertion_output:
    def validate:
    . as $errors
    | ($errors | map("\(.level | ascii_upcase): \(.message)") | join("\n")) as $output
    | if $errors | map(select(.level == "error")) | length > 0 then
    4 changes: 2 additions & 2 deletions usage.jq
    Original 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($response.authors.length > 0; "No authors in response"; "warn")
    | assert("warn"; $response.authors.length > 0; "No authors in response")
    # Output
    | print_assertion_output;
    | validate;
  2. Checksum revised this gist Sep 7, 2020. No changes.
  3. Checksum revised this gist Sep 7, 2020. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions usage.jq
    Original 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;
  4. Checksum created this gist Sep 7, 2020.
    23 changes: 23 additions & 0 deletions assert.jq
    Original 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;