Skip to content

file() loader outputs object schema for top-level array JSON, causing validation issues in VS Code #16602

@atomiechen

Description

@atomiechen

Astro Info

Astro                    v6.2.2
Node                     v22.22.0
System                   Linux (x64)
Package Manager          unknown
Output                   static
Adapter                  none
Integrations             @astrojs/mdx
                         @astrojs/sitemap

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

This is a follow-up to #13536.

I have tested again after upgrading Astro to v6.2.2 (from v5.5.6). The build passes cleanly.

The issue still exists: the file() loader with an object schema still generates an incorrect JSON schema when the source JSON is an array.

Reproduction summary:

  1. src/data/dogs.json: an array of objects, each with {id, name}
[
  {
    "id": 1,
    "name": "lab"
  },
  {
    "id": 2,
    "name": "yorkie"
  }
]
  1. src/content.config.ts: uses file() loader with schema z.object({ id: z.number(), name: z.string() })
import { file } from 'astro/loaders';
import { defineCollection, z } from 'astro:content';

const dogsCollection = defineCollection({
  loader: file('src/data/dogs.json'),
  schema: z.object({
    id: z.number(),
    name: z.string(),
  }),
});

export const collections = { dogs: dogsCollection };
  1. Running npm run sync produces .astro/collections/dogs.schema.json like below:
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "$schema": {
      "type": "string"
    }
  },
  "additionalProperties": {
    "type": "object",
    "properties": {
      "id": {
        "type": "number"
      },
      "name": {
        "type": "string"
      }
    },
    "required": [
      "id",
      "name"
    ]
  }
}

Expected: Since dogs.json is an array, the generated schema should be type: "array" (with items), not an object with additionalProperties. The current schema causes VS Code JSON validation to flag the file as invalid, since the schema reports an object but the file is actually an array.

What's the expected result?

When file() loader is given a top-level array JSON file, Astro should output a schema with type: array (not object), so that VS Code's JSON/YAML validation works without having to change content shape to object-map.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-wuk59bem-jrik786q?file=.vscode%2Fsettings.json

Participation

  • I am willing to submit a pull request for this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P2: has workaroundAn edge case that only affects very specific usage, but has a trivial workaround (priority)fix pending verificationReporter needs to verify the triage bot fix workspkg: astroRelated to the core `astro` package (scope)

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions