Skip to main content

Verify YAML testspecs against JSON Schemas and generate tests from schema refs

Project description

TeDS — Test‑Driven Schema Development Tool

TeDS (Test‑Driven Schema Development Tool) is a CLI to specify and test your API contracts as YAML: verify that your JSON Schemas accept what they should and reject what they must, and generate tests from schema refs.

Why this tool?

APIs live and die by their contracts. A schema is a promise: it defines the shape of data and which values are allowed. In practice, teams often check only the “happy path” (examples) — linters like Redocly can verify that. What’s usually missing is verifying the negative space: that the schema actually rejects what must be rejected. This tool focuses on both sides of the promise.

What it gives you:

  • Contract verification as tests:
    • Positive cases: ensure the schema accepts data it should accept (including example‑based cases).
    • Negative cases: ensure the schema rejects data it must reject (explicit “invalid” cases, plus useful warnings for “format” divergences).
  • Repeatable and readable specs:
    • Tests are YAML, live next to your schemas, versioned in Git, and easy to review.
    • Output is deterministic and CI‑friendly; in‑place updates keep files short and curated.
  • Generation from real schemas:
    • Seed “valid” tests directly from examples in your schemas; extend with explicit edge cases as needed.

Best Practice: Test‑Driven Schema Development (TeDS)

TeDS promotes writing small, focused tests for your schemas — before or alongside the schema changes — and keeping them in version control. Benefits:

  • Early detection of breaking changes and ambiguity (especially with formats, boundaries, enums, additionalProperties, oneOf/anyOf).
  • Living documentation: explicit valid/invalid cases clarify intent for reviewers and consumers.
  • CI stability: deterministic validation and predictable outputs prevent regressions and flakiness.
  • Safer evolution: refactors and migrations are guarded by negative tests that catch unintended acceptance.
  • Shared understanding: developers, reviewers and integrators discuss examples, not abstractions.

TeDS fits how most API developers already think. In practice you start from intent: which payloads must be valid, and which must be rejected? With TeDS you write those expectations down first (valid and invalid examples), run the verifier, and then refine the schema until the expectations are met. This short feedback loop makes the schema a faithful reflection of the contract you have in mind — and the tests become living documentation that guards it over time.

Why this matters (concrete scenarios):

Bottom line: You specify the contract, you also test the contract — both what is allowed and what is forbidden. That improves quality and serves as living documentation.

Install

  • From source (dev):
    • python3 -m venv .venv && . .venv/bin/activate
    • pip install -r requirements.txt
    • Optional build: pip install hatch && hatch build

Installation from PyPI can be added later once releases are tagged.

Quickstart

Testspec Format

Top‑level YAML document:

version: "1.0.0"   # required SemVer; must match tool’s supported MAJOR and not exceed supported MINOR
tests:
  <ref>:            # e.g. schema.yaml#/Foo
    valid:   { <cases> }
    invalid: { <cases> }

Case objects may contain:

  • description: string
  • payload: any
  • parse_payload: boolean (if true, payload is parsed as YAML/JSON)
  • result: SUCCESS|WARNING|ERROR
  • message: string (error message)
  • validation_message: string (validator message)
  • payload_parsed: any (emitted when parse_payload is true)
  • from_examples: boolean (derived by generator)
  • warnings: [string | {generated, code}]

The schema is in spec_schema.yaml. TeDS validates your testspec against this schema.

Strict YAML parsing:

  • duplicate keys are rejected to avoid ambiguity.

Verify a testspec:

  • teds verify demo/sample_tests.yaml

Generate testspec(s) from schema refs:

  • teds generate demo/sample_schemas.yaml#/components/schemas → writes demo/sample_schemas.components+schemas.tests.yaml

Public API–inspired demos:

Verify negative and positive contract cases:

  • teds verify demo/public_specs.yaml
  • Schemas: demo/public_schemas.yaml (email contact, ISO date-time, phone E.164, currency+amount, strict user object)
  • Specs: demo/public_specs.yaml (explicit invalid/valid cases highlighting typical pitfalls)

Exit codes:

  • 0: success
  • 1: verification produced cases with result: ERROR
  • 2: hard failures (I/O, YAML parse, invalid testspec schema, schema/ref resolution, version mismatch, unexpected)

