value-validator
Advanced tools
+11
-8
@@ -74,2 +74,4 @@ 'use strict'; | ||
| value: function _validate(v) { | ||
| var _this2 = this; | ||
| var rules = this._rules; | ||
@@ -83,4 +85,5 @@ | ||
| var first = rules[0]; | ||
| var init = first.call(this._context, v); | ||
| var result = rules.length === 1 ? first(v) : rules.reduce(function (prev, current, index) { | ||
| var result = rules.length === 1 ? init : rules.reduce(function (prev, current, index) { | ||
@@ -93,3 +96,3 @@ if (index === 0) { | ||
| if (pass) { | ||
| return current(v); | ||
| return current.call(_this2._context, v); | ||
| } | ||
@@ -102,3 +105,3 @@ | ||
| : wrap_non_promise_result(prev, current, v); | ||
| }, first(v)); | ||
| }, init); | ||
@@ -142,3 +145,3 @@ return is_promise_like(result) ? result : wrap_non_promise_result(result); | ||
| value: function _decodePreset(rule) { | ||
| var _this2 = this; | ||
| var _this3 = this; | ||
@@ -149,3 +152,3 @@ return this._codec(rule).forEach(function (_ref2) { | ||
| var preset = _this2._presets[name]; | ||
| var preset = _this3._presets[name]; | ||
@@ -157,3 +160,3 @@ if (!preset) { | ||
| if (typeof preset === 'function') { | ||
| _this2._rules.push(_this2._wrapWithArgs(preset, args)); | ||
| _this3._rules.push(_this3._wrapWithArgs(preset, args)); | ||
| return; | ||
@@ -165,3 +168,3 @@ } | ||
| if (_util2.default.isArray(preset)) { | ||
| preset.forEach(_this2._add, _this2); | ||
| preset.forEach(_this3._add, _this3); | ||
| } | ||
@@ -183,3 +186,3 @@ }); | ||
| return function (v) { | ||
| return method.apply(undefined, [v].concat(_toConsumableArray(args))); | ||
| return method.apply(this, [v].concat(_toConsumableArray(args))); | ||
| }; | ||
@@ -186,0 +189,0 @@ } |
+1
-1
| { | ||
| "name": "value-validator", | ||
| "version": "2.2.1", | ||
| "version": "2.2.2", | ||
| "description": "Low-level rule manager for validating values.", | ||
@@ -5,0 +5,0 @@ "main": "./lib", |
+6
-0
@@ -75,2 +75,8 @@ [](https://travis-ci.org/kaelzhang/value-validator) | ||
| ## .context(context) | ||
| - **context** `Object` specify this object for all validator functions. | ||
| Returns `this` | ||
| ### Sync Function-type `rule` | ||
@@ -77,0 +83,0 @@ |
@@ -24,2 +24,12 @@ import ava from 'ava' | ||
| { | ||
| title: 'test context', | ||
| context: { | ||
| a: 1 | ||
| }, | ||
| test (v) { | ||
| return v === this.a | ||
| }, | ||
| value: 1 | ||
| }, | ||
| { | ||
| title: 'function, returns boolean, pass', | ||
@@ -90,3 +100,4 @@ test: (v) => { | ||
| pass, | ||
| only | ||
| only, | ||
| context | ||
| } = c | ||
@@ -116,2 +127,6 @@ | ||
| if (context) { | ||
| v.context(context) | ||
| } | ||
| v.validate(value).then( | ||
@@ -118,0 +133,0 @@ success => { |
124159
0.31%433
3.84%154
4.05%