Skip to content

Commit b7d90bb

Browse files
authored
feat: do not display min/max values for oas formats (#131)
1 parent 01724d4 commit b7d90bb

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

src/components/shared/Validations.tsx

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,42 @@ const validationFormatters: Record<string, (value: unknown) => ValidationFormat
6565
default: createValidationsFormatter('Default'),
6666
};
6767

68+
const oasFormats = {
69+
int32: {
70+
minimum: 0 - 2 ** 31,
71+
maximum: 2 ** 31 - 1,
72+
},
73+
int64: {
74+
minimum: 0 - 2 ** 63,
75+
maximum: 2 ** 63 - 1,
76+
},
77+
float: {
78+
minimum: 0 - 2 ** 128,
79+
maximum: 2 ** 128 - 1,
80+
},
81+
double: {
82+
minimum: 0 - Number.MAX_VALUE,
83+
maximum: Number.MAX_VALUE,
84+
},
85+
byte: {
86+
pattern: '^[\\w\\d+\\/=]*$',
87+
},
88+
};
89+
90+
function filterOutOasFormatValidations(format: string, values: Dictionary<unknown>) {
91+
if (!(format in oasFormats)) return values;
92+
93+
const newValues = { ...values };
94+
95+
for (const [key, value] of Object.entries(oasFormats[format])) {
96+
if (value === newValues[key]) {
97+
delete newValues[key];
98+
}
99+
}
100+
101+
return newValues;
102+
}
103+
68104
export const Validations: React.FunctionComponent<IValidations> = ({ validations }) => {
69105
const numberValidations = pick(validations, numberValidationNames);
70106
const booleanValidations = omit(
@@ -172,6 +208,14 @@ export function getValidationsFromSchema(schemaNode: RegularNode) {
172208
...(schemaNode.annotations['x-example'] ? { ['x-example']: schemaNode.annotations['x-example'] } : null),
173209
}
174210
: null),
175-
...schemaNode.validations,
211+
...getFilteredValidations(schemaNode),
176212
};
177213
}
214+
215+
function getFilteredValidations(schemaNode: RegularNode) {
216+
if (schemaNode.format !== null) {
217+
return filterOutOasFormatValidations(schemaNode.format, schemaNode.validations);
218+
}
219+
220+
return schemaNode.validations;
221+
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,34 @@ describe('Validations component', () => {
4747
expect(wrapper).toIncludeText('Example values:"Example 1""Example 2"');
4848
expect(wrapper).toIncludeText('Allowed value:"bar"');
4949
});
50+
51+
describe('OAS formats', () => {
52+
it('given default range, should not render any validation', () => {
53+
const node = new RegularNode({
54+
type: 'integer',
55+
format: 'int32',
56+
minimum: 0 - Math.pow(2, 31),
57+
maximum: Math.pow(2, 31) - 1,
58+
});
59+
60+
const validations = getValidationsFromSchema(node);
61+
const wrapper = mount(<Validations validations={validations} />);
62+
63+
expect(wrapper).toBeEmptyRender();
64+
});
65+
66+
it('should render non-standard values', () => {
67+
const node = new RegularNode({
68+
type: 'integer',
69+
format: 'int64',
70+
minimum: 0,
71+
maximum: Math.pow(2, 63) - 1,
72+
});
73+
74+
const validations = getValidationsFromSchema(node);
75+
const wrapper = mount(<Validations validations={validations} />);
76+
77+
expect(wrapper).toIncludeText('>= 0');
78+
});
79+
});
5080
});

0 commit comments

Comments
 (0)