Skip to content

Commit 77fa111

Browse files
committed
feat: add required
1 parent 9d91914 commit 77fa111

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

src/components/SchemaRow.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export const SchemaRow: React.FunctionComponent<ISchemaRow> = ({ className, node
2323
throw new Error('Missing metadata');
2424
}
2525

26-
const schemaNode = metadata.schema;
26+
const { path, schema: schemaNode } = metadata;
27+
28+
const parentSchemaNode =
29+
node.parent === null || !(node.parent.id in MetadataStore) ? null : MetadataStore[node.parent.id].schema;
2730
const description = 'annotations' in schemaNode ? schemaNode.annotations.description : null;
2831

2932
return (
@@ -46,16 +49,24 @@ export const SchemaRow: React.FunctionComponent<ISchemaRow> = ({ className, node
4649
/>
4750
)}
4851

49-
{'combiner' in schemaNode && <Divider kind={schemaNode.combiner} />}
52+
{node.parent !== null &&
53+
node.parent.children.length > 0 &&
54+
parentSchemaNode !== null &&
55+
'combiner' in parentSchemaNode &&
56+
node.parent.children[0] !== node && <Divider kind={parentSchemaNode.combiner} />}
5057

5158
<div className="flex-1 flex truncate">
5259
<Property node={schemaNode} path={metadata.path} onGoToRef={onGoToRef} />
5360
{description && <Description value={description} />}
5461
</div>
5562

5663
<Validations
57-
required={/* todo: implement me */ false}
58-
// required={!!schemaNode.required}
64+
required={
65+
parentSchemaNode !== null &&
66+
'required' in parentSchemaNode &&
67+
Array.isArray(parentSchemaNode.required) &&
68+
parentSchemaNode.required.includes(String(path[path.length - 1]))
69+
}
5970
validations={{
6071
...('annotations' in schemaNode &&
6172
schemaNode.annotations.default && { default: schemaNode.annotations.default }),

src/tree/walk.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getCombiner } from '../utils/getCombiner';
77
import { getPrimaryType } from '../utils/getPrimaryType';
88
import { getValidations } from '../utils/getValidations';
99
import { inferType } from '../utils/inferType';
10+
import { normalizeRequired } from '../utils/normalizeRequired';
1011

1112
function assignNodeSpecificFields(base: IBaseNode, node: JSONSchema4) {
1213
switch (getPrimaryType(node)) {
@@ -43,6 +44,7 @@ function processNode(node: JSONSchema4): SchemaNode | void {
4344
type: flattenTypes(type),
4445
validations: getValidations(node),
4546
annotations: getAnnotations(node),
47+
...('required' in node && { required: normalizeRequired(node.required) }),
4648
enum: node.enum,
4749
};
4850

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface IBaseNode extends Pick<JSONSchema4, 'enum'> {
3333
readonly type?: JSONSchema4TypeName | JSONSchema4TypeName[];
3434
annotations: Pick<JSONSchema4, JSONSchema4Annotations>;
3535
validations: Dictionary<unknown>;
36+
required?: string[];
3637
}
3738

3839
export interface IRefNode {

src/utils/normalizeRequired.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Optional } from '@stoplight/types';
2+
3+
export const normalizeRequired = (required: unknown): Optional<string[]> => {
4+
if (!Array.isArray(required)) return;
5+
return required.filter(item => typeof item === 'string' || typeof item === 'number').map(String);
6+
}

0 commit comments

Comments
 (0)