Skip to content

Commit bef329c

Browse files
committed
fix(nested-definitions): add definitions availability for nested definitions
1 parent 3cf35bf commit bef329c

File tree

3 files changed

+117
-8
lines changed

3 files changed

+117
-8
lines changed

src/index.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,26 @@ const handleDefinition = async <T>(
3131
if (type) {
3232
// Walk just the definitions types
3333
const walker = new Walker<T>();
34-
await walker.loadSchema({ ...def, $schema: schema['$schema'] } as any, {
35-
dereference: true,
36-
cloneSchema: true,
37-
dereferenceOptions: {
38-
dereference: {
39-
circular: 'ignore',
34+
await walker.loadSchema(
35+
{
36+
definitions: schema['definitions'] || [],
37+
...def,
38+
$schema: schema['$schema'],
39+
} as any,
40+
{
41+
dereference: true,
42+
cloneSchema: true,
43+
dereferenceOptions: {
44+
dereference: {
45+
circular: 'ignore',
46+
},
4047
},
41-
},
42-
});
48+
}
49+
);
4350
await walker.walk(convertSchema, walker.vocabularies.DRAFT_07);
51+
if ('definitions' in walker.rootSchema) {
52+
delete (<any>walker.rootSchema).definitions;
53+
}
4454
return walker.rootSchema;
4555
} else if (Array.isArray(def)) {
4656
// if it's an array, we might want to reconstruct the type;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Vitest Snapshot v1
2+
3+
exports[`throws an error when dereferecing fails 1`] = `
4+
{
5+
"additionalProperties": false,
6+
"definitions": {
7+
"configvariable": {
8+
"additionalProperties": false,
9+
"properties": {
10+
"default": {
11+
"type": "string",
12+
},
13+
"name": {
14+
"pattern": "^[A-Z_]+[A-Z0-9_]*$",
15+
"type": "string",
16+
},
17+
"required": {
18+
"default": true,
19+
"type": "boolean",
20+
},
21+
},
22+
"required": [
23+
"name",
24+
],
25+
"type": "object",
26+
},
27+
"envVarName": {
28+
"pattern": "^[A-Z_]+[A-Z0-9_]*$",
29+
"type": "string",
30+
},
31+
},
32+
"properties": {
33+
"componentId": {
34+
"pattern": "^(.*)$",
35+
"title": "The component id Schema",
36+
"type": "string",
37+
},
38+
"configurationTemplate": {
39+
"items": {
40+
"$ref": "#/definitions/configvariable",
41+
},
42+
"title": "The Configurationtemplate Schema",
43+
"type": "array",
44+
},
45+
},
46+
"required": [
47+
"componentId",
48+
],
49+
"title": "Component Manifest Schema",
50+
"type": "object",
51+
}
52+
`;

test/dereference_schema.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,50 @@ it('throws an error when dereferecing fails', async ({ expect }) => {
264264

265265
expect(error).have.property('ioErrorCode', 'ENOENT');
266266
});
267+
268+
it('throws an error when dereferecing fails', async ({ expect }) => {
269+
const schema = {
270+
definitions: {
271+
envVarName: {
272+
type: 'string',
273+
pattern: '^[A-Z_]+[A-Z0-9_]*$',
274+
},
275+
configvariable: {
276+
type: 'object',
277+
properties: {
278+
name: { $ref: '#/definitions/envVarName' },
279+
default: { type: 'string' },
280+
required: { type: 'boolean', default: true },
281+
},
282+
required: ['name'],
283+
additionalProperties: false,
284+
},
285+
},
286+
$schema: 'http://json-schema.org/draft-07/schema#',
287+
$id: 'http://example.com/root.json',
288+
type: 'object',
289+
title: 'Component Manifest Schema',
290+
required: ['componentId'],
291+
additionalProperties: false,
292+
properties: {
293+
componentId: {
294+
$id: '#/properties/componentId',
295+
type: 'string',
296+
title: 'The component id Schema',
297+
pattern: '^(.*)$',
298+
},
299+
configurationTemplate: {
300+
$id: '#/properties/configurationTemplate',
301+
type: 'array',
302+
title: 'The Configurationtemplate Schema',
303+
items: {
304+
$ref: '#/definitions/configvariable',
305+
},
306+
},
307+
},
308+
};
309+
310+
const result = await convert(schema);
311+
312+
expect(result).toMatchSnapshot();
313+
});

0 commit comments

Comments
 (0)