Skip to content

Commit 910c72b

Browse files
author
Jakub Jankowski
committed
fix: formatters
1 parent 819957b commit 910c72b

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

src/__tests__/__snapshots__/index.spec.tsx.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2847,7 +2847,7 @@ exports[`HTML Output should match default-schema.json 1`] = `
28472847
<span>20</span>
28482848
</div>
28492849
<div>
2850-
<span>Multiple of value:</span>
2850+
<span>Multiple of:</span>
28512851
<span>10</span>
28522852
</div>
28532853
<div style=\\"height: 1px\\"><div style=\\"height: 1px; background-color: lightgray\\"></div></div>
@@ -2867,8 +2867,8 @@ exports[`HTML Output should match default-schema.json 1`] = `
28672867
<span>required</span>
28682868
</div>
28692869
<div>
2870-
<span>Match pattern value:</span>
2871-
<span>\\"MM/dd/yyyy H:mm\\"</span>
2870+
<span>Match pattern:</span>
2871+
<span>MM/dd/yyyy H:mm</span>
28722872
</div>
28732873
<div style=\\"height: 1px\\"><div style=\\"height: 1px; background-color: lightgray\\"></div></div>
28742874
</div>

src/components/shared/Validations.tsx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,32 @@ const numberValidationFormatters: Record<string, (value: unknown) => string> = {
3737
maxLength: value => `<= ${value} characters`,
3838
};
3939

40-
const validationsFormatter = (name: string) => (value: unknown[] | unknown): ValidationFormat | null => {
40+
const createStringFormatter = (nowrap: boolean | undefined) => (value: unknown) => {
41+
return nowrap || typeof value !== 'string' ? String(value) : `"${value}"`;
42+
};
43+
44+
const createValidationsFormatter = (name: string, options?: { exact?: boolean; nowrap?: boolean }) => (
45+
value: unknown[] | unknown,
46+
): ValidationFormat | null => {
4147
const values = Array.isArray(value) ? value : [value];
4248
if (values.length) {
4349
return {
44-
name: values.length > 1 ? `${name} values` : `${name} value`,
45-
values: values.map(stringifyValue),
50+
name: options?.exact ? name : values.length > 1 ? `${name} values` : `${name} value`,
51+
values: values.map(createStringFormatter(options?.nowrap)),
4652
};
4753
}
4854
return null;
4955
};
5056

5157
const validationFormatters: Record<string, (value: unknown) => ValidationFormat | null> = {
52-
['const']: validationsFormatter('Allowed'),
53-
enum: validationsFormatter('Allowed'),
54-
examples: validationsFormatter('Example'),
55-
example: validationsFormatter('Example'),
56-
['x-example']: validationsFormatter('Example'),
57-
multipleOf: validationsFormatter('Multiple of'),
58-
pattern: validationsFormatter('Match pattern'),
59-
default: validationsFormatter('Default'),
58+
['const']: createValidationsFormatter('Allowed'),
59+
enum: createValidationsFormatter('Allowed'),
60+
examples: createValidationsFormatter('Example'),
61+
example: createValidationsFormatter('Example'),
62+
['x-example']: createValidationsFormatter('Example'),
63+
multipleOf: createValidationsFormatter('Multiple of', { exact: true }),
64+
pattern: createValidationsFormatter('Match pattern', { exact: true, nowrap: true }),
65+
default: createValidationsFormatter('Default'),
6066
};
6167

6268
export const Validations: React.FunctionComponent<IValidations> = ({ validations }) => {
@@ -125,7 +131,7 @@ const KeyValueValidation = ({ className, name, values }: { className?: string; n
125131
return (
126132
<Flex flexWrap color="muted" my={2} className={className}>
127133
<Text fontWeight="light">{capitalize(name)}:</Text>
128-
{values.map(value => (
134+
{uniq(values).map(value => (
129135
<Text key={value} ml={2} px={1} fontFamily="mono" border rounded="lg" className={className}>
130136
{value}
131137
</Text>
@@ -178,7 +184,3 @@ export function getValidationsFromSchema(schemaNode: RegularNode) {
178184
...schemaNode.validations,
179185
};
180186
}
181-
182-
function stringifyValue(value: unknown) {
183-
return typeof value === 'string' ? `"${value}"` : String(value);
184-
}

src/components/shared/__tests__/Validations.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('Validations component', () => {
2525
expect(wrapper).toIncludeText('>= 10<= 40');
2626
expect(wrapper).toIncludeText('Allowed values:10203040');
2727
expect(wrapper).toIncludeText('Default value:20');
28-
expect(wrapper).toIncludeText('Multiple of value:10');
28+
expect(wrapper).toIncludeText('Multiple of:10');
2929
expect(wrapper).toIncludeText('Example value:20');
3030
});
3131

0 commit comments

Comments
 (0)