CLI Tutorial

Overview

The TeDS CLI centers around two commands: verify and generate. Paths are relative to the current working directory. By default, only local schemas are resolved; enable network access explicitly (see Network Access).

Verify

teds verify [--output-level all|warning|error] [-i|--in-place] [--allow-network] <SPEC>...

  • Verifies testspec file(s) and prints normalized results to stdout unless -i is used (then writes back to the file).
  • Examples:
    • Verify a single file (default output level = warning):
      • teds verify demo/sample_tests.yaml
    • Verify multiple files and show only errors:
      • teds verify a.yaml b.yaml --output-level error
    • Write results back in place (in-place preserves all top-level metadata like version and only updates tests):
      • teds verify demo/sample_tests.yaml -i

Version gate:

  • testspec version must match the supported MAJOR and not exceed the supported MINOR; otherwise RC=2 and no write.

Output levels:

  • all: show everything
  • warning: show WARNING/ERROR
  • error: show only ERROR

Network:

  • add --allow-network to enable HTTP/HTTPS $ref resolution (see Network notes below).

Warnings (user-defined and generated)

TeDS distinguishes between user-defined warnings and tool-generated warnings. In both cases a successful case (result: SUCCESS) is elevated to result: WARNING if any warnings are present — this helps you surface “it works, but be careful” situations in CI and reviews.

  • User-defined warnings

    • Purpose: Let authors call out policy or integration caveats that should be visible in output without failing the case.
    • How: add a warnings array to a case with free‑form strings.
    • Example:
      tests:
        schema.yaml#/Email:
          valid:
            "policy note":
              payload: alice@example.com
              warnings:
                - "Accepts plus-addressing, downstream system strips tags"
      
    • Effect: the case is marked with result: WARNING in the output; it shows up with --output-level warning|all.
  • Generated warnings

    • Purpose: TeDS can attach structured warnings when it detects situations that are valid but fragile. Currently implemented for JSON Schema format:
      • Code: format-divergence — strict validators (that enforce format) reject an instance, while non‑strict validators accept it.
      • Output structure under the case:
        warnings:
          - generated: |
              Relies on JSON Schema 'format' assertion (format: email).
              Validators that *enforce* 'format' will reject this instance.
              Consider enforcing the expected format by adding an explicit 'pattern' property to the schema.
            code: format-divergence
        
    • How to act on generated warnings
      • Prefer explicitness: tighten the schema with a pattern that encodes your acceptance criteria (e.g., for email).
      • Re‑run TeDS — if the schema covers your intent, the warning disappears.
      • If the divergence is acceptable by design (e.g., non‑strict runtime), keep the warning as living documentation; it will remain visible at --output-level warning|all.
Notes
  • Warnings do not cause result: ERROR and therefore do not turn the overall exit code to 2. They do, however, change a case’s result from SUCCESS → WARNING; with --output-level error they are filtered out.
  • “Generated” warning entries are added by TeDS; reserve that shape ({generated, code}) for tool output. For user-authored warnings prefer plain strings.

In-Place Behavior

With -i/--in-place, TeDS writes only the tests section back to the file; all top-level metadata (e.g., version) remain as is. On version mismatch (unsupported MAJOR/MINOR), TeDS does not write and returns RC=2 with a clear message.

Generate

