Skip to content

feat: Create CRAPSchema constant and expose via gaze schema crap #141

Description

@jflowers

Summary

Create a formal JSON Schema (Draft 2020-12) for gaze crap --format=json output and wire it into the gaze schema command. This is the feature request portion of #118.

Parent issue: #118
Depends on: #140 (positional arg support must exist first)

Problem

The CRAP JSON output is the most likely to be machine-consumed by CI scripts and dashboards, but has no formal JSON Schema. The docs/reference/json-schemas.md:119 explicitly notes: "The CRAP JSON output is not covered by a formal embedded schema."

Scope Decisions Required

1. Output mode scoping

gaze crap produces two different JSON structures:

  • Normal mode: crap.Reportscores []Score + summary Summary
  • Baseline comparison mode (--baseline): crap.ComparisonResultdeltas, new_functions, removed_functions, comparison

Decision needed: Does the schema cover both output modes, or only the standard crap.Report? Options:

  • (a) Schema covers only crap.Report; document that --baseline produces a different structure
  • (b) Schema uses oneOf to cover both shapes
  • (c) Two separate schemas: CRAPSchema and CRAPComparisonSchema

2. Forward compatibility

Consider adding additionalProperties: true to allow additive field changes without breaking downstream validation. The CRAP output has gained 6 new fields across recent releases.

Proposed Changes

1. New constant in internal/report/schema.go

CRAPSchema constant (~200-300 lines) covering crap.Report:

  • Score struct: 14 JSON fields (8 optional with omitempty)
  • Summary struct: ~15 JSON fields (8 optional)
  • RecommendedAction struct: 10 JSON fields
  • Enum types: Quadrant (4 values), FixStrategy (4 values)
  • Follow existing pattern: Draft 2020-12, $id URL, $defs for shared types, description on every field

2. CLI wiring

Add "crap" to ValidArgs in newSchemaCmd() and dispatch to report.CRAPSchema.

3. Tests (round-trip, not hand-crafted)

  • TestCRAPSchema_Compiles — schema is valid Draft 2020-12
  • TestCRAPSchema_ValidatesMinimalOutput — minimal Report with one Score
  • TestCRAPSchema_ValidatesFullOutput — all optional fields populated
  • TestCRAPSchema_ValidatesEmptyScores — empty scores array
  • TestCRAPWriteJSON_ValidAgainstSchemaactual WriteJSON() output validated against schema (critical round-trip test)

4. Documentation

  • docs/reference/json-schemas.md — add annotated CRAP schema section
  • docs/reference/cli/schema.md — add crap to argument list and examples

Risks

  • Maintenance ratchet: Every future field addition to Score/Summary requires a schema update
  • Schema drift: Manually-maintained Go string constants have no automated drift detection against struct tags
  • Baseline mode: If not covered by the schema, users may be confused when --baseline output fails validation

Note on report output

Per the review council analysis on #118, gaze report --format=json should NOT get a schema. It is explicitly documented as "an internal format" (json-schemas.md:352), uses json.RawMessage fields, and CI integration uses threshold flags rather than schema validation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions