Rendered from specs/F06-inference.md — edit it there, not here.
F06 — Schema Inference from Data Files
Overview
The extension can generate a draft JSON Schema from an existing JSON, JSONC, JSONL, or YAML data file using the genson-js library.
User Stories
- As a developer starting from existing data, I want to generate a schema automatically rather than writing it from scratch.
- As a team lead adopting schema-first workflows, I want a starting schema I can refine and commit alongside the data.
Functional Requirements
Command
- F06-FR-01 The command
jsonschema.inferSchemaMUST be available in the Command Palette. - F06-FR-02 The Generate Schema toolbar icon (wand) MUST appear only when
jsonschema.isJsonSchemaisfalse(data files, not schema files) andjsonschema.hasSchemaBindingisfalse— when the active file already has a schema (an inline$schema, a settings binding, or a natively-resolved schema per F04-FR-15) there is no point offering to generate one from it, so the icon MUST be hidden. Thejsonschema.hasSchemaBindingcontext key is maintained by the binding manager's status-bar refresh, alongside the state it publishes for F04-FR-06/07/15.
Parsing
- F06-FR-03 JSON files MUST be parsed with
JSON.parse. - F06-FR-04 JSONC files MUST have comments stripped before parsing.
- F06-FR-05 JSONL files MUST be parsed as an array (one JSON object per line).
- F06-FR-06 YAML / YML files MUST be parsed with the
yamlpackage. - F06-FR-07 A parse error MUST show an error notification with the error message and MUST NOT proceed.
Generation
- F06-FR-08 The inferred schema MUST be produced by
genson-js'screateSchemafunction. - F06-FR-09 The generated schema MUST include
"$schema": "http://json-schema.org/draft-07/schema#". - F06-FR-10 The generated schema MUST be pretty-printed (2-space indentation).
Output
- F06-FR-11 The generated schema MUST be opened in a new untitled document with
languageId: "json"inViewColumn.Beside. - F06-FR-12 A notification MUST inform the user to save and bind the schema to use it for validation.
Acceptance Criteria
- Running Generate Schema from This File on
person-valid.jsonopens a new editor tab containing valid JSON with a$schemafield and at least the top-level property types inferred from the data. - Running the command on a malformed JSON file shows an error notification.
Requirement traceability
Status of each requirement in specs/traceability.json. Test coverage is auto-discovered from [ID] tags in test titles and is not listed here.
| Requirement | Status | Implementation | Note |
|---|---|---|---|
F06-FR-01 | implemented | package.json | inferSchema in commandPalette |
F06-FR-02 | implemented | package.json, src/SchemaBindingManager.ts | inferSchema editor/title icon, gated on !isJsonSchema && !hasSchemaBinding; hasSchemaBinding context key set in SchemaBindingManager.refresh() |
F06-FR-03 | implemented | src/extension.ts | inferSchema: JSON.parse path (unit-tested) |
F06-FR-04 | implemented | src/extension.ts | inferSchema: JSONC strip (unit-tested) |
F06-FR-05 | implemented | src/extension.ts | inferSchema: JSONL array (unit-tested) |
F06-FR-06 | implemented | src/extension.ts | inferSchema: YAML parse (unit-tested) |
F06-FR-07 | implemented | src/extension.ts | inferSchema: parse-error notification (unit-tested) |
F06-FR-08 | implemented | src/extension.ts | inferSchema: createSchema (unit-tested) |
F06-FR-09 | implemented | src/extension.ts | inferSchema: draft-07 $schema (unit-tested) |
F06-FR-10 | implemented | src/extension.ts | inferSchema: pretty-printed (unit-tested) |
F06-FR-11 | implemented | src/extension.ts | inferSchema: opens json Beside (unit-tested) |
F06-FR-12 | implemented | src/extension.ts | inferSchema: bind hint notification (unit-tested) |