Skip to content

Publish a JSON Schema for coverage.json#1184

Open
sferik wants to merge 1 commit into
mainfrom
schema
Open

Publish a JSON Schema for coverage.json#1184
sferik wants to merge 1 commit into
mainfrom
schema

Conversation

@sferik
Copy link
Copy Markdown
Collaborator

@sferik sferik commented May 14, 2026

Since we want third-party tools to use the coverage.json (and not .resultset.json), we should encourage that by versioning the document and publishing a JSON Schema. This was suggested by @keithrbennett in #1143 (comment) and I agree that it’s a good idea.

This PR adds:

  • schemas/coverage.schema.json, which includes meta, total, per-file coverage, groups, and all four errors shapes. The gemspec has also been modified to add the schemas directory.
  • That file contains a meta.schema_version (currently "1.0"), which is intentionally independent of the gem version, so we can bump the gem version without bumping the schema version.
  • A spec that validates JSONFormatter output against the schema.
  • json_schemer as a development dependency to validate the schema.

A few choices worth mentioning:

  • The version number lives in the document, not the filename. This keeps the canonical URL stable across bumps.
  • I chose JSON Schema Draft-07 over 2020-12, since nothing in the shape needs the newer features and there still seems to be better tooling for Draft-07.
  • I documented this in the README rather than a separate docs/ tree, since that’s where people land and I don’t think there’s enough documentation to warrant its own directory at this point.

This comment was marked as resolved.

@keithrbennett
Copy link
Copy Markdown

keithrbennett commented May 17, 2026

@sferik Thanks for doing this. Regarding versioned and unversioned schema files, I think the versioned files need to be primary/canonical and the 'latest' or 'current' version a convenience. I think the generated result set data file needs to contain a schema spec that includes the schema version so that it will always be usable when future schema versions are released. I may be misunderstanding though.

Here is a ChatGPT discussion of their recommended version strategy and implementation:

https://chatgpt.com/share/6a095730-57d0-8322-aac5-38f90035c2ac

(References in that chat to 'cov-loupe' are unintentional and should be 'simplecov' instead.)

Comment thread README.md
"groups": { "<group name>": { /* per-group stats + files */ } },
"errors": { /* minimum_coverage, minimum_coverage_by_file, minimum_coverage_by_group, maximum_coverage_drop violations */ }
}
```
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to get full example for some minimal 'Hello World' script (that covers all features).

Comment thread README.md

> The JSON formatter was originally a separate gem called [simplecov_json_formatter](https://github.com/codeclimate-community/simplecov_json_formatter). It is now built in and loaded by default. Existing code that does `require "simplecov_json_formatter"` will continue to work.

### `coverage.json` schema
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### `coverage.json` schema
### SimpleCov JSON coverage format schema

@@ -0,0 +1,242 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest spec is 2020-12. Why not use it?

https://json-schema.org/specification

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mention my reasoning for this in the PR description above:

I chose JSON Schema Draft-07 over 2020-12, since nothing in the shape needs the newer features and there still seems to be better tooling for Draft-07.

Comment thread README.md

### `coverage.json` schema

The `coverage.json` payload is treated as a public contract. Every emitted file carries a `meta.schema_version` (`"major.minor"`) describing which version of the schema it conforms to. A JSON Schema (draft-07) lives in the repo at [`schemas/coverage.schema.json`](schemas/coverage.schema.json); downstream tools can use it to validate inputs, generate types, or pin themselves to a known shape.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The `coverage.json` payload is treated as a public contract. Every emitted file carries a `meta.schema_version` (`"major.minor"`) describing which version of the schema it conforms to. A JSON Schema (draft-07) lives in the repo at [`schemas/coverage.schema.json`](schemas/coverage.schema.json); downstream tools can use it to validate inputs, generate types, or pin themselves to a known shape.
Generated `coverage.json` is covered by the [JSON Schema](schemas/coverage.schema.json), so that tools can validate and use it as interchangeable format.
The schema is versioned, with the version stored in `meta.schema_version` (`"major.minor"`).

Comment thread simplecov.gemspec
gem.required_ruby_version = ">= 3.1"

gem.files = Dir["lib/**/*.*", "exe/*", "LICENSE", "CHANGELOG.md", "README.md", "doc/*"]
gem.files = Dir["{lib,schemas}/**/*.*", "exe/*", "LICENSE", "CHANGELOG.md", "README.md", "doc/*"]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we are at it, maybe it is possible to generate human readable documentation with real world examples to close #1030 (comment) ?

There are a couple of documentation generators listed https://json-schema.org/tools#documentation but I haven't found a nice one yet.

@abitrolly
Copy link
Copy Markdown

Here is a ChatGPT discussion

image

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants