Skip to content

Extract subschema (to definitions) #132

Open
@jdesrosiers

Description

@jdesrosiers

SchemaNodes have an isSchema property that can be used to know if something is a subschema. SchemaNode also has a keywordUri property. The keyword URI can be used to find the SchemaNode for the definitions object. The keyword URI for definitions is https://json-schema.org/keyword/definitions regardless of the draft. So, don't look for a property called definitions/$defs in the schema, look for the node with with the https://json-schema.org/keyword/definitions keyword URI.

Example:

Before refactoring:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",

  "type": "object",
  "properties": {
    "foo": { "$ref": "#/$defs/number" },
    "bar": { "type": "boolean" }
  },

  "$defs": {
    "number": { "type": "number" }
  }
}

There should be a code action on the subschema at /properties/bar to extract the subschema to $defs and replace it with a reference ($ref) to the subschema in $defs.

After refactoring:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",

  "type": "object",
  "properties": {
    "foo": { "$ref": "#/$defs/number" }
    "bar": { "$ref": "#/$defs/boolean" },
  },

  "$defs": {
    "number": { "type": "number" },
    "boolean": { "type": "boolen" }
  }
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions