support $anchor
This commit is contained in:
Родитель
56aa9db31a
Коммит
c5e86f42e3
|
@ -62,6 +62,7 @@ export interface JSONSchema {
|
|||
dependentRequired?: { [prop: string]: string[] };
|
||||
dependentSchemas?: JSONSchemaMap;
|
||||
$defs?: { [name: string]: JSONSchema };
|
||||
$anchor?: string;
|
||||
|
||||
// schema 2020-12
|
||||
prefixItems?: JSONSchemaRef[];
|
||||
|
|
|
@ -12,7 +12,7 @@ import { SchemaRequestService, WorkspaceContextService, PromiseConstructor, Then
|
|||
|
||||
import * as nls from 'vscode-nls';
|
||||
import { createRegex } from '../utils/glob';
|
||||
import { isObject } from '../utils/objects';
|
||||
import { isObject, isString } from '../utils/objects';
|
||||
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
|
@ -520,12 +520,10 @@ export class JSONSchemaService implements IJSONSchemaService {
|
|||
const result = new Map<string, JSONSchema>();
|
||||
this.traverseNodes(root, next => {
|
||||
const id = next.$id || next.id;
|
||||
if (typeof id === 'string' && id.charAt(0) === '#') {
|
||||
// delete next.$id;
|
||||
// delete next.id;
|
||||
const anchor = id.substring(1);
|
||||
const anchor = isString(id) && id.charAt(0) === '#' ? id.substring(1) : next.$anchor;
|
||||
if (anchor) {
|
||||
if (result.has(anchor)) {
|
||||
resolveErrors.push(localize('json.schema.duplicateid', 'Duplicate id declaration: \'{0}\'', id));
|
||||
resolveErrors.push(localize('json.schema.duplicateid', 'Duplicate anchor declaration: \'{0}\'', anchor));
|
||||
} else {
|
||||
result.set(anchor, next);
|
||||
}
|
||||
|
|
|
@ -312,6 +312,46 @@ suite('JSON Schema', () => {
|
|||
});
|
||||
});
|
||||
|
||||
test('Resolving $refs to local $anchors', async function () {
|
||||
const service = new SchemaService.JSONSchemaService(newMockRequestService(), workspaceContext);
|
||||
|
||||
service.setSchemaContributions({
|
||||
schemas: {
|
||||
"https://example.com/schemas/address": {
|
||||
"$id": "https://example.com/schemas/address",
|
||||
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"street_address":
|
||||
{
|
||||
"$anchor": "street_address",
|
||||
"type": "string"
|
||||
},
|
||||
"city": { "type": "string" },
|
||||
"state": { "type": "string" }
|
||||
},
|
||||
"required": ["street_address", "city", "state"]
|
||||
},
|
||||
"https://example.com/schemas/customer": {
|
||||
"$id": "https://example.com/schemas/customer",
|
||||
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"first_name": { "type": "string" },
|
||||
"last_name": { "type": "string" },
|
||||
"street_address": { "$ref": "/schemas/address#street_address" },
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const fs = await service.getResolvedSchema('https://example.com/schemas/customer');
|
||||
assert.deepStrictEqual(fs?.schema.properties?.street_address, {
|
||||
type: 'string',
|
||||
$anchor: "street_address"
|
||||
});
|
||||
});
|
||||
|
||||
test('Resolving $refs to external $ids', async function () {
|
||||
const service = new SchemaService.JSONSchemaService(newMockRequestService(), workspaceContext);
|
||||
service.setSchemaContributions({
|
||||
|
|
Загрузка…
Ссылка в новой задаче