Skip to content

Auto-insert $schema when missing from DB file on startup#1717

Merged
typicode merged 6 commits intomainfrom
copilot/fix-5502029-14747598-625448cc-db9d-4d3b-b6a1-7dcd9045f6b8
Feb 28, 2026
Merged

Auto-insert $schema when missing from DB file on startup#1717
typicode merged 6 commits intomainfrom
copilot/fix-5502029-14747598-625448cc-db9d-4d3b-b6a1-7dcd9045f6b8

Conversation

Copy link
Contributor

Copilot AI commented Feb 27, 2026

When a DB file lacks a root-level $schema property, json-server now automatically inserts the README-documented default path, consistent with the existing id auto-fix behavior.

Changes

  • src/service.ts: Added fixSchema(data) helper that sets $schema to ./node_modules/json-server/schema.json when absent; called in Service constructor alongside fixAllItemsIds
  • src/service.test.ts: Extended constructor test to assert $schema insertion; added test asserting an existing $schema value is not overwritten

Behavior

// db.json before (missing $schema)
{ "posts": [{ "id": "1", "title": "a title" }] }

// db.data after Service construction
{
  "$schema": "./node_modules/json-server/schema.json",
  "posts": [{ "id": "1", "title": "a title" }]
}
Original prompt

Implement an auto-fix for missing $schema in JSON/JSON5 DB files, similar in spirit to existing auto-fix behavior for missing id values.

Repository: typicode/json-server

Goal

When json-server reads a DB file and the root object does not include $schema, it should automatically insert:

"$schema": "./node_modules/json-server/schema.json"

This exact path must match what is documented in the README usage example.

Requirements

  1. Add logic in the same code path where DB normalization/auto-fix currently happens (where ids are auto-fixed) so that:
    • If root $schema is missing, it is added with value ./node_modules/json-server/schema.json.
    • If $schema already exists, leave it unchanged.
  2. Preserve existing id auto-fix behavior; do not regress current functionality.
  3. Ensure behavior applies to normal startup/write flow in a way consistent with existing auto-fixes.
  4. Add/update tests to cover:
    • Missing $schema gets inserted.
    • Existing $schema is not overwritten.
    • Existing id auto-fix tests still pass (or are updated only as needed).
  5. Run/update formatting and tests according to repository standards.

Notes

  • Use the README-documented path exactly: ./node_modules/json-server/schema.json.
  • Keep the change minimal and aligned with existing code style and architecture.
  • If there is a dedicated helper for auto-fixes/default data normalization, extend it there rather than introducing parallel logic.

This pull request was created from Copilot chat.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits February 27, 2026 09:21
Co-authored-by: typicode <5502029+typicode@users.noreply.github.com>
Copilot AI changed the title [WIP] Auto-fix missing $schema using README schema path in DB files Auto-insert $schema when missing from DB file on startup Feb 27, 2026
@typicode typicode marked this pull request as ready for review February 28, 2026 00:18
@typicode typicode merged commit 037609c into main Feb 28, 2026
1 check passed
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.

2 participants