Skip to content

Add --validate-schema to check a response against a JSON Schema#274

Open
jongio wants to merge 1 commit into
mainfrom
idea/validate-schema
Open

Add --validate-schema to check a response against a JSON Schema#274
jongio wants to merge 1 commit into
mainfrom
idea/validate-schema

Conversation

@jongio

@jongio jongio commented Jul 12, 2026

Copy link
Copy Markdown
Owner

What

Adds a --validate-schema <file> flag that validates the JSON response against a JSON Schema and exits non-zero when it does not conform. Instead of piping a response into an external validator like ajv, you can check an Azure response against a contract inline.

Behavior

  • Conforming response: exits 0.
  • Non-conforming response: prints each validation error to stderr (one per line, instanceLocation: message) and exits 1. The response body is still printed first.
  • Missing or unreadable schema file, a schema that is not valid JSON or not a valid JSON Schema, or a non-JSON response: exits 2 with a clear message.

The check runs after the body is written, so it composes with normal output and with --output-file.

Example

azd rest get https://management.azure.com/subscriptions/{sub}/resourceGroups/{rg}?api-version=2021-04-01 --validate-schema rg.schema.json

Notes

  • Uses github.com/santhosh-tekuri/jsonschema/v6, promoted from an indirect to a direct dependency (it was already in the module graph, so no new third-party code is pulled in). It supports Draft 4 through 2020-12 and preserves number precision via json.Number.
  • Reuses client.IsJSON for the response-type guard, matching how service.go and color.go already gate JSON handling.
  • Docs updated: CLI reference flag table, exit-code table, and a README example.

Tests cover a conforming response, a non-conforming response (with error output), and the error paths (missing schema, invalid schema JSON, non-JSON response), plus end-to-end conforming and non-conforming runs through Execute that confirm the body is still written.

Closes #267

--validate-schema <file> validates the JSON response against a JSON Schema
and exits non-zero when it does not conform, printing each validation error
to stderr so contract checks in CI can fail the build. The response body is
still printed first, so this composes with normal output.

Exit codes follow the existing contract: a conforming response exits 0, a
non-conforming response exits 1, and a missing or invalid schema file (or a
non-JSON response) exits 2 with a clear message. Validation uses the
santhosh-tekuri/jsonschema/v6 library, promoted from an indirect to a direct
dependency.

Closes #267

Co-authored-by: Copilot App <[email protected]>
@jongio jongio added the idea Feature idea from the idea pipeline label Jul 12, 2026
@jongio jongio self-assigned this Jul 12, 2026
github-actions Bot added a commit that referenced this pull request Jul 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Website Preview

Your PR preview is ready!

📎 Preview URL: https://jongio.github.io/azd-rest/pr/274/

This preview will be automatically cleaned up when the PR is closed.

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Test This PR

A preview build (0.5.0-pr274) is ready for testing!

One-Line Install (Recommended)

PowerShell (Windows):

iex "& { $(irm https://raw.githubusercontent.com/jongio/azd-rest/main/scripts/install-pr.ps1) } -PrNumber 274 -Version 0.5.0-pr274"

Bash (macOS/Linux):

curl -fsSL https://raw.githubusercontent.com/jongio/azd-rest/main/scripts/install-pr.sh | bash -s 274 0.5.0-pr274

Uninstall

PowerShell (Windows):

iex "& { $(irm https://raw.githubusercontent.com/jongio/azd-rest/main/scripts/uninstall-pr.ps1) } -PrNumber 274"

Bash (macOS/Linux):

curl -fsSL https://raw.githubusercontent.com/jongio/azd-rest/main/scripts/uninstall-pr.sh | bash -s 274

Build Info:

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

Labels

idea Feature idea from the idea pipeline

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add --validate-schema to check a response against a JSON Schema

1 participant