validator-runner
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -1,2 +0,2 @@ | ||
function _classCallCheck(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,r){for(var t=0;t<r.length;t++){var n=r[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function _createClass(e,r,t){return r&&_defineProperties(e.prototype,r),t&&_defineProperties(e,t),e}function _toConsumableArray(e){if(Array.isArray(e)){for(var r=0,t=new Array(e.length);r<e.length;r++)t[r]=e[r];return t}return Array.from(e)}var Validator=function(){function e(r){var t=this;_classCallCheck(this,e),this.transformToPromise=function(e,r,n,a){var o=r.validator.apply(r,_toConsumableArray(n)),i=!0;o&&o.then&&o.catch||(i=!1,o=o?Promise.resolve(o):Promise.reject(o));var l={errors:a,fieldName:e,target:n,isAsync:i};return o.then(function(e){return Object.assign(l,{promiseValue:e})},function(e){return l.errors=t.messageHandler(a,r.message,n,e),Promise.reject(Object.assign(l,{promiseValue:e}))})},this.lastValidator={},this.descriptor=r}return _createClass(e,[{key:"validateItem",value:function(e,r,t){var n,a=this,o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{traversal:!1,retention:!1,concurrent:!1},i=[],l=(arguments.length>4?arguments[4]:void 0)||this.descriptor[r];if(l&&!(l.length<1))return Array.isArray(l)||(l=[l]),n=o.concurrent?Promise.all(l.map(function(t){var n=[r].concat(_toConsumableArray(t.join||[])).map(function(r){return e[r]});return a.transformToPromise(r,t,n,i).then(function(e){return e},o.traversal?function(e){return e}:null)})):l.reduce(function(t,n){var l=[r].concat(_toConsumableArray(n.join||[])).map(function(r){return e[r]});return t.then(function(){return a.transformToPromise(r,n,l,i)},o.traversal?function(){return a.transformToPromise(r,n,l,i)}:null)},Promise.resolve()),this.lastValidator[r]=n,n.then(function(e){return(o.retention||a.lastValidator[r]===n)&&t&&t(i,e),e},function(e){return(o.retention||a.lastValidator[r]===n)&&t&&t(i,e),Promise.reject(e)});t(i,{})}},{key:"validate",value:function(e,r){var t=this,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},a=!1,o={},i=[];return(n.specificField||Object.keys(this.descriptor)).forEach(function(r){i.push(t.validateItem(e,r,function(e){o[r]=e,n.fieldCallback&&n.fieldCallback(r,e)},n))}),Promise.all(i.map(function(e){return e.catch(function(e){return a=!0,e})})).then(function(){return r(o,!a),a?Promise.reject(o):o})}},{key:"messageHandler",value:function(e,r,t,n){return"function"==typeof r?e.push("".concat(r.apply(void 0,_toConsumableArray(t).concat([n]))||"Error!").trim()):e.push("".concat(r||"Error!").trim()),e}},{key:"cancelItem",value:function(e){this.lastValidator[e]=null}},{key:"cancelAll",value:function(){this.lastValidator={}}},{key:"addRule",value:function(e){this.ruleSet=Object.assign(this.ruleSet,e)}},{key:"updateRuleSet",value:function(e){this.ruleSet=e}},{key:"deleteRule",value:function(e){delete this.ruleSet[e]}}]),e}();export default Validator; | ||
function _classCallCheck(r,t){if(!(r instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(r,t){for(var e=0;e<t.length;e++){var n=t[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(r,n.key,n)}}function _createClass(r,t,e){return t&&_defineProperties(r.prototype,t),e&&_defineProperties(r,e),r}function _toConsumableArray(r){return _arrayWithoutHoles(r)||_iterableToArray(r)||_nonIterableSpread()}function _arrayWithoutHoles(r){if(Array.isArray(r)){for(var t=0,e=new Array(r.length);t<r.length;t++)e[t]=r[t];return e}}function _iterableToArray(r){if(Symbol.iterator in Object(r)||"[object Arguments]"===Object.prototype.toString.call(r))return Array.from(r)}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}var Validator=function(){function r(t){var e=this,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{first:!0,concurrent:!1};_classCallCheck(this,r),this.transformToPromise=function(r,t,n){for(var a=arguments.length,o=new Array(a>3?a-3:0),i=3;i<a;i++)o[i-3]=arguments[i];if("function"!=typeof r.validator)throw"invalid validator";var l=r.validator.apply(r,[t].concat(o));l&&l.then&&l.catch||(l=l?Promise.resolve(l):Promise.reject(l));var s={errors:n,target:t};return l.then(function(r){return Object.assign(s,{promiseValue:r})},function(a){return s.errors=e.messageHandler.apply(e,[n,r.message,t,a].concat(o)),Promise.reject(Object.assign(s,{promiseValue:a}))})},this.lastValidator={},this.schema=t,this.options=n}return _createClass(r,[{key:"validate",value:function(r,t){for(var e=this,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},a=arguments.length,o=new Array(a>3?a-3:0),i=3;i<a;i++)o[i-3]=arguments[i];var l=[];return!t||t.length<1?Promise.resolve({value:r}):(Array.isArray(t)||(t=[t]),n.concurrent?Promise.all(t.map(function(t){return e.transformToPromise.apply(e,[t,r,l].concat(o)).then(function(r){return r},n.first?function(r){return r}:null)})):t.reduce(function(t,a){return t.then(function(){return e.transformToPromise.apply(e,[a,r,l].concat(o))},n.first?null:function(){return e.transformToPromise.apply(e,[a,r,l].concat(o))})},Promise.resolve()))}},{key:"validateItem",value:function(r,t,e){var n=this,a=this.schema[t],o=r[t];if(a&&a.rules&&(a=a.rules),a&&a.withFields){var i=Array.isArray(a.withFields)?a.withFields:[a.withFields];o=[t].concat(_toConsumableArray(i)).map(function(t){return r[t]})}for(var l=arguments.length,s=new Array(l>3?l-3:0),c=3;c<l;c++)s[c-3]=arguments[c];var u=this.validate.apply(this,[o,a,this.options].concat(s));return this.lastValidator[t]=u,u.then(function(r){return e&&n.lastValidator[t]===u&&(n.lastValidator[t]=null,e(null,r.target)),r},function(r){return e&&n.lastValidator[t]===u&&(n.lastValidator[t]=null,e(r.errors,r.target)),r})}},{key:"messageHandler",value:function(r,t,e,n){if("function"==typeof t){for(var a=arguments.length,o=new Array(a>4?a-4:0),i=4;i<a;i++)o[i-4]=arguments[i];r.push("".concat(t.apply(void 0,[e,n].concat(o))||"Error!").trim())}else r.push("".concat(null!=t?t:"Error!").trim());return r}},{key:"cancelItem",value:function(r){this.lastValidator[r]=null}},{key:"cancelAll",value:function(){this.lastValidator={}}},{key:"setSchema",value:function(r){this.schema=r}},{key:"setOptions",value:function(r){this.options=r}}]),r}();export default Validator; | ||
//# sourceMappingURL=bundle.esm.js.map |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.validator=t()}(this,function(){"use strict";function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function t(e){if(Array.isArray(e)){for(var t=0,r=new Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)}return function(){function r(e){var n=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,r),this.transformToPromise=function(e,r,i,a){var o=r.validator.apply(r,t(i)),u=!0;o&&o.then&&o.catch||(u=!1,o=o?Promise.resolve(o):Promise.reject(o));var l={errors:a,fieldName:e,target:i,isAsync:u};return o.then(function(e){return Object.assign(l,{promiseValue:e})},function(e){return l.errors=n.messageHandler(a,r.message,i,e),Promise.reject(Object.assign(l,{promiseValue:e}))})},this.lastValidator={},this.descriptor=e}var n,i,a;return n=r,(i=[{key:"validateItem",value:function(e,r,n){var i,a=this,o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{traversal:!1,retention:!1,concurrent:!1},u=arguments.length>4?arguments[4]:void 0,l=[],c=u||this.descriptor[r];if(c&&!(c.length<1))return Array.isArray(c)||(c=[c]),i=o.concurrent?Promise.all(c.map(function(n){var i=[r].concat(t(n.join||[])).map(function(t){return e[t]});return a.transformToPromise(r,n,i,l).then(function(e){return e},o.traversal?function(e){return e}:null)})):c.reduce(function(n,i){var u=[r].concat(t(i.join||[])).map(function(t){return e[t]});return n.then(function(){return a.transformToPromise(r,i,u,l)},o.traversal?function(){return a.transformToPromise(r,i,u,l)}:null)},Promise.resolve()),this.lastValidator[r]=i,i.then(function(e){return(o.retention||a.lastValidator[r]===i)&&n&&n(l,e),e},function(e){return(o.retention||a.lastValidator[r]===i)&&n&&n(l,e),Promise.reject(e)});n(l,{})}},{key:"validate",value:function(e,t){var r=this,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i=!1,a={},o=[];return(n.specificField||Object.keys(this.descriptor)).forEach(function(t){o.push(r.validateItem(e,t,function(e){a[t]=e,n.fieldCallback&&n.fieldCallback(t,e)},n))}),Promise.all(o.map(function(e){return e.catch(function(e){return i=!0,e})})).then(function(){return t(a,!i),i?Promise.reject(a):a})}},{key:"messageHandler",value:function(e,r,n,i){return"function"==typeof r?e.push("".concat(r.apply(void 0,t(n).concat([i]))||"Error!").trim()):e.push("".concat(r||"Error!").trim()),e}},{key:"cancelItem",value:function(e){this.lastValidator[e]=null}},{key:"cancelAll",value:function(){this.lastValidator={}}},{key:"addRule",value:function(e){this.ruleSet=Object.assign(this.ruleSet,e)}},{key:"updateRuleSet",value:function(e){this.ruleSet=e}},{key:"deleteRule",value:function(e){delete this.ruleSet[e]}}])&&e(n.prototype,i),a&&e(n,a),r}()}); | ||
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):t.validator=r()}(this,function(){"use strict";function t(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function r(t){return function(t){if(Array.isArray(t)){for(var r=0,e=new Array(t.length);r<t.length;r++)e[r]=t[r];return e}}(t)||function(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}return function(){function e(t){var r=this,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{first:!0,concurrent:!1};!function(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}(this,e),this.transformToPromise=function(t,e,n){for(var a=arguments.length,o=new Array(a>3?a-3:0),i=3;i<a;i++)o[i-3]=arguments[i];if("function"!=typeof t.validator)throw"invalid validator";var l=t.validator.apply(t,[e].concat(o));l&&l.then&&l.catch||(l=l?Promise.resolve(l):Promise.reject(l));var s={errors:n,target:e};return l.then(function(t){return Object.assign(s,{promiseValue:t})},function(a){return s.errors=r.messageHandler.apply(r,[n,t.message,e,a].concat(o)),Promise.reject(Object.assign(s,{promiseValue:a}))})},this.lastValidator={},this.schema=t,this.options=n}var n,a,o;return n=e,(a=[{key:"validate",value:function(t,r){for(var e=this,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},a=arguments.length,o=new Array(a>3?a-3:0),i=3;i<a;i++)o[i-3]=arguments[i];var l=[];return!r||r.length<1?Promise.resolve({value:t}):(Array.isArray(r)||(r=[r]),n.concurrent?Promise.all(r.map(function(r){return e.transformToPromise.apply(e,[r,t,l].concat(o)).then(function(t){return t},n.first?function(t){return t}:null)})):r.reduce(function(r,a){return r.then(function(){return e.transformToPromise.apply(e,[a,t,l].concat(o))},n.first?null:function(){return e.transformToPromise.apply(e,[a,t,l].concat(o))})},Promise.resolve()))}},{key:"validateItem",value:function(t,e,n){var a=this,o=this.schema[e],i=t[e];if(o&&o.rules&&(o=o.rules),o&&o.withFields){var l=Array.isArray(o.withFields)?o.withFields:[o.withFields];i=[e].concat(r(l)).map(function(r){return t[r]})}for(var s=arguments.length,u=new Array(s>3?s-3:0),c=3;c<s;c++)u[c-3]=arguments[c];var f=this.validate.apply(this,[i,o,this.options].concat(u));return this.lastValidator[e]=f,f.then(function(t){return n&&a.lastValidator[e]===f&&(a.lastValidator[e]=null,n(null,t.target)),t},function(t){return n&&a.lastValidator[e]===f&&(a.lastValidator[e]=null,n(t.errors,t.target)),t})}},{key:"messageHandler",value:function(t,r,e,n){if("function"==typeof r){for(var a=arguments.length,o=new Array(a>4?a-4:0),i=4;i<a;i++)o[i-4]=arguments[i];t.push("".concat(r.apply(void 0,[e,n].concat(o))||"Error!").trim())}else t.push("".concat(null!=r?r:"Error!").trim());return t}},{key:"cancelItem",value:function(t){this.lastValidator[t]=null}},{key:"cancelAll",value:function(){this.lastValidator={}}},{key:"setSchema",value:function(t){this.schema=t}},{key:"setOptions",value:function(t){this.options=t}}])&&t(n.prototype,a),o&&t(n,o),e}()}); |
{ | ||
"name": "validator-runner", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Validate form asynchronous", | ||
@@ -27,7 +27,7 @@ "main": "dist/bundle.js", | ||
"babel-eslint": "^8.0.1", | ||
"babel-jest": "^22.1.0", | ||
"babel-jest": "^23.0.1", | ||
"babel-plugin-transform-class-properties": "^6.24.1", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0", | ||
"babel-plugin-transform-object-rest-spread": "^6.26.0", | ||
"eslint": "^4.8.0", | ||
"eslint": "^5.0.1", | ||
"eslint-config-prettier": "^2.6.0", | ||
@@ -37,8 +37,7 @@ "eslint-plugin-import": "^2.7.0", | ||
"eslint-plugin-prettier": "^2.6.0", | ||
"jest": "^22.1.1", | ||
"jest": "^23.1.0", | ||
"prettier": "^1.7.4", | ||
"rimraf": "^2.6.2", | ||
"rollup": "^0.54.0", | ||
"rollup-plugin-uglify": "^2.0.1", | ||
"uglify-es": "^3.3.7" | ||
"rollup": "^0.62.0", | ||
"rollup-plugin-terser": "^1.0.1" | ||
}, | ||
@@ -45,0 +44,0 @@ "dependencies": { |
@@ -11,43 +11,41 @@ # validator-runner | ||
define descriptor => passing the descriptor => add callback | ||
```js | ||
import Validator from 'validator-runner'; | ||
const descriptor = { | ||
name: [ | ||
{ | ||
validator: name => name, | ||
message: 'Please input something!', | ||
}, | ||
{ | ||
validator(name) { | ||
return new Promise((res, rej) => { | ||
setTimeout(() => { | ||
name === 'loading' ? res(name) : rej(name); | ||
}, 0); | ||
}); | ||
}, | ||
message: name => `"${name}" is not my name!`, | ||
}, | ||
], | ||
birth: { | ||
validator(date) { | ||
return date === '1995'; | ||
}, | ||
message: 'Wrong Date!', | ||
const schema = { | ||
name: [ | ||
{ | ||
validator: name => name, | ||
message: 'Please input something!', | ||
}, | ||
{ | ||
validator(name) { | ||
return new Promise((res, rej) => { | ||
setTimeout(() => { | ||
name === 'loading' ? res(name) : rej(name); | ||
}, 0); | ||
}); | ||
}, | ||
message: name => `"${name}" is not my name!`, | ||
}, | ||
], | ||
birth: { | ||
validator(date) { | ||
return date === '1995'; | ||
}, | ||
message: 'Wrong Date!', | ||
}, | ||
}; | ||
const validator = new Validator(descriptor); | ||
const validator = new Validator(schema); | ||
validator.validateItem({ name: 'loading' }, 'name').then(data => { | ||
// data: | ||
// errors: an array of errors | ||
// rule: the validation object that executed last | ||
// data: | ||
// errors: an array of errors | ||
// rule: the validation object that executed last | ||
}); | ||
// or | ||
validator.validateItem({ name: 'loading' }, 'name', (errors, data) => { | ||
// errors: an array of errors | ||
// data: | ||
// errors: an array of errors | ||
// rule: the validation object that executed last | ||
// errors: an array of errors | ||
// data: | ||
// errors: an array of errors | ||
// rule: the validation object that executed last | ||
}); | ||
@@ -62,36 +60,26 @@ ``` | ||
### validateItem | ||
### Validator | ||
`validateItem(source, fieldName, callback, options = { traversal: false, retention: false, concurrent: false },customFieldDescriptor)` | ||
`new Validator(schema, options)` | ||
#### Arguments | ||
### options | ||
- source: `object` The data to validate | ||
- fieldName: `string` The name of field to validate | ||
- callback: `function(errors, data)` | ||
- option: `object` | ||
`option.first` default: `true`. If true, every field will not stop validation at first failed rule | ||
`option.traversal` If true, every field will not stop validation at first failed rule | ||
`option.retention` default: `false`. If true, the last pending validation will be not covered when a new validation begins | ||
`option.retention` If true, the last pending validation will be not covered when a new validation begins | ||
`option.concurrent` default: `false`. if true, all validator will be executed at the same time | ||
`option.concurrent` if true, all validator will be executed at the same time | ||
### validateItem | ||
#### Return | ||
`validateItem(source, fieldName, callback})` | ||
- promise: A promise that will be rejected if the validation fail | ||
### validate | ||
`validate(source, callback, specificField, options = { traversal: false, retention: false, concurrent: false },customFieldDescriptor)` | ||
#### Arguments | ||
- source: `object` The data to validate | ||
- fieldName: `string` The name of field to validate | ||
- callback: `function(errors, data)` | ||
- specificField: `array` The names of field to validate. If null, all fields will be validated. | ||
- option: `object` | ||
#### Return | ||
- promise: A promise that will be rejected if the validation fail | ||
- promise: A promise that will be rejected if the validation fails. |
161
src/index.js
export default class Validator { | ||
constructor(descriptor) { | ||
constructor(schema, options = { first: true, concurrent: false }) { | ||
this.lastValidator = {}; | ||
this.descriptor = descriptor; | ||
this.schema = schema; | ||
this.options = options; | ||
} | ||
validateItem( | ||
source, | ||
fieldName, | ||
callback, | ||
options = { traversal: false, retention: false, concurrent: false }, | ||
customFieldDescriptor, | ||
) { | ||
validate(target, rules, options = {}, ...other) { | ||
let errors = []; | ||
let promiseQueue; | ||
let rules = customFieldDescriptor || this.descriptor[fieldName]; | ||
if (!rules || rules.length < 1) { | ||
callback(errors, {}); | ||
return; | ||
return Promise.resolve({ | ||
value: target, | ||
}); | ||
} | ||
@@ -27,11 +22,6 @@ if (!Array.isArray(rules)) { | ||
rules.map(rule => { | ||
const ruleTarget = [fieldName, ...(rule.join || [])].map( | ||
name => source[name], | ||
return this.transformToPromise(rule, target, errors, ...other).then( | ||
value => value, | ||
options.first ? value => value : null, | ||
); | ||
return this.transformToPromise( | ||
fieldName, | ||
rule, | ||
ruleTarget, | ||
errors, | ||
).then(value => value, options.traversal ? value => value : null); | ||
}), | ||
@@ -41,10 +31,6 @@ ); | ||
promiseQueue = rules.reduce((queue, rule) => { | ||
const ruleTarget = [fieldName, ...(rule.join || [])].map( | ||
name => source[name], | ||
); | ||
return queue.then( | ||
() => this.transformToPromise(fieldName, rule, ruleTarget, errors), | ||
options.traversal | ||
? () => | ||
this.transformToPromise(fieldName, rule, ruleTarget, errors) | ||
() => this.transformToPromise(rule, target, errors, ...other), | ||
!options.first | ||
? () => this.transformToPromise(rule, target, errors, ...other) | ||
: null, | ||
@@ -54,15 +40,33 @@ ); | ||
} | ||
this.lastValidator[fieldName] = promiseQueue; | ||
return promiseQueue.then( | ||
return promiseQueue; | ||
} | ||
validateItem(source, fieldName, callback, ...other) { | ||
let rules = this.schema[fieldName]; | ||
let targets = source[fieldName]; | ||
if (rules && rules.rules) { | ||
rules = rules.rules; | ||
} | ||
if (rules && rules.withFields) { | ||
const fields = Array.isArray(rules.withFields) | ||
? rules.withFields | ||
: [rules.withFields]; | ||
targets = [fieldName, ...fields].map(name => source[name]); | ||
} | ||
const validation = this.validate(targets, rules, this.options, ...other); | ||
this.lastValidator[fieldName] = validation; | ||
return validation.then( | ||
value => { | ||
(options.retention || this.lastValidator[fieldName] === promiseQueue) && | ||
callback && | ||
callback(errors, value); | ||
if (callback && this.lastValidator[fieldName] === validation) { | ||
this.lastValidator[fieldName] = null; | ||
callback(null, value.target); | ||
} | ||
return value; | ||
}, | ||
value => { | ||
(options.retention || this.lastValidator[fieldName] === promiseQueue) && | ||
callback && | ||
callback(errors, value); | ||
return Promise.reject(value); | ||
if (callback && this.lastValidator[fieldName] === validation) { | ||
this.lastValidator[fieldName] = null; | ||
callback(value.errors, value.target); | ||
} | ||
return value; | ||
}, | ||
@@ -72,37 +76,34 @@ ); | ||
validate(source, callback, options = {}) { | ||
let hasError = false; | ||
const errors = {}; | ||
const promises = []; | ||
(options.specificField || Object.keys(this.descriptor)).forEach(name => { | ||
promises.push( | ||
this.validateItem( | ||
source, | ||
name, | ||
fieldError => { | ||
errors[name] = fieldError; | ||
options.fieldCallback && options.fieldCallback(name, fieldError); | ||
}, | ||
options, | ||
), | ||
); | ||
}); | ||
return Promise.all( | ||
promises.map(p => | ||
p.catch(v => { | ||
hasError = true; | ||
return v; | ||
}), | ||
), | ||
).then(() => { | ||
callback(errors, !hasError); | ||
return hasError ? Promise.reject(errors) : errors; | ||
}); | ||
} | ||
// validateAll(source, callback, options = {}) { | ||
// let hasError = false; | ||
// const errors = {}; | ||
// const promises = []; | ||
// (options.specificField || Object.keys(this.schema)).forEach(name => { | ||
// promises.push( | ||
// this.validateItem(source, name, fieldError => { | ||
// errors[name] = fieldError; | ||
// options.fieldCallback && options.fieldCallback(name, fieldError); | ||
// }), | ||
// ); | ||
// }); | ||
// return Promise.all( | ||
// promises.map(p => | ||
// p.catch(v => { | ||
// hasError = true; | ||
// return v; | ||
// }), | ||
// ), | ||
// ).then(() => { | ||
// callback(errors, !hasError); | ||
// return hasError ? null : errors; | ||
// }); | ||
// } | ||
messageHandler(result, message, target, promiseValue) { | ||
messageHandler(result, message, target, promiseValue, ...other) { | ||
if (typeof message === 'function') { | ||
result.push(`${message(...target, promiseValue) || 'Error!'}`.trim()); | ||
result.push( | ||
`${message(target, promiseValue, ...other) || 'Error!'}`.trim(), | ||
); | ||
} else { | ||
result.push(`${message || 'Error!'}`.trim()); | ||
result.push(`${message != null ? message : 'Error!'}`.trim()); | ||
} | ||
@@ -120,7 +121,8 @@ return result; | ||
transformToPromise = (fieldName, rule, target, errors) => { | ||
let ruleReturn = rule.validator(...target); | ||
let isAsync = true; | ||
transformToPromise = (rule, target, errors, ...other) => { | ||
if (typeof rule.validator !== 'function') { | ||
throw 'invalid validator'; | ||
} | ||
let ruleReturn = rule.validator(target, ...other); | ||
if (!ruleReturn || !ruleReturn.then || !ruleReturn.catch) { | ||
isAsync = false; | ||
ruleReturn = ruleReturn | ||
@@ -132,5 +134,3 @@ ? Promise.resolve(ruleReturn) | ||
errors, | ||
fieldName, | ||
target, | ||
isAsync, | ||
}; | ||
@@ -145,2 +145,3 @@ return ruleReturn.then( | ||
promiseValue, | ||
...other, | ||
); | ||
@@ -152,13 +153,9 @@ return Promise.reject(Object.assign(data, { promiseValue })); | ||
addRule(newRuleSet) { | ||
this.ruleSet = Object.assign(this.ruleSet, newRuleSet); | ||
setSchema(schema) { | ||
this.schema = schema; | ||
} | ||
updateRuleSet(newRuleSet) { | ||
this.ruleSet = newRuleSet; | ||
setOptions(options) { | ||
this.options = options; | ||
} | ||
deleteRule(ruleName) { | ||
delete this.ruleSet[ruleName]; | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18
20496
171
84
1