From 98b42fed7f1d60e98c28f1f616386c8e98f6d700 Mon Sep 17 00:00:00 2001 From: Carl Bauer Date: Mon, 13 Jul 2015 15:40:15 -0700 Subject: [PATCH] Flag when validation has validated for the first time. --- src/Validator.js | 7 ++++++- test/validator.js | 15 ++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Validator.js b/src/Validator.js index e396c9e..92b6a75 100644 --- a/src/Validator.js +++ b/src/Validator.js @@ -9,6 +9,7 @@ class Validator { this._validator = validate this._errors = Object.create(null) + this.hasValidated = false; } @@ -34,6 +35,10 @@ class Validator { this._removeError(name) return Promise.all(fields) + .then(fields => { + this.hasValidated = true + return fields + }) } _validateField(name, context){ @@ -58,4 +63,4 @@ class Validator { } } -module.exports = Validator \ No newline at end of file +module.exports = Validator diff --git a/test/validator.js b/test/validator.js index cc32077..5dce0ce 100644 --- a/test/validator.js +++ b/test/validator.js @@ -18,7 +18,7 @@ describe('validator', function(){ spy.should.have.been.calledWithExactly('field', context) }) }) - + it('should use passed in validation function', () => { var spy , context = {} @@ -63,12 +63,21 @@ describe('validator', function(){ return validator.validate('fieldA').should.be.fulfilled .then(() => { validator.errors().should.have.key('fieldA') - + return validator.validate('fieldA').should.be.fulfilled .then(() => { validator.errors().should.not.have.key('fieldA') }) }) }) -}) + it('should track initial validation state', () => { + var validator = new Validator( field => 'invalid') + validator.hasValidated.should.equal(false) + + return validator.validate('fieldA').should.be.fulfilled + .then(() => { + validator.hasValidated.should.equal(true) + }) + }) +})