From 8ad3c553289cc609a42d4c2398430ce87acd99ca Mon Sep 17 00:00:00 2001 From: Dave Malloy Date: Wed, 25 Sep 2024 14:14:36 -0400 Subject: [PATCH] fix(renderer): update the required validator to properly handle falsy numeric values --- .../src/tests/validators/validators.test.js | 24 +++++++++++++++++++ .../src/validators/validator-functions.js | 13 ++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/react-form-renderer/src/tests/validators/validators.test.js b/packages/react-form-renderer/src/tests/validators/validators.test.js index 9659257c9..5de304a2c 100644 --- a/packages/react-form-renderer/src/tests/validators/validators.test.js +++ b/packages/react-form-renderer/src/tests/validators/validators.test.js @@ -10,10 +10,34 @@ describe('New validators', () => { expect(validatorMapper[validatorTypes.REQUIRED]()('Foo')).toBeUndefined(); }); + it('should pass required validator if numeric truthy value', () => { + expect(validatorMapper[validatorTypes.REQUIRED]()(1)).toBeUndefined(); + }); + + it('should pass required validator if numeric falsy value', () => { + expect(validatorMapper[validatorTypes.REQUIRED]()(0)).toBeUndefined(); + }); + + it('should pass required validator if boolean true', () => { + expect(validatorMapper[validatorTypes.REQUIRED]()(true)).toBeUndefined(); + }); + + it('should pass required validator if boolean false', () => { + expect(validatorMapper[validatorTypes.REQUIRED]()(false)).toBeUndefined(); + }); + it('should fail required validator', () => { expect(validatorMapper[validatorTypes.REQUIRED]()()).toBe('Required'); }); + it('should fail required validator if explictly null', () => { + expect(validatorMapper[validatorTypes.REQUIRED]()(null)).toBe('Required'); + }); + + it('should fail required validator if explictly undefined', () => { + expect(validatorMapper[validatorTypes.REQUIRED]()(undefined)).toBe('Required'); + }); + it('should fail required validator if string of white spaces', () => { expect(validatorMapper[validatorTypes.REQUIRED]()(' ')).toBe('Required'); }); diff --git a/packages/react-form-renderer/src/validators/validator-functions.js b/packages/react-form-renderer/src/validators/validator-functions.js index 8431718c6..b8135a68b 100644 --- a/packages/react-form-renderer/src/validators/validator-functions.js +++ b/packages/react-form-renderer/src/validators/validator-functions.js @@ -2,8 +2,17 @@ import { memoize, prepare, prepareMsg, selectNum, isNumber, trunc } from '../com export const required = memoize(({ message } = {}) => { return prepare((value) => { - const cond = typeof value === 'string' ? !value.trim() : value && !isNaN(value.length) ? !value.length : !value; - if (cond) { + let failsValidation = true; + + if (typeof value === 'string') { + failsValidation = !value.trim(); + } else if (Array.isArray(value)) { + failsValidation = !value.length; + } else { + failsValidation = value === null || value === undefined; + } + + if (failsValidation) { return prepareMsg(message, 'required').defaultMessage; } });