Skip to content

Instantly share code, notes, and snippets.

@snmaddula
Created May 21, 2025 00:35
Show Gist options
  • Save snmaddula/894cf9db0d441229438837034aef7c7f to your computer and use it in GitHub Desktop.
Save snmaddula/894cf9db0d441229438837034aef7c7f to your computer and use it in GitHub Desktop.

Absolutely. To simplify the JQ validation for testing purposes — while still exercising the validation engine and ValidationResult logic — we can write a basic expression that:

  1. Produces an error if a required field (e.g., bmaController) is missing or blank.
  2. Produces a warning if a certain condition is met (e.g., vendor is "DELL").

Here’s a minimal JQ validation snippet that can be easily tested and reliably produces validation output:

✅ Simplified JQ Validation Expression

{
  "debug": $input,
  "errors": {
    "bmaController": [
      (if ($input.bmaController | tostring | length) == 0 then "bmaController is required" else null end)
    ]
  },
  "warnings": {
    "vendor": [
      (if ($input.vendor | ascii_downcase) == "dell" then "Dell machines may need firmware updates" else null end)
    ]
  }
} | with_entries(select(.value | type == "object") | .value |= with_entries(select(.value != null)))

💡 Explanation

  • "debug": $input: Echoes back the input for debugging.

  • Errors

    • Triggers if bmaController is missing or empty.
  • Warnings

    • Triggers if vendor is "dell" (case-insensitive).
  • with_entries(...): Removes null entries from both errors and warnings.


🧪 Sample Input for Testing

{
  "bmaController": "",
  "vendor": "dell",
  "serialNo": "1234"
}

✅ Expected Output

{
  "debug": {
    "bmaController": "",
    "vendor": "dell",
    "serialNo": "1234"
  },
  "errors": {
    "bmaController": ["bmaController is required"]
  },
  "warnings": {
    "vendor": ["Dell machines may need firmware updates"]
  }
}

📄 Usage in Manifest

validation:
  - type: JQ
    name: simplified validation for testing
    expr: |
      {
        "debug": $input,
        "errors": {
          "bmaController": [
            (if ($input.bmaController | tostring | length) == 0 then "bmaController is required" else null end)
          ]
        },
        "warnings": {
          "vendor": [
            (if ($input.vendor | ascii_downcase) == "dell" then "Dell machines may need firmware updates" else null end)
          ]
        }
      }
      | with_entries(select(.value | type == "object") | .value |= with_entries(select(.value != null)))

Let me know if you’d like a version that incorporates lookup response logic or produces multiple errors/warnings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment