From 8503e3cf6604242e1d62e7aaaffd7bc6214fe8b9 Mon Sep 17 00:00:00 2001 From: Jakob Guddas Date: Sun, 4 Jun 2023 12:22:46 +0200 Subject: [PATCH 1/3] fix: support maximum 0 in number field --- src/tests/createHeadlessForm.test.js | 24 ++++++++++++++++++++++++ src/tests/helpers.js | 17 +++++++++++++++++ src/yupSchema.js | 3 ++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/tests/createHeadlessForm.test.js b/src/tests/createHeadlessForm.test.js index 6fd16914d..698ea66fa 100644 --- a/src/tests/createHeadlessForm.test.js +++ b/src/tests/createHeadlessForm.test.js @@ -16,6 +16,7 @@ import { schemaInputTypeSelectMultiple, schemaInputTypeSelectMultipleOptional, schemaInputTypeNumber, + schemaInputTypeNumberZeroMaximum, schemaInputTypeDate, schemaInputTypeEmail, schemaInputWithStatement, @@ -780,6 +781,29 @@ describe('createHeadlessForm', () => { expect(() => fieldValidator.validateSync('')).toThrowError('The value must be a number'); }); + it('shows an error when positive number is entered into a "number" field with maximum set to zero', () => { + const result = createHeadlessForm(schemaInputTypeNumberZeroMaximum); + expect(result).toMatchObject({ + fields: [ + { + description: 'How many open tabs do you have?', + label: 'Tabs', + name: 'tabs', + required: false, + schema: expect.any(Object), + type: 'number', + minimum: -100, + maximum: 0, + }, + ], + }); + const fieldValidator = result.fields[0].schema; + expect(fieldValidator.isValidSync('0')).toBe(true); + expect(fieldValidator.isValidSync('10')).toBe(false); + expect(fieldValidator.isValidSync('-11')).toBe(true); + expect(() => fieldValidator.validateSync('5')).toThrowError('Must be smaller or equal to 0'); + }); + it('support "number" field type with the percentage attribute', () => { const result = createHeadlessForm(schemaInputTypeNumberWithPercentage); expect(result).toMatchObject({ diff --git a/src/tests/helpers.js b/src/tests/helpers.js index 32d02f613..bcd6348d0 100644 --- a/src/tests/helpers.js +++ b/src/tests/helpers.js @@ -46,6 +46,23 @@ export const mockNumberInput = { type: 'number', }; +export const mockNumberInputNegative = { + title: 'Tabs', + description: 'How many open tabs do you have?', + 'x-jsf-presentation': { + inputType: 'number', + }, + minimum: -100, + maximum: 0, + type: 'number', +}; + +export const schemaInputTypeNumberZeroMaximum = JSONSchemaBuilder() + .addInput({ + tabs: mockNumberInputNegative, + }) + .build(); + export const mockNumberInputWithPercentage = { title: 'Shares', description: 'What % of shares do you own?', diff --git a/src/yupSchema.js b/src/yupSchema.js index 362afc9e7..847706606 100644 --- a/src/yupSchema.js +++ b/src/yupSchema.js @@ -240,7 +240,8 @@ export function buildYupSchema(field, config) { validators.push(withMinLength); } - if (propertyFields.maximum) { + // support maximum with 0 value + if (propertyFields.maximum !== undefined) { validators.push(withMax); } From 3c76156337dec4b3a7e34254f53a2e6daef0b427 Mon Sep 17 00:00:00 2001 From: Jakob Guddas Date: Mon, 5 Jun 2023 16:18:57 +0200 Subject: [PATCH 2/3] test: changed tests to be more inline with the new test structure --- src/tests/createHeadlessForm.test.js | 10 ++++++++++ src/tests/helpers.js | 26 ++++++++++++-------------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/tests/createHeadlessForm.test.js b/src/tests/createHeadlessForm.test.js index 698ea66fa..29c8f8a06 100644 --- a/src/tests/createHeadlessForm.test.js +++ b/src/tests/createHeadlessForm.test.js @@ -1863,6 +1863,16 @@ describe('createHeadlessForm', () => { ).resolves.toEqual(assertObj); }); }); + describe('and maximum is set to zero', () => { + it('shows an error when a positive number', () => { + const { handleValidation } = createHeadlessForm(schemaInputTypeNumberZeroMaximum); + const validateForm = (vals) => friendlyError(handleValidation(vals)); + + expect(validateForm({ tabs: 1 })).toEqual({ + tabs: 'Must be smaller or equal to 0', + }); + }); + }); }); describe('when a field has a maxLength of 10', () => { diff --git a/src/tests/helpers.js b/src/tests/helpers.js index bcd6348d0..46002ba44 100644 --- a/src/tests/helpers.js +++ b/src/tests/helpers.js @@ -46,23 +46,21 @@ export const mockNumberInput = { type: 'number', }; -export const mockNumberInputNegative = { - title: 'Tabs', - description: 'How many open tabs do you have?', - 'x-jsf-presentation': { - inputType: 'number', +export const schemaInputTypeNumberZeroMaximum = { + properties: { + tabs: { + title: 'Tabs', + description: 'How many open tabs do you have?', + 'x-jsf-presentation': { + inputType: 'number', + }, + minimum: -100, + maximum: 0, + type: 'number', + }, }, - minimum: -100, - maximum: 0, - type: 'number', }; -export const schemaInputTypeNumberZeroMaximum = JSONSchemaBuilder() - .addInput({ - tabs: mockNumberInputNegative, - }) - .build(); - export const mockNumberInputWithPercentage = { title: 'Shares', description: 'What % of shares do you own?', From 77caf14978f7b025df9cc5373d5088922842425a Mon Sep 17 00:00:00 2001 From: Jakob Guddas Date: Thu, 8 Jun 2023 22:01:40 +0200 Subject: [PATCH 3/3] chore: changed tests to be more inline with the new test structure --- src/tests/createHeadlessForm.test.js | 28 ++++------------------------ 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/src/tests/createHeadlessForm.test.js b/src/tests/createHeadlessForm.test.js index 29c8f8a06..3dbcb015b 100644 --- a/src/tests/createHeadlessForm.test.js +++ b/src/tests/createHeadlessForm.test.js @@ -781,29 +781,6 @@ describe('createHeadlessForm', () => { expect(() => fieldValidator.validateSync('')).toThrowError('The value must be a number'); }); - it('shows an error when positive number is entered into a "number" field with maximum set to zero', () => { - const result = createHeadlessForm(schemaInputTypeNumberZeroMaximum); - expect(result).toMatchObject({ - fields: [ - { - description: 'How many open tabs do you have?', - label: 'Tabs', - name: 'tabs', - required: false, - schema: expect.any(Object), - type: 'number', - minimum: -100, - maximum: 0, - }, - ], - }); - const fieldValidator = result.fields[0].schema; - expect(fieldValidator.isValidSync('0')).toBe(true); - expect(fieldValidator.isValidSync('10')).toBe(false); - expect(fieldValidator.isValidSync('-11')).toBe(true); - expect(() => fieldValidator.validateSync('5')).toThrowError('Must be smaller or equal to 0'); - }); - it('support "number" field type with the percentage attribute', () => { const result = createHeadlessForm(schemaInputTypeNumberWithPercentage); expect(result).toMatchObject({ @@ -1864,10 +1841,13 @@ describe('createHeadlessForm', () => { }); }); describe('and maximum is set to zero', () => { - it('shows an error when a positive number', () => { + it('shows the correct validation', () => { const { handleValidation } = createHeadlessForm(schemaInputTypeNumberZeroMaximum); const validateForm = (vals) => friendlyError(handleValidation(vals)); + expect(validateForm({ tabs: '0' })).toBeUndefined(); + expect(validateForm({ tabs: '-10' })).toBeUndefined(); + expect(validateForm({ tabs: 1 })).toEqual({ tabs: 'Must be smaller or equal to 0', });