Skip to content

Commit

Permalink
fix: render unknown properties
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Feb 27, 2020
1 parent 13ffc48 commit f923926
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
27 changes: 23 additions & 4 deletions src/tree/__tests__/populateTree.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Tree } from '@stoplight/tree-list';
import { Tree, TreeListParentNode } from '@stoplight/tree-list';
import * as fs from 'fs';
import { JSONSchema4 } from 'json-schema';
import * as path from 'path';
import { generateId } from '../../utils/generateId';
import { getNodeMetadata } from '../metadata';
import { populateTree } from '../utils/populateTree';

const BASE_PATH = path.resolve(__dirname, '../../__fixtures__/');
Expand Down Expand Up @@ -47,9 +49,9 @@ describe('populateTree util', () => {
});

it('given schema with complex types, throws', () => {
const schema = {
const schema: JSONSchema4 = {
type: [
'null',
'null' as any,
{
type: 'object',
properties: {
Expand All @@ -63,8 +65,25 @@ describe('populateTree util', () => {
};

const root = Tree.createArtificialRoot();
expect(() => populateTree(schema as any, root, 0, [], null)).toThrow(
expect(() => populateTree(schema, root, 0, [], null)).toThrow(
'The "type" property must be a string, or an array of strings. Objects and array of objects are not valid.',
);
});

it('includes properties with unknown types', () => {
const schema: JSONSchema4 = {
type: 'object',
properties: {
foo: {
__ERROR__: 'dd',
},
},
};

const root = Tree.createArtificialRoot();
populateTree(schema, root, 0, [], null);
expect(getNodeMetadata((root.children[0] as TreeListParentNode).children[0])).toHaveProperty('schema', {
__ERROR__: 'dd',
});
});
});
6 changes: 6 additions & 0 deletions src/tree/utils/walk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ function processNode(node: JSONSchema4): SchemaNode | void {
// if ('not' in node) {
// // todo: shall we support it?
// }

return {
id: generateId(),
validations: {},
annotations: {},
};
}

export type WalkerValue = {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface ICombinerNode {
export interface IBaseNode extends Pick<JSONSchema4, 'enum'> {
id: string;
readonly type?: JSONSchema4TypeName | JSONSchema4TypeName[];
annotations: Pick<JSONSchema4, JSONSchema4Annotations>;
annotations: Partial<Pick<JSONSchema4, JSONSchema4Annotations>>;
validations: Dictionary<unknown>;
required?: string[];
}
Expand Down

0 comments on commit f923926

Please sign in to comment.