From 689fe64b2d972532e71c5cebed2b5260110ede75 Mon Sep 17 00:00:00 2001 From: nanov Date: Sat, 12 Oct 2019 17:10:08 +0300 Subject: [PATCH 1/7] feat: visualise 'multipleOf' property --- src/utils/openapi.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/utils/openapi.ts b/src/utils/openapi.ts index 2836cfedc2..c2f1b0b48e 100644 --- a/src/utils/openapi.ts +++ b/src/utils/openapi.ts @@ -368,6 +368,14 @@ export function isNamedDefinition(pointer?: string): boolean { return /^#\/components\/schemas\/[^\/]+$/.test(pointer || ''); } +function humanizeMultipleOfConstraint(multipleOf: number | undefined): string | undefined { + if (multipleOf === undefined) return; + const strigifiedMultipleOf = multipleOf.toString(10); + const parts = strigifiedMultipleOf.split('.'); + if (parts.length < 2 || !/^0*1$/.test(parts[1])) return `multiple of ${strigifiedMultipleOf}`; + return `decimals <= ${parts[1].length} digits`; +} + function humanizeRangeConstraint( description: string, min: number | undefined, @@ -406,6 +414,11 @@ export function humanizeConstraints(schema: OpenAPISchema): string[] { res.push(arrayRange); } + const multipleOfConstraint = humanizeMultipleOfConstraint(schema.multipleOf); + if (multipleOfConstraint !== undefined) { + res.push(multipleOfConstraint); + } + let numberRange; if (schema.minimum !== undefined && schema.maximum !== undefined) { numberRange = schema.exclusiveMinimum ? '( ' : '[ '; From b9b07cde0f8bc5d3ff2cc86940e20314f560281c Mon Sep 17 00:00:00 2001 From: nanov Date: Sat, 12 Oct 2019 17:12:26 +0300 Subject: [PATCH 2/7] add 'multipleOf' to honeyPerDay --- demo/openapi.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/demo/openapi.yaml b/demo/openapi.yaml index b7c1833a02..6f73eb28b8 100644 --- a/demo/openapi.yaml +++ b/demo/openapi.yaml @@ -729,6 +729,7 @@ components: type: number description: Average amount of honey produced per day in ounces example: 3.14 + multipleOf: .01 required: - honeyPerDay Id: From 2855fca34f6ac53cf2029f393bad46891b1918dc Mon Sep 17 00:00:00 2001 From: nanov Date: Sat, 12 Oct 2019 17:38:37 +0300 Subject: [PATCH 3/7] write tests for 'multipleOf' constrain --- src/utils/__tests__/openapi.test.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/utils/__tests__/openapi.test.ts b/src/utils/__tests__/openapi.test.ts index 244ab63c22..fbc56ea296 100644 --- a/src/utils/__tests__/openapi.test.ts +++ b/src/utils/__tests__/openapi.test.ts @@ -333,7 +333,8 @@ describe('Utils', () => { const itemConstraintSchema = ( min: number | undefined = undefined, max: number | undefined = undefined, - ) => ({ type: 'array', minItems: min, maxItems: max }); + multipleOf: number | undefined = undefined, + ) => ({ type: 'array', minItems: min, maxItems: max, multipleOf }); it('should not have a humanized constraint without schema constraints', () => { expect(humanizeConstraints(itemConstraintSchema())).toHaveLength(0); @@ -358,6 +359,18 @@ describe('Utils', () => { it('should have a humazined constraint when justMinItems is set, and it is equal to 1', () => { expect(humanizeConstraints(itemConstraintSchema(1))).toContain('non-empty'); }); + + it('should have a humazined constraint when multipleOf is set, and it is in format of /^0\\.0*1$/', () => { + expect(humanizeConstraints(itemConstraintSchema(undefined, undefined, 0.01))).toContain( + 'decimals <= 2 digits', + ); + }); + + it('should have a humazined constraint when multipleOf is set, and it is in format other than /^0\\.0*1$/', () => { + expect(humanizeConstraints(itemConstraintSchema(undefined, undefined, 0.5))).toContain( + 'multiple of 0.5', + ); + }); }); describe('OpenAPI pluralizeType', () => { @@ -387,7 +400,9 @@ describe('Utils', () => { expect(pluralizeType('objects (Pet)')).toEqual('objects (Pet)'); expect(pluralizeType('strings ')).toEqual('strings '); expect(pluralizeType('objects or strings')).toEqual('objects or strings'); - expect(pluralizeType('objects (Pet) or numbers ')).toEqual('objects (Pet) or numbers '); + expect(pluralizeType('objects (Pet) or numbers ')).toEqual( + 'objects (Pet) or numbers ', + ); }); }); From 383a41d3a00c36bcbde40d47dba163083e886bc7 Mon Sep 17 00:00:00 2001 From: nanov Date: Sat, 12 Oct 2019 17:39:17 +0300 Subject: [PATCH 4/7] refactor contrats check --- src/utils/openapi.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/utils/openapi.ts b/src/utils/openapi.ts index c2f1b0b48e..bcf17d3ebe 100644 --- a/src/utils/openapi.ts +++ b/src/utils/openapi.ts @@ -369,11 +369,14 @@ export function isNamedDefinition(pointer?: string): boolean { } function humanizeMultipleOfConstraint(multipleOf: number | undefined): string | undefined { - if (multipleOf === undefined) return; + if (multipleOf === undefined) { + return; + } const strigifiedMultipleOf = multipleOf.toString(10); - const parts = strigifiedMultipleOf.split('.'); - if (parts.length < 2 || !/^0*1$/.test(parts[1])) return `multiple of ${strigifiedMultipleOf}`; - return `decimals <= ${parts[1].length} digits`; + if (!/^0\.0*1$/.test(strigifiedMultipleOf)) { + return `multiple of ${multipleOf}`; + } + return `decimals <= ${strigifiedMultipleOf.split('.')[1].length} digits`; } function humanizeRangeConstraint( From d761b8f7a2ddf74bcf7f879b30b50d9c0a01ae34 Mon Sep 17 00:00:00 2001 From: nanov Date: Sun, 13 Oct 2019 07:29:34 +0300 Subject: [PATCH 5/7] fix typo in tests dscriptions --- src/utils/__tests__/openapi.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/__tests__/openapi.test.ts b/src/utils/__tests__/openapi.test.ts index fbc56ea296..9842f101fb 100644 --- a/src/utils/__tests__/openapi.test.ts +++ b/src/utils/__tests__/openapi.test.ts @@ -356,17 +356,17 @@ describe('Utils', () => { expect(humanizeConstraints(itemConstraintSchema(7, 7))).toContain('7 items'); }); - it('should have a humazined constraint when justMinItems is set, and it is equal to 1', () => { + it('should have a humanized constraint when justMinItems is set, and it is equal to 1', () => { expect(humanizeConstraints(itemConstraintSchema(1))).toContain('non-empty'); }); - it('should have a humazined constraint when multipleOf is set, and it is in format of /^0\\.0*1$/', () => { + it('should have a humanized constraint when multipleOf is set, and it is in format of /^0\\.0*1$/', () => { expect(humanizeConstraints(itemConstraintSchema(undefined, undefined, 0.01))).toContain( 'decimals <= 2 digits', ); }); - it('should have a humazined constraint when multipleOf is set, and it is in format other than /^0\\.0*1$/', () => { + it('should have a humanized constraint when multipleOf is set, and it is in format other than /^0\\.0*1$/', () => { expect(humanizeConstraints(itemConstraintSchema(undefined, undefined, 0.5))).toContain( 'multiple of 0.5', ); From b83a972b4a4369a0c4adbb3fa8daef973030ce70 Mon Sep 17 00:00:00 2001 From: nanov Date: Mon, 21 Oct 2019 10:29:29 +0300 Subject: [PATCH 6/7] change decimal places description --- src/utils/__tests__/openapi.test.ts | 2 +- src/utils/openapi.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/__tests__/openapi.test.ts b/src/utils/__tests__/openapi.test.ts index 9842f101fb..bcdfb1932d 100644 --- a/src/utils/__tests__/openapi.test.ts +++ b/src/utils/__tests__/openapi.test.ts @@ -362,7 +362,7 @@ describe('Utils', () => { it('should have a humanized constraint when multipleOf is set, and it is in format of /^0\\.0*1$/', () => { expect(humanizeConstraints(itemConstraintSchema(undefined, undefined, 0.01))).toContain( - 'decimals <= 2 digits', + 'decimal places <= 2', ); }); diff --git a/src/utils/openapi.ts b/src/utils/openapi.ts index bcf17d3ebe..55efc600f7 100644 --- a/src/utils/openapi.ts +++ b/src/utils/openapi.ts @@ -374,9 +374,9 @@ function humanizeMultipleOfConstraint(multipleOf: number | undefined): string | } const strigifiedMultipleOf = multipleOf.toString(10); if (!/^0\.0*1$/.test(strigifiedMultipleOf)) { - return `multiple of ${multipleOf}`; + return `multiple of ${strigifiedMultipleOf}`; } - return `decimals <= ${strigifiedMultipleOf.split('.')[1].length} digits`; + return `decimal places <= ${strigifiedMultipleOf.split('.')[1].length} digits`; } function humanizeRangeConstraint( From eadf65f56631d7c9fa8721e2eb48bb8267f2b9d2 Mon Sep 17 00:00:00 2001 From: nanov Date: Tue, 22 Oct 2019 09:11:26 +0300 Subject: [PATCH 7/7] remove 'digits' from description --- src/utils/openapi.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/openapi.ts b/src/utils/openapi.ts index 55efc600f7..e9657547bb 100644 --- a/src/utils/openapi.ts +++ b/src/utils/openapi.ts @@ -376,7 +376,7 @@ function humanizeMultipleOfConstraint(multipleOf: number | undefined): string | if (!/^0\.0*1$/.test(strigifiedMultipleOf)) { return `multiple of ${strigifiedMultipleOf}`; } - return `decimal places <= ${strigifiedMultipleOf.split('.')[1].length} digits`; + return `decimal places <= ${strigifiedMultipleOf.split('.')[1].length}`; } function humanizeRangeConstraint(