Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(validation): correctly validate empty not required data #1606

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ui/src/util/Validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ class Validator {
*/
// Validate the range of numeric field
static NumberValidator(field, label, validator, data) {
// this validation should be before this function but adding it
// here to avoid any errors until this module is moved to ts
if (data === null || data === undefined) {
return false;
}

const { error } = parseNumberValidator(validator.range);
if (error) {
return { errorField: field, errorMsg: error };
Expand Down
19 changes: 19 additions & 0 deletions ui/src/util/tests/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ describe('Validator.doValidation - number case', () => {
{
field: 'testField',
label: 'Test Field',
type: 'text',
validators: [
{
type: 'number',
Expand All @@ -210,6 +211,24 @@ describe('Validator.doValidation - number case', () => {
expect(result).toBe(false);
});

it.each([undefined, null])('should return false validation for %s when optional', (value) => {
const validator = new Validator(entities);
const data = { testField: value };
const result = validator.doValidation(data);
expect(result).toBe(false);
});

it.each([undefined, null])('should return error for %s number when required', (value) => {
const requiredEntities = [{ ...entities[0], required: true }];
const validator = new Validator(requiredEntities);
const data = { testField: value };
const result = validator.doValidation(data);
expect(result).toEqual({
errorField: 'testField',
errorMsg: 'Field Test Field is required',
});
});

it('should return an error for number out of range', () => {
const validator = new Validator(entities);
const data = { testField: 15 };
Expand Down
Loading