From fafe4075440801312b0687d63a26d2d48347538b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Thu, 8 Apr 2021 01:50:09 +0200 Subject: [PATCH 1/3] feat: do not display min/max values for oas formats --- src/components/__tests__/Validations.spec.tsx | 2 + src/utils/getValidations.ts | 42 ++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/components/__tests__/Validations.spec.tsx b/src/components/__tests__/Validations.spec.tsx index 5bca3e7d..035fed52 100644 --- a/src/components/__tests__/Validations.spec.tsx +++ b/src/components/__tests__/Validations.spec.tsx @@ -32,6 +32,8 @@ describe('Validations component', () => { expect(wrapper).toHaveText('deprecated'); }); }); + + test.todo('when oas format is specified'); }); describe('Format', () => { diff --git a/src/utils/getValidations.ts b/src/utils/getValidations.ts index 1854c636..1dff0be3 100644 --- a/src/utils/getValidations.ts +++ b/src/utils/getValidations.ts @@ -25,6 +25,44 @@ const VALIDATION_TYPES = { array: ['additionalItems', 'minItems', 'maxItems', 'uniqueItems'], }; +const OAS_FORMATS = { + int32: { + minimum: 0 - Math.pow(2, 31), + maximum: Math.pow(2, 31) - 1, + }, + int64: { + minimum: 0 - Math.pow(2, 63), + maximum: Math.pow(2, 63) - 1, + }, + float: { + minimum: 0 - Math.pow(2, 128), + maximum: Math.pow(2, 128) - 1, + }, + double: { + minimum: 0 - Number.MAX_VALUE, + maximum: Number.MAX_VALUE, + }, + byte: { + pattern: '^[\\w\\d+\\/=]*$', + }, +}; + +function filterOutMaxAndMin(values: Dictionary) { + const { format } = values; + + if (typeof format !== 'string' || !(format in OAS_FORMATS)) return values; + + const newValues = { ...values }; + + for (const [key, value] of Object.entries(OAS_FORMATS[format])) { + if (value === newValues[key]) { + delete newValues[key]; + } + } + + return newValues; +} + function getDeprecatedValue(node: JSONSchema4): Optional { if ('x-deprecated' in node) { return !!node['x-deprecated']; @@ -48,9 +86,9 @@ function getTypeValidations(type: JSONSchema4TypeName | JSONSchema4TypeName[]): export const getValidations = (node: JSONSchema4): Dictionary => { const extraValidations = node.type && getTypeValidations(node.type); const deprecated = getDeprecatedValue(node); - return { + return filterOutMaxAndMin({ ..._pick(node, COMMON_VALIDATION_TYPES), ...(extraValidations && _pick(node, extraValidations)), ...(deprecated !== void 0 && { deprecated }), - }; + }); }; From d4b7a11b47227ceb39691b82ca09a4b043462e64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Thu, 8 Apr 2021 11:37:01 +0200 Subject: [PATCH 2/3] test: add --- src/components/__tests__/Validations.spec.tsx | 2 -- src/utils/__tests__/getValidations.spec.ts | 34 +++++++++++++++++++ src/utils/getValidations.ts | 4 +-- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/components/__tests__/Validations.spec.tsx b/src/components/__tests__/Validations.spec.tsx index 035fed52..5bca3e7d 100644 --- a/src/components/__tests__/Validations.spec.tsx +++ b/src/components/__tests__/Validations.spec.tsx @@ -32,8 +32,6 @@ describe('Validations component', () => { expect(wrapper).toHaveText('deprecated'); }); }); - - test.todo('when oas format is specified'); }); describe('Format', () => { diff --git a/src/utils/__tests__/getValidations.spec.ts b/src/utils/__tests__/getValidations.spec.ts index a8acf845..ecf373fb 100644 --- a/src/utils/__tests__/getValidations.spec.ts +++ b/src/utils/__tests__/getValidations.spec.ts @@ -18,6 +18,40 @@ describe('getValidations util', () => { }); }); + describe('when oas format is specified', () => { + test('given default range, should not ignore both minimum and maximum values', () => { + expect( + getValidations({ + type: 'integer', + format: 'int64', + minimum: 0 - Math.pow(2, 63), + maximum: Math.pow(2, 63) - 1, + }), + ).toStrictEqual({ format: 'int64' }); + }); + + test('given customized range, should include changed values', () => { + expect( + getValidations({ type: 'integer', format: 'int32', minimum: 20, maximum: Math.pow(2, 31) - 1 }), + ).toStrictEqual({ + format: 'int32', + minimum: 20, + }); + + expect( + getValidations({ + type: 'number', + format: 'float', + minimum: 0 - Math.pow(2, 128), + maximum: Math.pow(2, 16) - 1, + }), + ).toStrictEqual({ + format: 'float', + maximum: Math.pow(2, 16) - 1, + }); + }); + }); + test('should support integer type', () => { expect( getValidations({ diff --git a/src/utils/getValidations.ts b/src/utils/getValidations.ts index 1dff0be3..4f3c4c41 100644 --- a/src/utils/getValidations.ts +++ b/src/utils/getValidations.ts @@ -47,7 +47,7 @@ const OAS_FORMATS = { }, }; -function filterOutMaxAndMin(values: Dictionary) { +function filterOutFormatValidations(values: Dictionary) { const { format } = values; if (typeof format !== 'string' || !(format in OAS_FORMATS)) return values; @@ -86,7 +86,7 @@ function getTypeValidations(type: JSONSchema4TypeName | JSONSchema4TypeName[]): export const getValidations = (node: JSONSchema4): Dictionary => { const extraValidations = node.type && getTypeValidations(node.type); const deprecated = getDeprecatedValue(node); - return filterOutMaxAndMin({ + return filterOutFormatValidations({ ..._pick(node, COMMON_VALIDATION_TYPES), ...(extraValidations && _pick(node, extraValidations)), ...(deprecated !== void 0 && { deprecated }), From ad6b371312e376439509252e8d0da88f92884004 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Thu, 8 Apr 2021 11:46:12 +0200 Subject: [PATCH 3/3] chore: codeclimate exclude