更新验证方式

This commit is contained in:
Harvey Zack committed 2018-10-31 12:25:50 +08:00
1 parent 87a68beeb7
commit c5c5d6cf04
6 files changed
+88 -80

No files matched your search

+77 -71
View File
@@ -524,82 +524,86 @@
return false;
}
class OptionValidator {
constructor(option, rule, paths = ['option']) {
this.validator(option, rule, paths);
function optionValidator(option, scheme, paths = ['option']) {
checkType(option, scheme, paths);
checkValidator(option, scheme, paths);
checkChild(option, scheme, paths);
for (const key in scheme) {
if (scheme.hasOwnProperty(key)) {
const optionValue = option[key];
const schemeValue = scheme[key];
const currentPath = paths.concat(key);
if (checkRequired(option, key, schemeValue, currentPath)) continue;
checkType(optionValue, schemeValue, currentPath);
checkValidator(optionValue, schemeValue, currentPath);
checkChild(optionValue, schemeValue, currentPath);
}
}
}
function checkRequired(option, key, schemeValue, paths) {
if (!Object.prototype.hasOwnProperty.call(option, key)) {
if (schemeValue.__required__ === true || schemeValue.required === true) {
throw new TypeError(`'${paths.join('.')}' is required`);
} else {
return true;
}
}
}
function checkType(optionValue, schemeValue, paths) {
let schemeType;
if (kindOf(schemeValue) === 'string') {
schemeType = schemeValue;
} else if (schemeValue.__type__) {
schemeType = schemeValue.__type__;
} else if (schemeValue.type) {
schemeType = schemeValue.type;
}
validator(option, rule, paths) {
const optionType = kindOf(option);
const ruleType = kindOf(rule);
const pathsType = kindOf(paths);
if (optionType !== 'object' && optionType !== 'array') {
throw new TypeError(`'option' only support object or array type, but got '${optionType}'`);
} else if (ruleType !== 'object') {
throw new TypeError(`'rule' only support object type, but got '${ruleType}'`);
} else if (pathsType !== 'array') {
throw new TypeError(`'paths' only support array type, but got '${pathsType}'`);
if (schemeType && kindOf(schemeType) === 'string') {
schemeType = schemeType.trim().toLowerCase();
const optionType = kindOf(optionValue);
if (optionType !== schemeType) {
throw new TypeError(`'${paths.join('.')}' require '${schemeType}' type, but got '${optionType}'`);
}
}
}
for (const key in rule) {
if (rule.hasOwnProperty(key)) {
const optionValue = option[key];
const optionType = kindOf(optionValue);
const ruleValue = rule[key];
function checkValidator(optionValue, schemeValue, paths) {
let schemeValidator;
if (schemeValue.___validator__) {
schemeValidator = schemeValue.___validator__;
} else if (schemeValue.validator) {
schemeValidator = schemeValue.validator;
}
if (!Object.prototype.hasOwnProperty.call(option, key)) {
if (ruleValue.__required__ === true || ruleValue.required === true) {
throw new TypeError(`'${paths.join('.')}.${key}' is required`);
} else {
return;
}
}
if (kindOf(schemeValidator) === 'function') {
const optionType = kindOf(optionValue);
const resule = schemeValidator(paths, optionValue, optionType);
if (resule !== true) {
throw new TypeError(`The scheme for '${paths.join('.')}' validator function require return true, but got '${resule}'`);
}
}
}
let ruleType;
if (kindOf(ruleValue) === 'string') {
ruleType = ruleValue;
} else if (ruleValue.__type__) {
ruleType = ruleValue.__type__;
} else if (ruleValue.type) {
ruleType = ruleValue.type;
}
function checkChild(optionValue, schemeValue, paths) {
let schemeChild;
if (schemeValue.___child__) {
schemeChild = schemeValue.___child__;
} else if (schemeValue.child) {
schemeChild = schemeValue.child;
}
if (ruleType && optionType !== ruleType) {
throw new TypeError(`'${paths.join('.')}.${key}' require '${ruleType}' type, but got '${optionType}'`);
}
let ruleValidator;
if (ruleValue.___validator__) {
ruleValidator = ruleValue.___validator__;
} else if (ruleValue.validator) {
ruleValidator = ruleValue.validator;
}
if (kindOf(ruleValidator) === 'function') {
const resule = ruleValidator(paths.concat(key), optionValue, optionType);
if (resule !== true) {
throw new TypeError(`The rule for '${paths.join('.')}.${key}' validator function require return true, but got '${resule}'`);
}
}
let ruleChild;
if (ruleValue.___child__) {
ruleChild = ruleValue.___child__;
} else if (ruleValue.child) {
ruleChild = ruleValue.child;
}
if (kindOf(ruleChild) === 'object') {
if (ruleType === 'object') {
this.validator(optionValue, ruleChild, paths.concat(key));
} else if (ruleType === 'array') {
optionValue.forEach((item, index) => {
this.validator(item, ruleChild, paths.concat(`${key}[${index}]`));
});
}
}
}
if (kindOf(schemeChild) === 'object') {
const optionType = kindOf(optionValue);
if (optionType === 'object') {
optionValidator(optionValue, schemeChild, paths);
} else if (optionType === 'array') {
optionValue.forEach((item, index) => {
optionValidator(item, schemeChild, paths.concat(index));
});
}
}
}
@@ -645,7 +649,9 @@
playbackRate: 'boolean',
aspectRatio: 'boolean',
loop: 'boolean',
type: 'string',
type: {
type: 'string'
},
mimeCodec: 'string',
layers: {
type: 'array',
@@ -2825,7 +2831,7 @@
_this.emit('init:start');
_this.option = deepMerge({}, Artplayer.DEFAULTS, option);
new OptionValidator(_this.option, scheme);
optionValidator(_this.option, scheme);
_this.init();
+1 -1
View File
@@ -1 +1 @@
{"version":3,"file":"artplayer.js","sources":["../../node_modules/@babel/runtime/helpers/setPrototypeOf.js","../../node_modules/@babel/runtime/helpers/construct.js","../../node_modules/@babel/runtime/helpers/arrayWithoutHoles.js","../../node_modules/@babel/runtime/helpers/iterableToArray.js","../../node_modules/@babel/runtime/helpers/nonIterableSpread.js","../../node_modules/@babel/runtime/helpers/toConsumableArray.js","../../node_modules/@babel/runtime/helpers/classCallCheck.js","../../node_modules/@babel/runtime/helpers/createClass.js","../../node_modules/@babel/runtime/helpers/typeof.js","../../node_modules/@babel/runtime/helpers/assertThisInitialized.js","../../node_modules/@babel/runtime/helpers/possibleConstructorReturn.js","../../node_modules/@babel/runtime/helpers/getPrototypeOf.js","../../node_modules/@babel/runtime/helpers/inherits.js","../../node_modules/@babel/runtime/helpers/defineProperty.js","../../node_modules/@babel/runtime/helpers/objectSpread.js","../../node_modules/@babel/runtime/helpers/isNativeFunction.js","../../node_modules/@babel/runtime/helpers/wrapNativeSuper.js","../../src/utils/index.js","../../node_modules/kind-of/index.js","../../node_modules/option-validator/index.js","../../src/scheme/validElement.js","../../src/scheme/index.js","../../src/config/mimeCodec.js","../../src/config/mse.js","../../src/config/video.js","../../src/config/index.js","../../node_modules/tiny-emitter/index.js","../../src/template.js","../../src/i18n.js","../../src/player.js","../../src/controls/danmu.js","../../src/controls/fullscreen.js","../../src/controls/pip.js","../../node_modules/@babel/runtime/helpers/arrayWithHoles.js","../../node_modules/@babel/runtime/helpers/iterableToArrayLimit.js","../../node_modules/@babel/runtime/helpers/nonIterableRest.js","../../node_modules/@babel/runtime/helpers/slicedToArray.js","../../src/icons/index.js","../../src/controls/playAndPause.js","../../src/controls/progress.js","../../src/controls/subtitle.js","../../src/controls/time.js","../../src/controls/volume.js","../../src/controls/setting.js","../../src/controls/thumbnails.js","../../src/controls/screenshot.js","../../src/controls/index.js","../../src/contextmenu.js","../../src/danmu.js","../../src/info.js","../../src/subtitle.js","../../src/events.js","../../src/hotkey.js","../../src/layers.js","../../src/loading.js","../../src/notice.js","../../src/mask.js","../../src/index.js"],"sourcesContent":["function _setPrototypeOf(o, p) {\n module.exports = _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {\n o.__proto__ = p;\n return o;\n };\n\n return _setPrototypeOf(o, p);\n}\n\nmodule.exports = _setPrototypeOf;","var setPrototypeOf = require(\"./setPrototypeOf\");\n\nfunction isNativeReflectConstruct() {\n if (typeof Reflect === \"undefined\" || !Reflect.construct) return false;\n if (Reflect.construct.sham) return false;\n if (typeof Proxy === \"function\") return true;\n\n try {\n Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));\n return true;\n } catch (e) {\n return false;\n }\n}\n\nfunction _construct(Parent, args, Class) {\n if (isNativeReflectConstruct()) {\n module.exports = _construct = Reflect.construct;\n } else {\n module.exports = _construct = function _construct(Parent, args, Class) {\n var a = [null];\n a.push.apply(a, args);\n var Constructor = Function.bind.apply(Parent, a);\n var instance = new Constructor();\n if (Class) setPrototypeOf(instance, Class.prototype);\n return instance;\n };\n }\n\n return _construct.apply(null, arguments);\n}\n\nmodule.exports = _construct;","function _arrayWithoutHoles(arr) {\n if (Array.isArray(arr)) {\n for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) {\n arr2[i] = arr[i];\n }\n\n return arr2;\n }\n}\n\nmodule.exports = _arrayWithoutHoles;","function _iterableToArray(iter) {\n if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === \"[object Arguments]\") return Array.from(iter);\n}\n\nmodule.exports = _iterableToArray;","function _nonIterableSpread() {\n throw new TypeError(\"Invalid attempt to spread non-iterable instance\");\n}\n\nmodule.exports = _nonIterableSpread;","var arrayWithoutHoles = require(\"./arrayWithoutHoles\");\n\nvar iterableToArray = require(\"./iterableToArray\");\n\nvar nonIterableSpread = require(\"./nonIterableSpread\");\n\nfunction _toConsumableArray(arr) {\n return arrayWithoutHoles(arr) || iterableToArray(arr) || nonIterableSpread();\n}\n\nmodule.exports = _toConsumableArray;","function _classCallCheck(instance, Constructor) {\n if (!(instance instanceof Constructor)) {\n throw new TypeError(\"Cannot call a class as a function\");\n }\n}\n\nmodule.exports = _classCallCheck;","function _defineProperties(target, props) {\n for (var i = 0; i < props.length; i++) {\n var descriptor = props[i];\n descriptor.enumerable = descriptor.enumerable || false;\n Line truncated
{"version":3,"file":"artplayer.js","sources":["../../node_modules/@babel/runtime/helpers/setPrototypeOf.js","../../node_modules/@babel/runtime/helpers/construct.js","../../node_modules/@babel/runtime/helpers/arrayWithoutHoles.js","../../node_modules/@babel/runtime/helpers/iterableToArray.js","../../node_modules/@babel/runtime/helpers/nonIterableSpread.js","../../node_modules/@babel/runtime/helpers/toConsumableArray.js","../../node_modules/@babel/runtime/helpers/classCallCheck.js","../../node_modules/@babel/runtime/helpers/createClass.js","../../node_modules/@babel/runtime/helpers/typeof.js","../../node_modules/@babel/runtime/helpers/assertThisInitialized.js","../../node_modules/@babel/runtime/helpers/possibleConstructorReturn.js","../../node_modules/@babel/runtime/helpers/getPrototypeOf.js","../../node_modules/@babel/runtime/helpers/inherits.js","../../node_modules/@babel/runtime/helpers/defineProperty.js","../../node_modules/@babel/runtime/helpers/objectSpread.js","../../node_modules/@babel/runtime/helpers/isNativeFunction.js","../../node_modules/@babel/runtime/helpers/wrapNativeSuper.js","../../src/utils/index.js","../../node_modules/kind-of/index.js","../../node_modules/option-validator/index.js","../../src/scheme/validElement.js","../../src/scheme/index.js","../../src/config/mimeCodec.js","../../src/config/mse.js","../../src/config/video.js","../../src/config/index.js","../../node_modules/tiny-emitter/index.js","../../src/template.js","../../src/i18n.js","../../src/player.js","../../src/controls/danmu.js","../../src/controls/fullscreen.js","../../src/controls/pip.js","../../node_modules/@babel/runtime/helpers/arrayWithHoles.js","../../node_modules/@babel/runtime/helpers/iterableToArrayLimit.js","../../node_modules/@babel/runtime/helpers/nonIterableRest.js","../../node_modules/@babel/runtime/helpers/slicedToArray.js","../../src/icons/index.js","../../src/controls/playAndPause.js","../../src/controls/progress.js","../../src/controls/subtitle.js","../../src/controls/time.js","../../src/controls/volume.js","../../src/controls/setting.js","../../src/controls/thumbnails.js","../../src/controls/screenshot.js","../../src/controls/index.js","../../src/contextmenu.js","../../src/danmu.js","../../src/info.js","../../src/subtitle.js","../../src/events.js","../../src/hotkey.js","../../src/layers.js","../../src/loading.js","../../src/notice.js","../../src/mask.js","../../src/index.js"],"sourcesContent":["function _setPrototypeOf(o, p) {\n module.exports = _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {\n o.__proto__ = p;\n return o;\n };\n\n return _setPrototypeOf(o, p);\n}\n\nmodule.exports = _setPrototypeOf;","var setPrototypeOf = require(\"./setPrototypeOf\");\n\nfunction isNativeReflectConstruct() {\n if (typeof Reflect === \"undefined\" || !Reflect.construct) return false;\n if (Reflect.construct.sham) return false;\n if (typeof Proxy === \"function\") return true;\n\n try {\n Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));\n return true;\n } catch (e) {\n return false;\n }\n}\n\nfunction _construct(Parent, args, Class) {\n if (isNativeReflectConstruct()) {\n module.exports = _construct = Reflect.construct;\n } else {\n module.exports = _construct = function _construct(Parent, args, Class) {\n var a = [null];\n a.push.apply(a, args);\n var Constructor = Function.bind.apply(Parent, a);\n var instance = new Constructor();\n if (Class) setPrototypeOf(instance, Class.prototype);\n return instance;\n };\n }\n\n return _construct.apply(null, arguments);\n}\n\nmodule.exports = _construct;","function _arrayWithoutHoles(arr) {\n if (Array.isArray(arr)) {\n for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) {\n arr2[i] = arr[i];\n }\n\n return arr2;\n }\n}\n\nmodule.exports = _arrayWithoutHoles;","function _iterableToArray(iter) {\n if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === \"[object Arguments]\") return Array.from(iter);\n}\n\nmodule.exports = _iterableToArray;","function _nonIterableSpread() {\n throw new TypeError(\"Invalid attempt to spread non-iterable instance\");\n}\n\nmodule.exports = _nonIterableSpread;","var arrayWithoutHoles = require(\"./arrayWithoutHoles\");\n\nvar iterableToArray = require(\"./iterableToArray\");\n\nvar nonIterableSpread = require(\"./nonIterableSpread\");\n\nfunction _toConsumableArray(arr) {\n return arrayWithoutHoles(arr) || iterableToArray(arr) || nonIterableSpread();\n}\n\nmodule.exports = _toConsumableArray;","function _classCallCheck(instance, Constructor) {\n if (!(instance instanceof Constructor)) {\n throw new TypeError(\"Cannot call a class as a function\");\n }\n}\n\nmodule.exports = _classCallCheck;","function _defineProperties(target, props) {\n for (var i = 0; i < props.length; i++) {\n var descriptor = props[i];\n descriptor.enumerable = descriptor.enumerable || false;\n Line truncated