diff --git a/src/tests/createHeadlessForm.test.js b/src/tests/createHeadlessForm.test.js index 6fd16914d..3dbcb015b 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, @@ -1839,6 +1840,19 @@ describe('createHeadlessForm', () => { ).resolves.toEqual(assertObj); }); }); + describe('and maximum is set to zero', () => { + 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', + }); + }); + }); }); describe('when a field has a maxLength of 10', () => { diff --git a/src/tests/helpers.js b/src/tests/helpers.js index 32d02f613..46002ba44 100644 --- a/src/tests/helpers.js +++ b/src/tests/helpers.js @@ -46,6 +46,21 @@ export const mockNumberInput = { type: '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', + }, + }, +}; + 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); }