hyperjump-io / json-schema

JSON Schema Validation, Annotation, and Bundling. Supports Draft 04, 06, 07, 2019-09, 2020-12, OpenAPI 3.0, and OpenAPI 3.1
https://json-schema.hyperjump.io/
MIT License
216 stars 22 forks source link

Issues with media types #42

Closed oliversalzburg closed 9 months ago

oliversalzburg commented 9 months ago

I'm trying to bundle a schema that I'm working on. I started with this code:

import { bundle } from "@hyperjump/json-schema/bundle";
const bundledSchema = await bundle(
  "https://schema.kitten-science.com/working-draft/settings-profile.schema.json",
);

This produces:

Error: https://schema.kitten-science.com/working-draft/settings-profile.schema.json is not a schema. Found a document with media type: application/schema+json

This is because the application/schema+json media type is defined in the index of this package, which I don't import here. I have to enforce running that code by running the index:

import "@hyperjump/json-schema";
import { bundle } from "@hyperjump/json-schema/bundle";
const bundledSchema = await bundle(
  "https://schema.kitten-science.com/working-draft/settings-profile.schema.json",
);

When I run that, I then get:

Error: https://json-schema.org/draft-07/schema is not a schema. Found a document with media type: application/octet-stream

What's the idea here? I certainly can't make json-schema.org change how they deliver their schemas. If this media type check is incompatible with json-schema.org, why does it even exist? I would assume the JSON Schema specs have to be loaded every time.

jdesrosiers commented 9 months ago

You have to import support for each dialect you need support for. It looks like you're using draft-07, so you want your first import to be "@hyperjump/json-schema/draft-07, not @hyperjump/json-schema. I'll see about making that more clear in the documentation.

When you import support for a dialect, that includes its meta-schema. So, you shouldn't ever see a standard dialect meta-schema being download if you've set things up correctly. The only time you might need to download a meta-schema is if you're using custom dialect.

About json-schema.org not serving schemas with the proper media type, that's getting fixed very soon. Until recently there were reasons it was hard to do and it hasn't been a priority because implementations are expected to package the meta-schemas, not download them on demand.

oliversalzburg commented 9 months ago

Oh I see. I might have just missed the relevant section of the docs also. That makes it much clearer. Thanks a lot 😊