Skip to content

Commit e18a9bb

Browse files
committed
feat(error-check): add check for error message
1 parent c33572c commit e18a9bb

File tree

3 files changed

+119
-2
lines changed

3 files changed

+119
-2
lines changed

src/components/SchemaRow/SchemaRow.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,25 @@ export const SchemaRow: React.FunctionComponent<SchemaRowProps> = React.memo(({
6969
const deprecated = isRegularNode(schemaNode) && schemaNode.deprecated;
7070
const validations = isRegularNode(schemaNode) ? schemaNode.validations : {};
7171
const hasProperties = useHasProperties({ required, deprecated, validations });
72+
let isInternalSchemaError = false;
73+
let internalSchemaError = 'You do not have permission to view this reference';
74+
75+
if ('fragment' in schemaNode) {
76+
if ('x-sl-error-message' in schemaNode.fragment) {
77+
isInternalSchemaError = true;
78+
internalSchemaError = schemaNode.fragment['x-sl-error-message'];
79+
}
80+
if (
81+
'primaryType' in schemaNode &&
82+
'items' in schemaNode.fragment &&
83+
schemaNode.primaryType === SchemaNodeKind.Array &&
84+
schemaNode.fragment.items !== null &&
85+
'x-sl-error-message' in (schemaNode.fragment.items as any)
86+
) {
87+
isInternalSchemaError = true;
88+
internalSchemaError = (schemaNode.fragment?.items as any)['x-sl-error-message'];
89+
}
90+
}
7291

7392
return (
7493
<>
@@ -158,9 +177,14 @@ export const SchemaRow: React.FunctionComponent<SchemaRowProps> = React.memo(({
158177
hideExamples={hideExamples}
159178
/>
160179

161-
{isBrokenRef && (
180+
{(isBrokenRef || isInternalSchemaError) && (
162181
// TODO (JJ): Add mosaic tooltip showing ref error
163-
<Icon title={refNode!.error!} color="danger" icon={faExclamationTriangle} size="sm" />
182+
<Icon
183+
title={refNode?.error! || internalSchemaError}
184+
color="danger"
185+
icon={faExclamationTriangle}
186+
size="sm"
187+
/>
164188
)}
165189
</VStack>
166190

src/components/SchemaRow/TopLevelSchemaRow.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Box, Flex, HStack, Icon, Menu, Pressable } from '@stoplight/mosaic';
44
import { useUpdateAtom } from 'jotai/utils';
55
import * as React from 'react';
66

7+
import { faExclamationTriangle } from '../../../node_modules/@fortawesome/free-solid-svg-icons/index';
78
import { COMBINER_NAME_MAP } from '../../consts';
89
import { useIsOnScreen } from '../../hooks/useIsOnScreen';
910
import { calculateChildrenToShow, isComplexArray } from '../../tree';
@@ -16,13 +17,24 @@ export const TopLevelSchemaRow = ({ schemaNode }: Pick<SchemaRowProps, 'schemaNo
1617
const { selectedChoice, setSelectedChoice, choices } = useChoices(schemaNode);
1718
const childNodes = React.useMemo(() => calculateChildrenToShow(selectedChoice.type), [selectedChoice.type]);
1819
const nestingLevel = 0;
20+
let isInternalSchemaError = false;
21+
let internalSchemaError = 'You do not have permission to view this reference';
22+
23+
if ('fragment' in schemaNode && 'x-sl-error-message' in schemaNode.fragment) {
24+
isInternalSchemaError = true;
25+
internalSchemaError = schemaNode.fragment['x-sl-error-message'];
26+
}
1927

2028
// regular objects are flattened at the top level
2129
if (isRegularNode(schemaNode) && isPureObjectNode(schemaNode)) {
2230
return (
2331
<>
2432
<ScrollCheck />
2533
<ChildStack schemaNode={schemaNode} childNodes={childNodes} currentNestingLevel={nestingLevel} />
34+
{isInternalSchemaError && (
35+
// TODO (JJ): Add mosaic tooltip showing ref error
36+
<Icon title={internalSchemaError} color="danger" icon={faExclamationTriangle} size="sm" />
37+
)}
2638
</>
2739
);
2840
}

src/components/__tests__/SchemaRow.spec.tsx

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,87 @@ describe('SchemaRow component', () => {
4949
});
5050
});
5151

52+
describe('resolving permission error', () => {
53+
let tree: RootNode;
54+
let schema: JSONSchema4;
55+
56+
it('given an object schema is marked as internal, a permission denied error messsage should be shown', () => {
57+
schema = {
58+
type: 'object',
59+
'x-sl-internally-excluded': true,
60+
'x-sl-error-message': 'You do not have permission to view this reference',
61+
};
62+
tree = buildTree(schema);
63+
const wrapper = mount(<SchemaRow schemaNode={tree.children[0]!} nestingLevel={0} />);
64+
expect(wrapper.find(Icon).at(0)).toHaveProp('title', `You do not have permission to view this reference`);
65+
wrapper.unmount();
66+
});
67+
68+
it('given a number schema is marked as internal, a permission denied error messsage should be shown', () => {
69+
schema = {
70+
type: 'number',
71+
'x-sl-internally-excluded': true,
72+
'x-sl-error-message': 'You do not have permission to view this reference',
73+
};
74+
tree = buildTree(schema);
75+
const wrapper = mount(<SchemaRow schemaNode={tree.children[0]!} nestingLevel={0} />);
76+
expect(wrapper.find(Icon).at(0)).toHaveProp('title', `You do not have permission to view this reference`);
77+
wrapper.unmount();
78+
});
79+
80+
it('given an integer schema is marked as internal, a permission denied error messsage should be shown', () => {
81+
schema = {
82+
type: 'integer',
83+
'x-sl-internally-excluded': true,
84+
'x-sl-error-message': 'You do not have permission to view this reference',
85+
};
86+
tree = buildTree(schema);
87+
const wrapper = mount(<SchemaRow schemaNode={tree.children[0]!} nestingLevel={0} />);
88+
expect(wrapper.find(Icon).at(0)).toHaveProp('title', `You do not have permission to view this reference`);
89+
wrapper.unmount();
90+
});
91+
92+
it('given a string schema is marked as internal, a permission denied error messsage should be shown', () => {
93+
schema = {
94+
type: 'string',
95+
'x-sl-internally-excluded': true,
96+
'x-sl-error-message': 'You do not have permission to view this reference',
97+
};
98+
tree = buildTree(schema);
99+
const wrapper = mount(<SchemaRow schemaNode={tree.children[0]!} nestingLevel={0} />);
100+
expect(wrapper.find(Icon).at(0)).toHaveProp('title', `You do not have permission to view this reference`);
101+
wrapper.unmount();
102+
});
103+
104+
it('given a boolean schema is marked as internal, a permission denied error messsage should be shown', () => {
105+
schema = {
106+
type: 'boolean',
107+
'x-sl-internally-excluded': true,
108+
'x-sl-error-message': 'You do not have permission to view this reference',
109+
};
110+
tree = buildTree(schema);
111+
const wrapper = mount(<SchemaRow schemaNode={tree.children[0]!} nestingLevel={0} />);
112+
expect(wrapper.find(Icon).at(0)).toHaveProp('title', `You do not have permission to view this reference`);
113+
wrapper.unmount();
114+
});
115+
116+
it('given an array schema is marked as internal, a permission denied error messsage should be shown', () => {
117+
schema = {
118+
title: 'test',
119+
type: 'array',
120+
items: {
121+
type: 'array',
122+
'x-sl-internally-excluded': true,
123+
'x-sl-error-message': 'You do not have permission to view this reference',
124+
},
125+
};
126+
tree = buildTree(schema);
127+
const wrapper = mount(<SchemaRow schemaNode={tree.children[0]!} nestingLevel={0} />);
128+
expect(wrapper.find(Icon).at(0)).toHaveProp('title', `You do not have permission to view this reference`);
129+
wrapper.unmount();
130+
});
131+
});
132+
52133
describe('required property', () => {
53134
let schema: JSONSchema4;
54135

0 commit comments

Comments
 (0)