[feature]: Add an offline validator for recorded tests, mocks, and mappings
Is there an existing feature request for this?
- I have searched the existing issues
Enhancement description
Summary
Add keploy validate, a read-only command that checks local recorded tests, mocks, and mappings before replay. It reports malformed files and inconsistent references without starting the application, Docker, or the Keploy agent.
The proposed command name and initial scope are open to maintainer feedback.
Problem
A recording contains related files: test cases describe API interactions, mocks contain recorded dependency traffic, and mappings associate tests with mocks.
These files can become inconsistent after interrupted recordings, incomplete copies, manual edits, or Git merges. Examples include:
- A mapping references a mock that no longer exists.
- Multiple mapping entries use the same test ID.
- A recorded test file is empty or malformed.
- A binary mock file is truncated.
- YAML and JSON versions coexist, making it unclear which file replay will use.
Existing readers detect some problems during replay, while other conditions are tolerated. For example, replay can fall back to timestamp-based matching when mappings cannot be read, and the binary mock reader can accept a truncated tail.
These recovery behaviors do not provide a complete inspection of the saved recording. Users need a way to identify recording problems before investigating application failures or starting expensive replay jobs.
Proposed Behavior
The validator should:
- Discover the selected local recording sets.
- Read their test, mock, and mapping files.
- Check file structure and references between files.
- Report findings with an explanation and suggested next step.
- Return a nonzero exit status when validation fails or cannot be completed.
- Leave every input file unchanged.
The first version should support completed, locally available recordings. It should not download remote recordings or attempt automatic repairs.
Proposed Usage
The following commands are proposed interfaces, not existing commands:
# Validate local API-test recordings.
keploy validate --path .
# Validate selected sets.
keploy validate --path . --test-sets test-set-0,test-set-1
# Validate a mock-only recording used by an external test runner.
keploy validate --path . --mode mock --test-sets checkout-mocks
# Produce machine-readable output for CI.
keploy validate --path . --format json--path should follow Keploy's existing path conventions.
An explicit --mode mock avoids treating external test-runner names as missing local test files.
Validation Rules
| Check | Expected behavior |
|---|---|
| Malformed test, mock, or mapping file | Report an error with the file and available location information. |
| Empty test file | Report an error rather than silently skipping it. |
| Mapping references a missing mock | Report the mapping entry and missing mock name. |
| Duplicate mapping test IDs | Report an error before conversion into a map hides the duplicate. |
| Duplicate mock identities | Report ambiguous identities within the scope used by the recording format. |
| Mapping refers to a missing local test | Report an error in API-test mode; do not apply this rule to mock-only recordings. |
| Mapping kind differs from the referenced mock kind | Report an error when both kinds are explicitly present. |
| Startup mapping references a missing mock | Report an error, just as for per-test mappings. |
| Truncated binary mock stream | Report incomplete data rather than treating a recovered prefix as a complete recording. |
| Multiple storage variants exist | Explain which variant replay would select; do not automatically classify coexistence as corruption. |
| Unsupported format or unavailable decoder | Report incomplete validation, not a clean pass. |
The validator must distinguish confirmed errors from informational findings.
Compatibility Requirements
The following cases must remain valid:
- Missing mappings: Older recordings can legitimately use timestamp-based matching.
- No dependency mocks: Some API tests make no external calls.
- Shared mocks: The same mock can legitimately appear in several tests or in startup mappings.
- Unreferenced mocks: These are not automatically invalid or safe to delete.
- Legacy mapping formats: Continue supporting the legacy and structured representations accepted by Keploy.
- Mock-only recordings: External test names do not require corresponding files under
tests/. - Mixed storage formats: Respect existing format-selection rules.
Validation should apply to stable recordings. If files change during inspection, report that the result is incomplete and ask the user to retry after recording finishes.
Example Output
Illustrative text output:
Validating test-set-0
ERROR MISSING_MOCK
File: test-set-0/mappings.yaml
Test: test-3
Reference: mock-12
No matching mock exists in the selected local mock files.
Next step: restore the missing recording or re-record the affected set.
ERROR DUPLICATE_TEST_ID
File: test-set-0/mappings.yaml
Test: test-2
Multiple mapping entries use this test ID.
Next step: review the conflicting entries.
Checked 1 set. Found 2 errors.
Validation failed. No files were modified.JSON output should include:
- A report schema version.
- Overall status:
passed,failed, orincomplete. - Counts of inspected sets, tests, and mocks.
- Findings with stable codes, severity, file, test/mock identifier, and explanation.
- Line/document location where available.
- Suggested next steps.
JSON output must contain only valid JSON on stdout. Diagnostics should go to stderr.
Reports should not include recorded request bodies, response bodies, authorization headers, or credentials.
Implementation Plan
1. Add CLI Wiring
Add cli/validate.go using the existing command registration pattern.
Implement path, set-selection, mode, and output-format options.
Use error-returning command handling so validation errors reach the process exit status. Define and document distinct outcomes for successful validation, invalid recordings, and incomplete inspection.
2. Add a Validation Service
Introduce a focused service, for example pkg/service/validate, responsible for:
- Discovering recording sets.
- Coordinating read-only readers.
- Running validation rules.
- Collecting and sorting findings.
- Rendering text or JSON reports.
The service should depend only on local recording readers. Its initialization must avoid Docker clients, agent setup, application execution, and remote storage access.
3. Reuse Existing Decoding Logic
Build on the existing storage packages:
pkg/platform/yaml/testdbpkg/platform/yaml/mockdbpkg/platform/yaml/mapdb- Shared YAML/JSON readers and mapping models.
Reuse decoding and format-selection behavior rather than implementing a separate interpretation of Keploy recordings.
Where existing readers intentionally skip or recover from malformed data, add a strict inspection path that preserves the diagnostic. Keep normal replay behavior unchanged.
Support YAML, JSON/NDJSON, and the current binary mock format. Unavailable decoding support must be explicitly reported.
4. Build Per-Set Indexes
Collect test identities, mock identities, kinds, and source locations.
Inspect original mapping entries before converting them into lookup maps, so duplicate entries remain detectable.
Stream large mock files and retain the metadata needed for validation rather than keeping all payloads in memory.
Mock identity must follow the applicable recording format. If per-owner recording support lands, names must be interpreted within that ownership scope.
5. Validate Relationships
Resolve per-test and startup mapping references against the mock index.
In API-test mode, resolve mapped test IDs against recorded test identities using replay-compatible identity rules.
Do not infer corruption solely from missing mappings, unused mocks, or shared references.
Continue checking independent files after a failure, but suppress misleading follow-up findings when an index could not be read completely.
6. Document CI Usage
Document running validation before replay in CI.
The standalone command should be opt-in initially. Automatically adding it to every replay can be considered separately after compatibility and performance are established.
Testing Plan
Add fixtures and tests covering:
- Valid YAML, JSON/NDJSON, and binary recordings.
- Legacy and current mapping representations.
- Missing mock references.
- Duplicate test and mock identities.
- Malformed and empty files.
- Truncated binary streams.
- Valid recordings without mappings or dependency mocks.
- Shared and startup mocks.
- Mock-only recordings with external test names.
- Storage-format preference when multiple variants exist.
- Unsupported formats and unavailable decoders.
- Cancellation and files changing during inspection.
CLI tests should verify:
- Correct exit statuses.
- Deterministic text and valid JSON output.
- No application, Docker, or agent startup.
- No network dependency.
- No changes to input files.
- No recorded payloads or credentials in reports.
Acceptance Criteria
- Users can validate completed local recordings without running their application.
- The command works without Docker, root privileges, or network interception.
- Broken references and malformed files produce actionable findings.
- Supported legacy recordings do not produce false errors.
- API-test and mock-only modes apply the correct identity rules.
- Unsupported or partially inspected data cannot produce a clean pass.
- Text and JSON output are deterministic.
- CI can distinguish validation failure from success through exit status.
- Input files remain unchanged.
- Documentation explains usage, compatibility, and limitations.
Scope and Limitations
This feature validates recording structure and internal consistency. It does not guarantee replay success or verify application behavior.
It cannot reliably detect:
- A mapping pointing to the wrong but existing mock.
- A request that was never captured and left no reference.
- Semantically incorrect responses that remain structurally valid.
- Differences caused by application state, timing, or environment.
Automatic repair, mock deletion, re-recording, environment health checks, and remote downloads are outside the initial scope.
Related Work
- #4488 concerns stale mappings after mock re-recording. This validator could detect resulting missing references, but would not replace the underlying fix.
- #3878 proposes environment/agent health checks. This proposal focuses on saved recording files.
- #4587 proposes per-test mock ownership. File discovery and identity checks should accommodate that design if merged.
During the repository review, no equivalent standalone offline recording validator was found in the inspected OSS implementation or issue/PR searches. Maintainer confirmation is requested before implementation to check for overlapping work.
Why should this be worked on?
Users should be able to check whether their recording data is usable before debugging their application or running replay infrastructure.
A read-only validator would make recording problems easier to diagnose, catch inconsistent files earlier in CI, and provide a clear distinction between structurally invalid test data and failures observed during replay.
Repository
keploy
Source: keploy/keploy