Skip to content

Commit

Permalink
FIX Handle extra empty values
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Nov 26, 2023
1 parent 1bbb401 commit c862349
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions client/src/lib/Validator.js
Expand Up @@ -35,9 +35,9 @@ class Validator {
return value;
}

validateValue(value, rule, config) {
validateValue(value, emptyValues, rule, config) {
// Empty values suppress error unless rule is required
if (value === '') {
if (emptyValues.includes(value)) {
return rule !== 'required';
}
switch (rule) {
Expand Down Expand Up @@ -120,8 +120,13 @@ class Validator {

const value = this.getFieldValue(name);

// no required rule given and no value, so skip all other validation
if (value === '' && rules.required) {
let emptyValues = [''];
if (rules.required && typeof rules.required === 'object' && rules.required.hasOwnProperty('extraEmptyValues')) {
emptyValues = emptyValues.concat(rules.required.extraEmptyValues);
}

// required rule given and empty value, so skip all other validation
if (rules.required && emptyValues.includes(value)) {
const config = Object.assign(
{ title: (title !== '') ? title : name },
rules.required
Expand All @@ -142,7 +147,7 @@ class Validator {
return;
}

const valid = this.validateValue(value, rule, config);
const valid = this.validateValue(value, emptyValues, rule, config);
if (!valid) {
const message = this.getMessage(rule, config);
response.valid = false;
Expand Down
10 changes: 5 additions & 5 deletions client/src/lib/tests/Validator-test.js
Expand Up @@ -61,16 +61,16 @@ describe('Validator', () => {
describe('validateValue()', () => {
it('should throw an error', () => {
validator = new Validator({});
validator.validateValue('one', 'ruledoesnotexist');
validator.validateValue('one', [''], 'ruledoesnotexist');
// eslint-disable-next-line no-console
expect(console.warn).toBeCalled();
});

it('should not error when empty and not required', () => {
validator = new Validator({});
const result1 = validator.validateValue('', 'date');
const result1 = validator.validateValue('', [''], 'date');
expect(result1).toBe(true);
const result2 = validator.validateValue('', 'required');
const result2 = validator.validateValue('', [''], 'required');
expect(result2).toBe(false);
});
});
Expand Down Expand Up @@ -100,10 +100,10 @@ describe('Validator', () => {

beforeEach(() => {
validator = new Validator({});
validator.validateValue = (value, rule) => {
validator.validateValue = (value, emptyValues, rule) => {
switch (rule) {
case 'required': {
return value !== '';
return !emptyValues.includes(value);
}
case 'numeric': {
return !isNaN(value);
Expand Down

0 comments on commit c862349

Please sign in to comment.