metosin / spec-tools

Clojure(Script) tools for clojure.spec
Eclipse Public License 2.0
593 stars 94 forks source link

JSON Schema output is missing the key $schema #206

Open miikka opened 4 years ago

miikka commented 4 years ago

As pointed out in #205, the JSON Schema output does not have a top-level key "$schema". According to the draft-04 spec:

The "$schema" keyword is both used as a JSON Schema version identifier and the location of a resource which is itself a JSON Schema, which describes any schema written for this particular version.

This keyword MUST be located at the root of a JSON Schema. The value of this keyword MUST be a URI [RFC3986] and a valid JSON Reference [json-reference]; this URI MUST be both absolute and normalized. The resource located at this URI MUST successfully describe itself. It is RECOMMENDED that schema authors include this keyword in their schemas.

Since there are now multiple versions of JSON Schema in production use, it'd be wise to include "$schema" in spec-tools's output:

"$schema": "http://json-schema.org/draft-04/schema#"
wandersoncferreira commented 4 years ago

Hi! As you said in #205 the current implementation only honors the draft-04 contract therefore I thought of only adding a simple function like

(defn- add-schema-key
  "As discussed in issue #205 the new drafts are not backward-compatible.
  That said, we can only add this new key with the correct value as-is."
  [schema]
  (assoc schema :$schema "http://json-schema.org/draft-04/schema#"))

And call it in the same places where maybe-with-titles is used. Is that appropriate? I can open a new PR with this inclusion.

miikka commented 4 years ago

That sounds like a good solution, a PR would be certainly welcome.