Skip to content

Commit 06ed0c6

Browse files
authored
feat: do not display min/max values for oas formats (#130)
* feat: do not display min/max values for oas formats * test: add * chore: codeclimate exclude
1 parent 951b5c5 commit 06ed0c6

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

src/utils/__tests__/getValidations.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,40 @@ describe('getValidations util', () => {
1818
});
1919
});
2020

21+
describe('when oas format is specified', () => {
22+
test('given default range, should not ignore both minimum and maximum values', () => {
23+
expect(
24+
getValidations({
25+
type: 'integer',
26+
format: 'int64',
27+
minimum: 0 - Math.pow(2, 63),
28+
maximum: Math.pow(2, 63) - 1,
29+
}),
30+
).toStrictEqual({ format: 'int64' });
31+
});
32+
33+
test('given customized range, should include changed values', () => {
34+
expect(
35+
getValidations({ type: 'integer', format: 'int32', minimum: 20, maximum: Math.pow(2, 31) - 1 }),
36+
).toStrictEqual({
37+
format: 'int32',
38+
minimum: 20,
39+
});
40+
41+
expect(
42+
getValidations({
43+
type: 'number',
44+
format: 'float',
45+
minimum: 0 - Math.pow(2, 128),
46+
maximum: Math.pow(2, 16) - 1,
47+
}),
48+
).toStrictEqual({
49+
format: 'float',
50+
maximum: Math.pow(2, 16) - 1,
51+
});
52+
});
53+
});
54+
2155
test('should support integer type', () => {
2256
expect(
2357
getValidations({

src/utils/getValidations.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,44 @@ const VALIDATION_TYPES = {
2525
array: ['additionalItems', 'minItems', 'maxItems', 'uniqueItems'],
2626
};
2727

28+
const OAS_FORMATS = {
29+
int32: {
30+
minimum: 0 - Math.pow(2, 31),
31+
maximum: Math.pow(2, 31) - 1,
32+
},
33+
int64: {
34+
minimum: 0 - Math.pow(2, 63),
35+
maximum: Math.pow(2, 63) - 1,
36+
},
37+
float: {
38+
minimum: 0 - Math.pow(2, 128),
39+
maximum: Math.pow(2, 128) - 1,
40+
},
41+
double: {
42+
minimum: 0 - Number.MAX_VALUE,
43+
maximum: Number.MAX_VALUE,
44+
},
45+
byte: {
46+
pattern: '^[\\w\\d+\\/=]*$',
47+
},
48+
};
49+
50+
function filterOutFormatValidations(values: Dictionary<unknown>) {
51+
const { format } = values;
52+
53+
if (typeof format !== 'string' || !(format in OAS_FORMATS)) return values;
54+
55+
const newValues = { ...values };
56+
57+
for (const [key, value] of Object.entries(OAS_FORMATS[format])) {
58+
if (value === newValues[key]) {
59+
delete newValues[key];
60+
}
61+
}
62+
63+
return newValues;
64+
}
65+
2866
function getDeprecatedValue(node: JSONSchema4): Optional<boolean> {
2967
if ('x-deprecated' in node) {
3068
return !!node['x-deprecated'];
@@ -48,9 +86,9 @@ function getTypeValidations(type: JSONSchema4TypeName | JSONSchema4TypeName[]):
4886
export const getValidations = (node: JSONSchema4): Dictionary<unknown> => {
4987
const extraValidations = node.type && getTypeValidations(node.type);
5088
const deprecated = getDeprecatedValue(node);
51-
return {
89+
return filterOutFormatValidations({
5290
..._pick(node, COMMON_VALIDATION_TYPES),
5391
...(extraValidations && _pick(node, extraValidations)),
5492
...(deprecated !== void 0 && { deprecated }),
55-
};
93+
});
5694
};

0 commit comments

Comments
 (0)