-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathSchemaPropertyView.tsx
174 lines (164 loc) · 5.18 KB
/
SchemaPropertyView.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import React, { ReactNode } from "react";
import {
Text,
TextContent,
TextVariants,
Flex,
FlexItem,
Label,
LabelGroup,
Level,
ExpandableSection,
CodeBlock,
CodeBlockCode,
} from "@patternfly/react-core";
import { OpenAPIV3 } from "openapi-types";
interface PropertyViewComponentProps {
propSchema?: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject;
propName: string;
propertyType: ReactNode;
required: boolean | undefined;
}
export const PropertyView: React.FunctionComponent<
PropertyViewComponentProps
> = ({ propSchema, propName, propertyType, required }) => {
let extraProps;
let extraExample;
if (propSchema && !("$ref" in propSchema)) {
extraProps = <ExtraPropertyView propSchema={propSchema} />;
if(propSchema.example) {
extraExample = <ExtraExampleView propSchema={propSchema} />
}
}
return (
<Flex>
<FlexItem>
<TextContent>
<Flex>
<FlexItem className="pf-u-mr-xs">
<Text component={TextVariants.h6}>{propName}</Text>
</FlexItem>
<FlexItem>
<Text
component={TextVariants.p}
className="pf-u-danger-color-100"
>
{required && "*"}
</Text>
</FlexItem>
<FlexItem>
<Text
component={TextVariants.p}>
{propertyType}
</Text>
</FlexItem>
</Flex>
<Flex>
<FlexItem>
{extraExample}
</FlexItem>
</Flex>
</TextContent>
</FlexItem>
<FlexItem>{extraProps}</FlexItem>
</Flex>
);
};
interface ExtraExampleViewProps {
propSchema: OpenAPIV3.SchemaObject;
}
export const ExtraExampleView: React.FunctionComponent<
ExtraExampleViewProps
> = ({ propSchema }) => {
return (
<><Flex>
{propSchema.description && (
<Level>
<Text>{propSchema.description}</Text>
</Level>
)}
</Flex>
<ExpandableSection toggleText="Example">
<CodeBlock>
<CodeBlockCode id="code-content">{propSchema.example}</CodeBlockCode>
</CodeBlock>
</ExpandableSection>
</>
)
}
interface ExtraPropertyViewProps {
propSchema: OpenAPIV3.SchemaObject;
}
export const ExtraPropertyView: React.FunctionComponent<
ExtraPropertyViewProps
> = ({ propSchema }) => {
let maxMin: string | undefined;
if (propSchema.maximum && propSchema.minimum) {
maxMin =
(propSchema.exclusiveMinimum ? ">" : "≥") + ` ${propSchema.minimum} and `;
maxMin +=
(propSchema.exclusiveMaximum ? "<" : "≤") + ` ${propSchema.maximum}`;
} else if (propSchema.maximum) {
maxMin =
(propSchema.exclusiveMaximum ? "<" : "≤") + ` ${propSchema.maximum}`;
} else if (propSchema.minimum) {
maxMin =
(propSchema.exclusiveMinimum ? ">" : "≥") + ` ${propSchema.minimum}`;
}
let maxMinChar: string | undefined;
if (propSchema.maxLength && propSchema.minLength) {
maxMinChar = `${propSchema.minLength} to ${propSchema.maxLength} chars`;
} else if (propSchema.maxLength) {
maxMinChar = `max ${propSchema.maxLength} chars`;
} else if (propSchema.minLength) {
maxMinChar = `min ${propSchema.minLength} chars`;
}
let maxMinItems: string | undefined;
if (propSchema.maxItems && propSchema.minItems) {
maxMinItems = `${propSchema.minItems} to ${propSchema.maxItems} items`;
} else if (propSchema.maxItems) {
maxMinItems = `max ${propSchema.maxItems} items`;
} else if (propSchema.minItems) {
maxMinItems = `min ${propSchema.minItems} items`;
}
let maxMinProps: string | undefined;
if (propSchema.maxProperties && propSchema.minProperties) {
maxMinProps = `${propSchema.minProperties} to ${propSchema.maxProperties} properties`;
} else if (propSchema.maxProperties) {
maxMinProps = `max ${propSchema.maxProperties} properties`;
} else if (propSchema.minItems) {
maxMinProps = `min ${propSchema.minProperties} properties`;
}
return (
<LabelGroup>
{propSchema.format && <Label isCompact>{propSchema.format}</Label>}
{propSchema.default && (
<Label isCompact>default: {propSchema.default}</Label>
)}
{propSchema.enum && (
<LabelGroup categoryName="Enums">
{propSchema.enum.map((e) => (
<Label key={e} isCompact>
{e}
</Label>
))}
</LabelGroup>
)}
{propSchema.pattern && (
<Label isCompact>pattern: {propSchema.pattern}</Label>
)}
{propSchema.multipleOf && (
<Label isCompact>multipleOf: {propSchema.multipleOf}</Label>
)}
{maxMin && <Label isCompact>{maxMin}</Label>}
{maxMinChar && <Label isCompact>{maxMinChar}</Label>}
{maxMinItems && <Label isCompact>{maxMinItems}</Label>}
{maxMinProps && <Label isCompact>{maxMinProps}</Label>}
{propSchema.uniqueItems && <Label isCompact>unique</Label>}
{propSchema.nullable && <Label isCompact>nullable</Label>}
{propSchema.readOnly && <Label isCompact>read only</Label>}
{propSchema.writeOnly && <Label isCompact>write only</Label>}
{propSchema.deprecated && <Label isCompact>deprecated</Label>}
</LabelGroup>
);
};