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" }
}
}
SchemaNodes have anisSchemaproperty that can be used to know if something is a subschema.SchemaNodealso has akeywordUriproperty. The keyword URI can be used to find theSchemaNodefor the definitions object. The keyword URI for definitions ishttps://json-schema.org/keyword/definitionsregardless of the draft. So, don't look for a property calleddefinitions/$defsin the schema, look for the node with with thehttps://json-schema.org/keyword/definitionskeyword 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/barto extract the subschema to$defsand 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" } } }