Definitelytyped: [json-schema] property $schema should be type string

Created on 17 Jan 2019  路  1Comment  路  Source: DefinitelyTyped/DefinitelyTyped

@bcherny @cyrilletuzi @lucianbuzzo @rolandjitsu

The type of the $schema property of a JSONSchema7 object is JSONSchema7Version, which is basically an enum of strings (each string being a URI to an official JSON-Schema meta-schema).

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/24b35dd9915b061e962bbacf996b422db09e52c9/types/json-schema/index.d.ts#L520

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/24b35dd9915b061e962bbacf996b422db09e52c9/types/json-schema/index.d.ts#L507-L510

However, this makes it impossible to extend a JSONSchema7 object, if you are writing your own meta-schemata:

/**
 * A meta-schema that validates JSON Schemata of a certain form.
 */
interface ExampleMetaSchema extends JSONSchema7 {
    /** The `$schema` property must be this string literal. */
    $schema: 'https://example.com/chharvey/meta-schema.jsd#'; // ---- ERROR! ----

    /** Title required. */
    title: string;

    /** Description required. */
    description: string;

    /**
     * The `type` property must be the literal `'array'`.
     * (The instance schema must only validate arrays.)
     */
    type: 'array';
}

In the example above, the line $schema: 'https://example.com/chharvey/meta-schema.jsd#'; will throw an error, because that string literal is not assignable to the JSONSchema7Definition type.

My suggestion is to allow the $schema property of JSONSchema7 to be a string:

interface JSONSchema7 {
    // ...
    $schema: JSONSchema7Version|string;
    // ...
}

Indeed, my suggestion is supported by the official JSON Schema Core spec as well as the official Core/Validation meta-schemata. The spec only specifies that $schema must be an URI, but does not specify which URIs, and the meta-schemata contains the following definition:

"$schema": {
    "type": "string",
    "format": "uri"
}

Most helpful comment

@chharvey feel free to make a PR.

>All comments

@chharvey feel free to make a PR.

Was this page helpful?
0 / 5 - 0 ratings