From bc96a8976894e21946badfb8e5c004e429a4c294 Mon Sep 17 00:00:00 2001 From: kymmew Date: Fri, 27 Apr 2018 12:44:45 +0100 Subject: [PATCH] only validate on an empty field if it is required (#27) * only validate on an empty field if it is required * Adding additional test --- CHANGELOG.md | 8 ++++ package.json | 2 +- src/index.js | 4 +- test/__snapshots__/validateOn.test.js.snap | 20 +++++++-- test/validateOn.test.js | 51 +++++++++++++++++++++- 5 files changed, 76 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3055eec..f7c7726 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +v0.18.0 +------------------------------ +*April 26, 2018* + +### Changed +- Changed logic to only validate on an empty field if it is required + + v0.17.1 ------------------------------ *April 12, 2018* diff --git a/package.json b/package.json index 541983b..3ff3f74 100755 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@justeat/f-validate", "description": "Fozzie vanilla JS Validation Component", - "version": "0.17.1", + "version": "0.18.0", "main": "dist/index.js", "files": [ "dist" diff --git a/src/index.js b/src/index.js index 48eacb1..8c1c288 100755 --- a/src/index.js +++ b/src/index.js @@ -158,8 +158,8 @@ export default class FormValidation { this.fields.forEach(field => { // currentElement refers to an element that is being validated on blur/keyup - // only validate on blur/keyup if the field is not empty - if (currentElement && (currentElement.field !== field || field.value === '')) { + // only validate on blur/keyup if the field is not empty and is not required + if (currentElement && (currentElement.field !== field || (field.value === '' && !testDefinitions.required.condition(field)))) { return; } diff --git a/test/__snapshots__/validateOn.test.js.snap b/test/__snapshots__/validateOn.test.js.snap index 1aadc1f..dce1879 100644 --- a/test/__snapshots__/validateOn.test.js.snap +++ b/test/__snapshots__/validateOn.test.js.snap @@ -7,9 +7,9 @@ exports[`validateOn blur should not run validation on other fields 1`] = ` " `; -exports[`validateOn blur should not validate if field is empty 1`] = ` +exports[`validateOn blur should not validate if field is empty and not required 1`] = ` "
- +
" `; @@ -73,15 +73,27 @@ exports[`validateOn blur should set form state to valid, as second select has no " `; +exports[`validateOn blur should validate if field is empty 1`] = ` +"
+

This field is required.

+
" +`; + exports[`validateOn blur should validate valid form 1`] = ` "
" `; -exports[`validateOn keyup should not validate if field is empty 1`] = ` +exports[`validateOn keyup should not validate if field is empty and not required 1`] = ` "
- + +
" +`; + +exports[`validateOn keyup should validate if field is empty 1`] = ` +"
+

This field is required.

" `; diff --git a/test/validateOn.test.js b/test/validateOn.test.js index 97b7da7..5f2adea 100644 --- a/test/validateOn.test.js +++ b/test/validateOn.test.js @@ -25,7 +25,7 @@ describe('validateOn', () => { describe('blur', () => { - it('should not validate if field is empty', () => { + it('should validate if field is empty', () => { // Arrange TestUtils.setBodyHtml(`
@@ -48,6 +48,29 @@ describe('validateOn', () => { }); + it('should not validate if field is empty and not required', () => { + + // Arrange + TestUtils.setBodyHtml(` + +
`); + const form = document.querySelector('form'); + const input = form.querySelector('input'); + + // eslint-disable-next-line no-new + new FormValidation(form, { + validateOn: 'blur' + }); + + // Act + TestUtils.dispatchEvent(input, 'blur'); + + // Assert + const html = TestUtils.getBodyHtml(); + expect(html).toMatchSnapshot(); + + }); + it('should validate valid form', () => { // Arrange @@ -239,7 +262,7 @@ describe('validateOn', () => { describe('keyup', () => { - it('should not validate if field is empty', () => { + it('should validate if field is empty', () => { // Arrange TestUtils.setBodyHtml(`
@@ -262,6 +285,30 @@ describe('validateOn', () => { }); + + it('should not validate if field is empty and not required', () => { + + // Arrange + TestUtils.setBodyHtml(` + +
`); + const form = document.querySelector('form'); + const input = form.querySelector('input'); + + // eslint-disable-next-line no-new + new FormValidation(form, { + validateOn: 'keyup' + }); + + // Act + TestUtils.dispatchEvent(input, 'keyup'); + + // Assert + const html = TestUtils.getBodyHtml(); + expect(html).toMatchSnapshot(); + + }); + it('should validate valid form', () => { // Arrange