teds generate [--allow-network] REF[=TARGET] ...

  • Generates testspec(s) for direct child schemas under a JSON Pointer.
  • Mapping syntax: each argument is REF[=TARGET].
    • REF: path/to/schema.yaml#/<json-pointer>
      • Pointer defaults to #/ (root) if omitted, so schema.yaml is equivalent to schema.yaml#/.
    • TARGET: optional literal path or template. If it’s a directory, the default filename is appended.
      • Relative targets resolve next to the schema file.
  • Default filename rules:
    • Pointer #/ (root) → {base}.tests.yaml
    • Any other pointer → {base}.{pointer}.tests.yaml (pointer without leading /, sanitized)
    • "sanitized" uses + as segment separator and escapes + inside segments by doubling it (++). Example: Emailcomponents+schemas+Email.
  • Template tokens (usable in TARGET): {file}, {base}, {ext}, {dir}, {pointer}, {pointer_raw}, {pointer_strict}, {index}
  • Examples (assume schema at demo/sample_schemas.yaml):
    • Omit TARGET (default filename next to schema):
      • teds generate demo/sample_schemas.yaml#/components/schemas
      • → writes demo/sample_schemas.components+schemas.tests.yaml
    • Omit pointer and target (root pointer + default filename):
      • teds generate demo/sample_schemas.yaml
      • → writes demo/sample_schemas.tests.yaml
    • Directory target (default filename appended):
      • teds generate demo/sample_schemas.yaml#/=specs/
      • → writes demo/specs/sample_schemas.tests.yaml
    • Template with sanitized pointer:
      • teds generate demo/sample_schemas.yaml#/components/schemas=specs/{base}.{pointer}.tests.yaml
      • → writes demo/specs/sample_schemas.components+schemas.tests.yaml
    • Template with raw pointer (nested directories):
      • teds generate demo/sample_schemas.yaml#/=specs/{base}/{pointer_raw}.tests.yaml
      • → writes demo/specs/sample_schemas/components/schemas.tests.yaml
  • Recommendation:
    • If you typically have exactly one schema root per file, and you target deeper roots (with / in the pointer), prefer a succinct literal mapping to keep filenames short, e.g.:
      • teds generate demo/sample_schemas.yaml#/components/schemas={base}.test.yaml
      • → writes demo/sample_schemas.test.yaml
    • This avoids long {pointer} tokens in filenames while still being deterministic.
  • Network: add --allow-network to enable HTTP/HTTPS $ref resolution (see Network notes below).

Network Access

By default, TeDS resolves only local file:// schemas. Enable remote $refs with --allow-network.

  • Limits: global timeout and size cap (defaults: 5s, 5MiB per resource).
  • Overrides: CLI --network-timeout, --network-max-bytes or env TEDS_NETWORK_TIMEOUT, TEDS_NETWORK_MAX_BYTES.
  • Recommended for CI: keep default (local-only). If enabling network, ensure stability (pin URLs/versions) and mind SSRF/DoS considerations.

Versioning & Compatibility

Tool uses Semantic Versioning and derives its version from Git tags (vX.Y.Z).

  • teds --version prints tool version and the supported testspec major.

Testspecs require version (SemVer). The tool enforces:

  • MAJOR must match the tool’s supported major.
  • MINOR must be less than or equal to the tool’s supported minor.
  • PATCH is ignored for compatibility checks.
  • On mismatch: RC=2, nothing is written. The error explains what to upgrade.

References and prior art

Development

  • Create env: python3 -m venv .venv && . .venv/bin/activate
  • Install deps: pip install -r requirements.txt
  • Run tests: pytest -q or hatch run test
  • Build (wheel/sdist): pip install hatch && hatch build (uses hatchling + hatch-vcs, version from Git tag)

Contributing

  • Conventional Commits (feat, fix, docs, etc.).
  • Keep changes focused; update or add tests under tests/.

Security Notes

  • External refs (HTTP/HTTPS): disabled by default for reproducibility and safety.
    • Opt-in with --allow-network to resolve remote $refs.
    • Limits: global timeout and size cap (defaults: 5s, 5MiB per resource).
    • Overrides: CLI --network-timeout, --network-max-bytes or env TEDS_NETWORK_TIMEOUT, TEDS_NETWORK_MAX_BYTES.
    • Recommended for CI: keep default (local-only). If enabling network, ensure stability (pin URLs/versions) and mind SSRF/DoS considerations.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

teds-0.1.19-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file teds-0.1.19-py3-none-any.whl.

File metadata

  • Download URL: teds-0.1.19-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for teds-0.1.19-py3-none-any.whl
Algorithm Hash digest
SHA256 d45796f262dda271224b2ae0f41603c371621a27596891d2e4715da9afc40425
MD5 1cc6ce5413fc98002e8a936470ea657f
BLAKE2b-256 bb0b5c96ec09a2fe0c7e0e50e6bddbca300d9088a5c990a2aa014cd91b1a1979

See more details on using hashes here.

Provenance

The following attestation bundles were made for teds-0.1.19-py3-none-any.whl:

Publisher: release.yml on yaccob/teds

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page