From 65162b15a311413105c5a85d5dbb70b07041409f Mon Sep 17 00:00:00 2001 From: Dominic Bartl Date: Tue, 16 May 2017 17:45:23 +0200 Subject: [PATCH] Add strict mode to run rules even if the field isnt' required --- core.js | 3 ++- test/spec.js | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/core.js b/core.js index 8d07cfe..62f2302 100644 --- a/core.js +++ b/core.js @@ -19,6 +19,7 @@ function Checkit(validations, options) { this.language = Checkit.i18n[options.language || Checkit.language] || {}; this.labelTransform = options.labelTransform || Checkit.labelTransform this.validations = prepValidations(validations || {}); + this.mode = options.mode; } Checkit.VERSION = '0.6.0'; @@ -198,7 +199,7 @@ function processItem(runner, currentValidation, key, context) { // If the rule isn't an existence / required check, return // true if the value doesn't exist. - if (rule !== 'accepted' && rule !== 'exists' && rule !== 'required') { + if (runner.checkit.mode !== 'strict' && rule !== 'accepted' && rule !== 'exists' && rule !== 'required') { if (value === '' || value == null) return; } var result = runRule(runner.validator, runner, rule, params) diff --git a/test/spec.js b/test/spec.js index 977ad13..5787cbb 100644 --- a/test/spec.js +++ b/test/spec.js @@ -453,4 +453,13 @@ describe('Checkit', function() { }); }); + describe('strict mode', function () { + it('should run all rules in strict mode', function(){ + var arr = Checkit({ + "emptyString": ['exactLength:1'] + }, {mode: 'strict'}).runSync({emptyString: ''}) + assert(arr[0] instanceof Checkit.Error) + }); + }); + });