Comparing version 0.5.2 to 0.6.0
@@ -9,2 +9,6 @@ 'use strict'; | ||
var _objectAssign = require('object-assign'); | ||
var _objectAssign2 = _interopRequireDefault(_objectAssign); | ||
var _utils = require('./utils'); | ||
@@ -18,3 +22,4 @@ | ||
return (0, _utils.toType)({ | ||
type: null | ||
type: null, | ||
name: 'any' | ||
}); | ||
@@ -26,2 +31,3 @@ }, | ||
type: Function, | ||
name: 'function', | ||
default: _utils.noop | ||
@@ -34,2 +40,3 @@ }); | ||
type: Boolean, | ||
name: 'boolean', | ||
default: true | ||
@@ -42,2 +49,3 @@ }); | ||
type: String, | ||
name: 'string', | ||
default: '' | ||
@@ -50,2 +58,3 @@ }); | ||
type: Number, | ||
name: 'number', | ||
default: 0 | ||
@@ -58,2 +67,3 @@ }); | ||
type: Array, | ||
name: 'array', | ||
default: Array | ||
@@ -66,2 +76,3 @@ }); | ||
type: Object, | ||
name: 'object', | ||
default: Object | ||
@@ -74,2 +85,3 @@ }); | ||
type: Number, | ||
name: 'integer', | ||
validator: function validator(value) { | ||
@@ -83,9 +95,16 @@ return (0, _utils.isInteger)(value); | ||
custom: function custom(validator) { | ||
if (typeof validator !== 'function') { | ||
throw new TypeError('You must provide a function as argument'); | ||
custom: function custom(validatorFn) { | ||
var warnMsg = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'custom validation failed'; | ||
if (typeof validatorFn !== 'function') { | ||
throw new TypeError('[VueTypes error]: You must provide a function as argument'); | ||
} | ||
return (0, _utils.toType)({ | ||
validator: validator | ||
name: validatorFn.name || '<<anonymous function>>', | ||
validator: function validator() { | ||
var valid = validatorFn.apply(undefined, arguments); | ||
if (!valid) (0, _utils.warn)(warnMsg); | ||
return valid; | ||
} | ||
}); | ||
@@ -95,7 +114,20 @@ }, | ||
if (!(0, _utils.isArray)(arr)) { | ||
throw new TypeError('You must provide an array as argument'); | ||
throw new TypeError('[VueTypes error]: You must provide an array as argument'); | ||
} | ||
var msg = 'value should be one of "' + arr.join('", "') + '"'; | ||
var allowedTypes = arr.reduce(function (ret, v) { | ||
if (v !== null && v !== undefined) { | ||
ret.indexOf(v.constructor) === -1 && ret.push(v.constructor); | ||
} | ||
return ret; | ||
}, []); | ||
return this.custom(function (value) { | ||
return arr.indexOf(value) !== -1; | ||
return (0, _utils.toType)({ | ||
name: 'oneOf', | ||
type: allowedTypes.length > 0 ? allowedTypes : null, | ||
validator: function validator(value) { | ||
var valid = arr.indexOf(value) !== -1; | ||
if (!valid) (0, _utils.warn)(msg); | ||
return valid; | ||
} | ||
}); | ||
@@ -105,2 +137,3 @@ }, | ||
return (0, _utils.toType)({ | ||
name: 'instanceOf', | ||
type: instanceConstructor | ||
@@ -111,21 +144,29 @@ }); | ||
if (!(0, _utils.isArray)(arr)) { | ||
throw new TypeError('You must provide an array as argument'); | ||
throw new TypeError('[VueTypes error]: You must provide an array as argument'); | ||
} | ||
var nativeChecks = arr.map(function (type) { | ||
var hasCustomValidators = false; | ||
var nativeChecks = arr.reduce(function (ret, type, i) { | ||
if ((0, _lodash2.default)(type)) { | ||
if (type.name === 'oneOf') { | ||
return ret.concat(type.type || []); | ||
} | ||
if (type.type && !(0, _utils.isFunction)(type.validator)) { | ||
return type.type; | ||
if ((0, _utils.isArray)(type.type)) return ret.concat(type.type); | ||
ret.push(type.type); | ||
} else if ((0, _utils.isFunction)(type.validator)) { | ||
hasCustomValidators = true; | ||
} | ||
return null; | ||
return ret; | ||
} | ||
return type; | ||
}).filter(function (type) { | ||
return !!type; | ||
}); | ||
ret.push(type); | ||
return ret; | ||
}, []); | ||
if (nativeChecks.length === arr.length) { | ||
if (!hasCustomValidators) { | ||
// we got just native objects (ie: Array, Object) | ||
// delegate to Vue native prop check | ||
return (0, _utils.toType)({ | ||
name: 'oneOfType', | ||
type: nativeChecks | ||
@@ -135,6 +176,20 @@ }); | ||
return this.custom(function (value) { | ||
return arr.some(function (type) { | ||
return (0, _utils.validateType)(type, value); | ||
var typesStr = arr.map(function (type) { | ||
if (type && (0, _utils.isArray)(type.type)) { | ||
return type.type.map(_utils.getType); | ||
} | ||
return (0, _utils.getType)(type); | ||
}).reduce(function (ret, type) { | ||
return ret.concat((0, _utils.isArray)(type) ? type : [type]); | ||
}, []).join('", "'); | ||
return this.custom(function oneOfType(value) { | ||
var valid = arr.some(function (type) { | ||
if (type.name === 'oneOf') { | ||
return type.type ? (0, _utils.validateType)(type.type, value, true) : true; | ||
} | ||
return (0, _utils.validateType)(type, value, true); | ||
}); | ||
if (!valid) (0, _utils.warn)('value type should be one of "' + typesStr + '"'); | ||
return valid; | ||
}); | ||
@@ -144,7 +199,10 @@ }, | ||
return (0, _utils.toType)({ | ||
name: 'arrayOf', | ||
type: Array, | ||
validator: function validator(values) { | ||
return values.every(function (value) { | ||
var valid = values.every(function (value) { | ||
return (0, _utils.validateType)(type, value); | ||
}); | ||
if (!valid) (0, _utils.warn)('value must be an array of \'' + (0, _utils.getType)(type) + '\''); | ||
return valid; | ||
} | ||
@@ -155,7 +213,10 @@ }); | ||
return (0, _utils.toType)({ | ||
name: 'objectOf', | ||
type: Object, | ||
validator: function validator(obj) { | ||
return Object.keys(obj).every(function (key) { | ||
var valid = Object.keys(obj).every(function (key) { | ||
return (0, _utils.validateType)(type, obj[key]); | ||
}); | ||
if (!valid) (0, _utils.warn)('value must be an object of \'' + (0, _utils.getType)(type) + '\''); | ||
return valid; | ||
} | ||
@@ -170,5 +231,8 @@ }); | ||
return (0, _utils.toType)({ | ||
var type = (0, _utils.toType)({ | ||
name: 'shape', | ||
type: Object, | ||
validator: function validator(value) { | ||
var _this = this; | ||
if (!(0, _lodash2.default)(value)) { | ||
@@ -183,2 +247,3 @@ return false; | ||
})) { | ||
(0, _utils.warn)('at least one of required properties "' + requiredKeys.join('", "') + '" is not present'); | ||
return false; | ||
@@ -189,2 +254,4 @@ } | ||
if (keys.indexOf(key) === -1) { | ||
if (_this.isLoose === true) return true; | ||
(0, _utils.warn)('object is missing "' + key + '" property'); | ||
return false; | ||
@@ -197,2 +264,15 @@ } | ||
}); | ||
Object.defineProperty(type, 'loose', { | ||
get: function get() { | ||
var t = (0, _objectAssign2.default)({ isLoose: true }, this); | ||
(0, _utils.withRequired)(t); | ||
(0, _utils.withDefault)(t); | ||
return t; | ||
}, | ||
enumerable: false | ||
}); | ||
return type; | ||
} | ||
@@ -199,0 +279,0 @@ }; |
'use strict'; | ||
exports.__esModule = true; | ||
exports.validateType = exports.toType = exports.withRequired = exports.withDefault = exports.isFunction = exports.isArray = exports.isInteger = exports.has = exports.noop = exports.hasOwn = undefined; | ||
exports.warn = exports.validateType = exports.toType = exports.withRequired = exports.withDefault = exports.isFunction = exports.isArray = exports.isInteger = exports.has = exports.noop = exports.getNativeType = exports.getType = exports.hasOwn = undefined; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _lodash = require('lodash.isplainobject'); | ||
@@ -25,7 +23,14 @@ | ||
// https://github.com/vuejs/vue/blob/dev/src/core/util/props.js#L159 | ||
var getType = function getType(fn) { | ||
var match = fn && fn.toString().match(FN_MATCH_REGEXP); | ||
var getType = exports.getType = function getType(fn) { | ||
var type = fn !== null && fn !== undefined ? fn.type ? fn.type : fn : null; | ||
var match = type && type.toString().match(FN_MATCH_REGEXP); | ||
return match && match[1]; | ||
}; | ||
var getNativeType = exports.getNativeType = function getNativeType(value) { | ||
if (value === null || value === undefined) return null; | ||
var match = value.constructor.toString().match(FN_MATCH_REGEXP); | ||
return match && match[1]; | ||
}; | ||
/** | ||
@@ -70,3 +75,3 @@ * No-op function | ||
* | ||
* @param {any} val - Value to check | ||
* @param {any} value - Value to check | ||
* @returns {boolean} | ||
@@ -87,3 +92,3 @@ */ | ||
if (!validateType(this, def)) { | ||
console.warn('default value not allowed here', def); // eslint-disable-line no-console | ||
warn('invalid default value', def); | ||
return type; | ||
@@ -141,8 +146,11 @@ } | ||
* @param {*} value - Value to check | ||
* @param {boolean} silent - Silence warnings | ||
* @returns {boolean} | ||
*/ | ||
var validateType = exports.validateType = function validateType(type, value) { | ||
var silent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
var typeToCheck = type; | ||
var valid = true; | ||
var expectedType = void 0; | ||
if (!(0, _lodash2.default)(type)) { | ||
@@ -153,12 +161,21 @@ typeToCheck = { type: type }; | ||
if (hasOwn.call(typeToCheck, 'type') && typeToCheck.type !== null) { | ||
var expectedType = getType(typeToCheck.type); | ||
if (isArray(typeToCheck.type)) { | ||
valid = typeToCheck.type.some(function (type) { | ||
return validateType(type, value, true); | ||
}); | ||
expectedType = typeToCheck.type.map(function (type) { | ||
return getType(type); | ||
}).join(' or '); | ||
} else { | ||
expectedType = getType(typeToCheck); | ||
if (expectedType === 'Array') { | ||
valid = isArray(value); | ||
} else if (expectedType === 'Object') { | ||
valid = (0, _lodash2.default)(value); | ||
} else if (expectedType === 'String' || expectedType === 'Number' || expectedType === 'Boolean' || expectedType === 'Function') { | ||
valid = (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === expectedType.toLowerCase(); | ||
} else { | ||
valid = value instanceof typeToCheck.type; | ||
if (expectedType === 'Array') { | ||
valid = isArray(value); | ||
} else if (expectedType === 'Object') { | ||
valid = (0, _lodash2.default)(value); | ||
} else if (expectedType === 'String' || expectedType === 'Number' || expectedType === 'Boolean' || expectedType === 'Function') { | ||
valid = getNativeType(value) === expectedType; | ||
} else { | ||
valid = value instanceof typeToCheck.type; | ||
} | ||
} | ||
@@ -168,2 +185,3 @@ } | ||
if (!valid) { | ||
silent === false && warn('value "' + value + '" should be of type \'' + expectedType + '\''); | ||
return false; | ||
@@ -173,5 +191,22 @@ } | ||
if (hasOwn.call(typeToCheck, 'validator') && isFunction(typeToCheck.validator)) { | ||
return typeToCheck.validator(value); | ||
valid = typeToCheck.validator(value); | ||
if (!valid && silent === false) warn('custom validation failed'); | ||
return valid; | ||
} | ||
return valid; | ||
}; | ||
}; | ||
var warn = noop; | ||
if (process.env.NODE_ENV !== 'production') { | ||
(function () { | ||
var hasConsole = typeof console !== 'undefined'; | ||
exports.warn = warn = function warn(msg) { | ||
if (hasConsole) { | ||
console.warn('[VueTypes warn]: ' + msg); | ||
} | ||
}; | ||
})(); | ||
} | ||
exports.warn = warn; |
125
es/index.js
import isPlainObject from 'lodash.isplainobject'; | ||
import { noop, toType, isFunction, validateType, isInteger, isArray } from './utils'; | ||
import objectAssign from 'object-assign'; | ||
import { noop, toType, getType, withDefault, withRequired, isFunction, validateType, isInteger, isArray, warn } from './utils'; | ||
@@ -8,3 +9,4 @@ var VuePropTypes = { | ||
return toType({ | ||
type: null | ||
type: null, | ||
name: 'any' | ||
}); | ||
@@ -16,2 +18,3 @@ }, | ||
type: Function, | ||
name: 'function', | ||
default: noop | ||
@@ -24,2 +27,3 @@ }); | ||
type: Boolean, | ||
name: 'boolean', | ||
default: true | ||
@@ -32,2 +36,3 @@ }); | ||
type: String, | ||
name: 'string', | ||
default: '' | ||
@@ -40,2 +45,3 @@ }); | ||
type: Number, | ||
name: 'number', | ||
default: 0 | ||
@@ -48,2 +54,3 @@ }); | ||
type: Array, | ||
name: 'array', | ||
default: Array | ||
@@ -56,2 +63,3 @@ }); | ||
type: Object, | ||
name: 'object', | ||
default: Object | ||
@@ -64,2 +72,3 @@ }); | ||
type: Number, | ||
name: 'integer', | ||
validator: function validator(value) { | ||
@@ -73,9 +82,16 @@ return isInteger(value); | ||
custom: function custom(validator) { | ||
if (typeof validator !== 'function') { | ||
throw new TypeError('You must provide a function as argument'); | ||
custom: function custom(validatorFn) { | ||
var warnMsg = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'custom validation failed'; | ||
if (typeof validatorFn !== 'function') { | ||
throw new TypeError('[VueTypes error]: You must provide a function as argument'); | ||
} | ||
return toType({ | ||
validator: validator | ||
name: validatorFn.name || '<<anonymous function>>', | ||
validator: function validator() { | ||
var valid = validatorFn.apply(undefined, arguments); | ||
if (!valid) warn(warnMsg); | ||
return valid; | ||
} | ||
}); | ||
@@ -85,7 +101,20 @@ }, | ||
if (!isArray(arr)) { | ||
throw new TypeError('You must provide an array as argument'); | ||
throw new TypeError('[VueTypes error]: You must provide an array as argument'); | ||
} | ||
var msg = 'value should be one of "' + arr.join('", "') + '"'; | ||
var allowedTypes = arr.reduce(function (ret, v) { | ||
if (v !== null && v !== undefined) { | ||
ret.indexOf(v.constructor) === -1 && ret.push(v.constructor); | ||
} | ||
return ret; | ||
}, []); | ||
return this.custom(function (value) { | ||
return arr.indexOf(value) !== -1; | ||
return toType({ | ||
name: 'oneOf', | ||
type: allowedTypes.length > 0 ? allowedTypes : null, | ||
validator: function validator(value) { | ||
var valid = arr.indexOf(value) !== -1; | ||
if (!valid) warn(msg); | ||
return valid; | ||
} | ||
}); | ||
@@ -95,2 +124,3 @@ }, | ||
return toType({ | ||
name: 'instanceOf', | ||
type: instanceConstructor | ||
@@ -101,21 +131,29 @@ }); | ||
if (!isArray(arr)) { | ||
throw new TypeError('You must provide an array as argument'); | ||
throw new TypeError('[VueTypes error]: You must provide an array as argument'); | ||
} | ||
var nativeChecks = arr.map(function (type) { | ||
var hasCustomValidators = false; | ||
var nativeChecks = arr.reduce(function (ret, type, i) { | ||
if (isPlainObject(type)) { | ||
if (type.name === 'oneOf') { | ||
return ret.concat(type.type || []); | ||
} | ||
if (type.type && !isFunction(type.validator)) { | ||
return type.type; | ||
if (isArray(type.type)) return ret.concat(type.type); | ||
ret.push(type.type); | ||
} else if (isFunction(type.validator)) { | ||
hasCustomValidators = true; | ||
} | ||
return null; | ||
return ret; | ||
} | ||
return type; | ||
}).filter(function (type) { | ||
return !!type; | ||
}); | ||
ret.push(type); | ||
return ret; | ||
}, []); | ||
if (nativeChecks.length === arr.length) { | ||
if (!hasCustomValidators) { | ||
// we got just native objects (ie: Array, Object) | ||
// delegate to Vue native prop check | ||
return toType({ | ||
name: 'oneOfType', | ||
type: nativeChecks | ||
@@ -125,6 +163,20 @@ }); | ||
return this.custom(function (value) { | ||
return arr.some(function (type) { | ||
return validateType(type, value); | ||
var typesStr = arr.map(function (type) { | ||
if (type && isArray(type.type)) { | ||
return type.type.map(getType); | ||
} | ||
return getType(type); | ||
}).reduce(function (ret, type) { | ||
return ret.concat(isArray(type) ? type : [type]); | ||
}, []).join('", "'); | ||
return this.custom(function oneOfType(value) { | ||
var valid = arr.some(function (type) { | ||
if (type.name === 'oneOf') { | ||
return type.type ? validateType(type.type, value, true) : true; | ||
} | ||
return validateType(type, value, true); | ||
}); | ||
if (!valid) warn('value type should be one of "' + typesStr + '"'); | ||
return valid; | ||
}); | ||
@@ -134,7 +186,10 @@ }, | ||
return toType({ | ||
name: 'arrayOf', | ||
type: Array, | ||
validator: function validator(values) { | ||
return values.every(function (value) { | ||
var valid = values.every(function (value) { | ||
return validateType(type, value); | ||
}); | ||
if (!valid) warn('value must be an array of \'' + getType(type) + '\''); | ||
return valid; | ||
} | ||
@@ -145,7 +200,10 @@ }); | ||
return toType({ | ||
name: 'objectOf', | ||
type: Object, | ||
validator: function validator(obj) { | ||
return Object.keys(obj).every(function (key) { | ||
var valid = Object.keys(obj).every(function (key) { | ||
return validateType(type, obj[key]); | ||
}); | ||
if (!valid) warn('value must be an object of \'' + getType(type) + '\''); | ||
return valid; | ||
} | ||
@@ -160,5 +218,8 @@ }); | ||
return toType({ | ||
var type = toType({ | ||
name: 'shape', | ||
type: Object, | ||
validator: function validator(value) { | ||
var _this = this; | ||
if (!isPlainObject(value)) { | ||
@@ -173,2 +234,3 @@ return false; | ||
})) { | ||
warn('at least one of required properties "' + requiredKeys.join('", "') + '" is not present'); | ||
return false; | ||
@@ -179,2 +241,4 @@ } | ||
if (keys.indexOf(key) === -1) { | ||
if (_this.isLoose === true) return true; | ||
warn('object is missing "' + key + '" property'); | ||
return false; | ||
@@ -187,2 +251,15 @@ } | ||
}); | ||
Object.defineProperty(type, 'loose', { | ||
get: function get() { | ||
var t = objectAssign({ isLoose: true }, this); | ||
withRequired(t); | ||
withDefault(t); | ||
return t; | ||
}, | ||
enumerable: false | ||
}); | ||
return type; | ||
} | ||
@@ -189,0 +266,0 @@ }; |
@@ -1,3 +0,1 @@ | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
import isPlainObject from 'lodash.isplainobject'; | ||
@@ -13,7 +11,14 @@ import objectAssign from 'object-assign'; | ||
// https://github.com/vuejs/vue/blob/dev/src/core/util/props.js#L159 | ||
var getType = function getType(fn) { | ||
var match = fn && fn.toString().match(FN_MATCH_REGEXP); | ||
export var getType = function getType(fn) { | ||
var type = fn !== null && fn !== undefined ? fn.type ? fn.type : fn : null; | ||
var match = type && type.toString().match(FN_MATCH_REGEXP); | ||
return match && match[1]; | ||
}; | ||
export var getNativeType = function getNativeType(value) { | ||
if (value === null || value === undefined) return null; | ||
var match = value.constructor.toString().match(FN_MATCH_REGEXP); | ||
return match && match[1]; | ||
}; | ||
/** | ||
@@ -58,3 +63,3 @@ * No-op function | ||
* | ||
* @param {any} val - Value to check | ||
* @param {any} value - Value to check | ||
* @returns {boolean} | ||
@@ -75,3 +80,3 @@ */ | ||
if (!validateType(this, def)) { | ||
console.warn('default value not allowed here', def); // eslint-disable-line no-console | ||
warn('invalid default value', def); | ||
return type; | ||
@@ -129,8 +134,11 @@ } | ||
* @param {*} value - Value to check | ||
* @param {boolean} silent - Silence warnings | ||
* @returns {boolean} | ||
*/ | ||
export var validateType = function validateType(type, value) { | ||
var silent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
var typeToCheck = type; | ||
var valid = true; | ||
var expectedType = void 0; | ||
if (!isPlainObject(type)) { | ||
@@ -141,12 +149,21 @@ typeToCheck = { type: type }; | ||
if (hasOwn.call(typeToCheck, 'type') && typeToCheck.type !== null) { | ||
var expectedType = getType(typeToCheck.type); | ||
if (isArray(typeToCheck.type)) { | ||
valid = typeToCheck.type.some(function (type) { | ||
return validateType(type, value, true); | ||
}); | ||
expectedType = typeToCheck.type.map(function (type) { | ||
return getType(type); | ||
}).join(' or '); | ||
} else { | ||
expectedType = getType(typeToCheck); | ||
if (expectedType === 'Array') { | ||
valid = isArray(value); | ||
} else if (expectedType === 'Object') { | ||
valid = isPlainObject(value); | ||
} else if (expectedType === 'String' || expectedType === 'Number' || expectedType === 'Boolean' || expectedType === 'Function') { | ||
valid = (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === expectedType.toLowerCase(); | ||
} else { | ||
valid = value instanceof typeToCheck.type; | ||
if (expectedType === 'Array') { | ||
valid = isArray(value); | ||
} else if (expectedType === 'Object') { | ||
valid = isPlainObject(value); | ||
} else if (expectedType === 'String' || expectedType === 'Number' || expectedType === 'Boolean' || expectedType === 'Function') { | ||
valid = getNativeType(value) === expectedType; | ||
} else { | ||
valid = value instanceof typeToCheck.type; | ||
} | ||
} | ||
@@ -156,2 +173,3 @@ } | ||
if (!valid) { | ||
silent === false && warn('value "' + value + '" should be of type \'' + expectedType + '\''); | ||
return false; | ||
@@ -161,5 +179,22 @@ } | ||
if (hasOwn.call(typeToCheck, 'validator') && isFunction(typeToCheck.validator)) { | ||
return typeToCheck.validator(value); | ||
valid = typeToCheck.validator(value); | ||
if (!valid && silent === false) warn('custom validation failed'); | ||
return valid; | ||
} | ||
return valid; | ||
}; | ||
}; | ||
var warn = noop; | ||
if (process.env.NODE_ENV !== 'production') { | ||
(function () { | ||
var hasConsole = typeof console !== 'undefined'; | ||
warn = function warn(msg) { | ||
if (hasConsole) { | ||
console.warn('[VueTypes warn]: ' + msg); | ||
} | ||
}; | ||
})(); | ||
} | ||
export { warn }; |
{ | ||
"name": "vue-types", | ||
"version": "0.5.2", | ||
"version": "0.6.0", | ||
"description": "Prop types utility for Vue", | ||
@@ -5,0 +5,0 @@ "author": "Marco Solazzi", |
@@ -258,2 +258,23 @@ # vue-types | ||
By default `.shape()` won't validate objects with properties not defined in the shape. To allow partial matching use the `loose` flag: | ||
```js | ||
export default { | ||
props: { | ||
userData: VueTypes.shape({ | ||
name: String, | ||
id: VueTypes.integer.isRequired | ||
}), | ||
userDataLoose: VueTypes.shape({ | ||
name: String, | ||
id: VueTypes.integer.isRequired | ||
}).loose | ||
} | ||
} | ||
//accepts: userData = {name: 'John', id: 1} | ||
//rejects: userData = {name: 'John', age: 30, id: 1} | ||
//accepts: userData2 = {name: 'John', age: 30, id: 1} --> loose matching | ||
``` | ||
## License | ||
@@ -260,0 +281,0 @@ |
116
src/index.js
import isPlainObject from 'lodash.isplainobject' | ||
import { noop, toType, isFunction, validateType, isInteger, isArray } from './utils' | ||
import objectAssign from 'object-assign' | ||
import { noop, toType, getType, withDefault, withRequired, isFunction, validateType, isInteger, isArray, warn } from './utils' | ||
@@ -8,3 +9,4 @@ const VuePropTypes = { | ||
return toType({ | ||
type: null | ||
type: null, | ||
name: 'any' | ||
}) | ||
@@ -16,2 +18,3 @@ }, | ||
type: Function, | ||
name: 'function', | ||
default: noop | ||
@@ -24,2 +27,3 @@ }) | ||
type: Boolean, | ||
name: 'boolean', | ||
default: true | ||
@@ -32,2 +36,3 @@ }) | ||
type: String, | ||
name: 'string', | ||
default: '' | ||
@@ -40,2 +45,3 @@ }) | ||
type: Number, | ||
name: 'number', | ||
default: 0 | ||
@@ -48,2 +54,3 @@ }) | ||
type: Array, | ||
name: 'array', | ||
default: Array | ||
@@ -56,2 +63,3 @@ }) | ||
type: Object, | ||
name: 'object', | ||
default: Object | ||
@@ -64,2 +72,3 @@ }) | ||
type: Number, | ||
name: 'integer', | ||
validator(value) { | ||
@@ -72,9 +81,14 @@ return isInteger(value) | ||
custom(validator) { | ||
if (typeof validator !== 'function') { | ||
throw new TypeError('You must provide a function as argument') | ||
custom(validatorFn, warnMsg = 'custom validation failed') { | ||
if (typeof validatorFn !== 'function') { | ||
throw new TypeError('[VueTypes error]: You must provide a function as argument') | ||
} | ||
return toType({ | ||
validator | ||
name: validatorFn.name || '<<anonymous function>>', | ||
validator(...args) { | ||
const valid = validatorFn(...args) | ||
if (!valid) warn(warnMsg) | ||
return valid | ||
} | ||
}) | ||
@@ -85,6 +99,21 @@ }, | ||
if (!isArray(arr)) { | ||
throw new TypeError('You must provide an array as argument') | ||
throw new TypeError('[VueTypes error]: You must provide an array as argument') | ||
} | ||
const msg = `value should be one of "${arr.join('", "')}"` | ||
const allowedTypes = arr.reduce((ret, v) => { | ||
if (v !== null && v !== undefined) { | ||
ret.indexOf(v.constructor) === -1 && ret.push(v.constructor) | ||
} | ||
return ret | ||
}, []) | ||
return this.custom((value) => arr.indexOf(value) !== -1) | ||
return toType({ | ||
name: 'oneOf', | ||
type: allowedTypes.length > 0 ? allowedTypes : null, | ||
validator(value) { | ||
const valid = arr.indexOf(value) !== -1 | ||
if (!valid) warn(msg) | ||
return valid | ||
} | ||
}) | ||
}, | ||
@@ -94,2 +123,3 @@ | ||
return toType({ | ||
name: 'instanceOf', | ||
type: instanceConstructor | ||
@@ -101,19 +131,29 @@ }) | ||
if (!isArray(arr)) { | ||
throw new TypeError('You must provide an array as argument') | ||
throw new TypeError('[VueTypes error]: You must provide an array as argument') | ||
} | ||
const nativeChecks = arr.map((type) => { | ||
let hasCustomValidators = false | ||
const nativeChecks = arr.reduce((ret, type, i) => { | ||
if (isPlainObject(type)) { | ||
if (type.name === 'oneOf') { | ||
return ret.concat(type.type || []) | ||
} | ||
if (type.type && !isFunction(type.validator)) { | ||
return type.type | ||
if (isArray(type.type)) return ret.concat(type.type) | ||
ret.push(type.type) | ||
} else if (isFunction(type.validator)) { | ||
hasCustomValidators = true | ||
} | ||
return null | ||
return ret | ||
} | ||
return type | ||
}).filter((type) => !!type) | ||
ret.push(type) | ||
return ret | ||
}, []) | ||
if (nativeChecks.length === arr.length) { | ||
if (!hasCustomValidators) { | ||
// we got just native objects (ie: Array, Object) | ||
// delegate to Vue native prop check | ||
return toType({ | ||
name: 'oneOfType', | ||
type: nativeChecks | ||
@@ -123,6 +163,18 @@ }) | ||
return this.custom((value) => { | ||
return arr.some((type) => { | ||
return validateType(type, value) | ||
const typesStr = arr.map((type) => { | ||
if (type && isArray(type.type)) { | ||
return type.type.map(getType) | ||
} | ||
return getType(type) | ||
}).reduce((ret, type) => ret.concat(isArray(type) ? type : [type]), []).join('", "') | ||
return this.custom(function oneOfType(value) { | ||
const valid = arr.some((type) => { | ||
if (type.name === 'oneOf') { | ||
return type.type ? validateType(type.type, value, true) : true | ||
} | ||
return validateType(type, value, true) | ||
}) | ||
if (!valid) warn(`value type should be one of "${typesStr}"`) | ||
return valid | ||
}) | ||
@@ -133,5 +185,8 @@ }, | ||
return toType({ | ||
name: 'arrayOf', | ||
type: Array, | ||
validator(values) { | ||
return values.every((value) => validateType(type, value)) | ||
const valid = values.every((value) => validateType(type, value)) | ||
if (!valid) warn(`value must be an array of '${getType(type)}'`) | ||
return valid | ||
} | ||
@@ -143,5 +198,8 @@ }) | ||
return toType({ | ||
name: 'objectOf', | ||
type: Object, | ||
validator(obj) { | ||
return Object.keys(obj).every((key) => validateType(type, obj[key])) | ||
const valid = Object.keys(obj).every((key) => validateType(type, obj[key])) | ||
if (!valid) warn(`value must be an object of '${getType(type)}'`) | ||
return valid | ||
} | ||
@@ -155,3 +213,4 @@ }) | ||
return toType({ | ||
const type = toType({ | ||
name: 'shape', | ||
type: Object, | ||
@@ -166,2 +225,3 @@ validator(value) { | ||
if (requiredKeys.length > 0 && requiredKeys.some((req) => valueKeys.indexOf(req) === -1)) { | ||
warn(`at least one of required properties "${requiredKeys.join('", "')}" is not present`) | ||
return false | ||
@@ -172,2 +232,4 @@ } | ||
if (keys.indexOf(key) === -1) { | ||
if (this.isLoose === true) return true | ||
warn(`object is missing "${key}" property`) | ||
return false | ||
@@ -180,2 +242,14 @@ } | ||
}) | ||
Object.defineProperty(type, 'loose', { | ||
get() { | ||
const t = objectAssign({ isLoose: true }, this) | ||
withRequired(t) | ||
withDefault(t) | ||
return t | ||
}, | ||
enumerable: false | ||
}) | ||
return type | ||
} | ||
@@ -182,0 +256,0 @@ |
@@ -11,7 +11,14 @@ import isPlainObject from 'lodash.isplainobject' | ||
// https://github.com/vuejs/vue/blob/dev/src/core/util/props.js#L159 | ||
const getType = (fn) => { | ||
const match = fn && fn.toString().match(FN_MATCH_REGEXP) | ||
export const getType = (fn) => { | ||
const type = (fn !== null && fn !== undefined) ? (fn.type ? fn.type : fn) : null | ||
const match = type && type.toString().match(FN_MATCH_REGEXP) | ||
return match && match[1] | ||
} | ||
export const getNativeType = (value) => { | ||
if (value === null || value === undefined) return null | ||
const match = value.constructor.toString().match(FN_MATCH_REGEXP) | ||
return match && match[1] | ||
} | ||
/** | ||
@@ -54,3 +61,3 @@ * No-op function | ||
* | ||
* @param {any} val - Value to check | ||
* @param {any} value - Value to check | ||
* @returns {boolean} | ||
@@ -69,7 +76,9 @@ */ | ||
if (!validateType(this, def)) { | ||
console.warn('default value not allowed here', def) // eslint-disable-line no-console | ||
warn('invalid default value', def) | ||
return type | ||
} | ||
const newType = objectAssign({}, this, { | ||
default: (isArray(def) || isPlainObject(def)) ? function () { return def } : def | ||
default: (isArray(def) || isPlainObject(def)) ? function () { | ||
return def | ||
} : def | ||
}) | ||
@@ -119,8 +128,9 @@ if (!hasOwn.call(newType, 'required')) { | ||
* @param {*} value - Value to check | ||
* @param {boolean} silent - Silence warnings | ||
* @returns {boolean} | ||
*/ | ||
export const validateType = (type, value) => { | ||
export const validateType = (type, value, silent = false) => { | ||
let typeToCheck = type | ||
let valid = true | ||
let expectedType | ||
if (!isPlainObject(type)) { | ||
@@ -131,12 +141,17 @@ typeToCheck = { type } | ||
if (hasOwn.call(typeToCheck, 'type') && typeToCheck.type !== null) { | ||
const expectedType = getType(typeToCheck.type) | ||
if (isArray(typeToCheck.type)) { | ||
valid = typeToCheck.type.some((type) => validateType(type, value, true)) | ||
expectedType = typeToCheck.type.map((type) => getType(type)).join(' or ') | ||
} else { | ||
expectedType = getType(typeToCheck) | ||
if (expectedType === 'Array') { | ||
valid = isArray(value) | ||
} else if (expectedType === 'Object') { | ||
valid = isPlainObject(value) | ||
} else if (expectedType === 'String' || expectedType === 'Number' || expectedType === 'Boolean' || expectedType === 'Function') { | ||
valid = typeof value === expectedType.toLowerCase() | ||
} else { | ||
valid = value instanceof typeToCheck.type | ||
if (expectedType === 'Array') { | ||
valid = isArray(value) | ||
} else if (expectedType === 'Object') { | ||
valid = isPlainObject(value) | ||
} else if (expectedType === 'String' || expectedType === 'Number' || expectedType === 'Boolean' || expectedType === 'Function') { | ||
valid = getNativeType(value) === expectedType | ||
} else { | ||
valid = value instanceof typeToCheck.type | ||
} | ||
} | ||
@@ -146,2 +161,3 @@ } | ||
if (!valid) { | ||
silent === false && warn(`value "${value}" should be of type '${expectedType}'`) | ||
return false | ||
@@ -151,5 +167,20 @@ } | ||
if (hasOwn.call(typeToCheck, 'validator') && isFunction(typeToCheck.validator)) { | ||
return typeToCheck.validator(value) | ||
valid = typeToCheck.validator(value) | ||
if (!valid && silent === false) warn('custom validation failed') | ||
return valid | ||
} | ||
return valid | ||
} | ||
let warn = noop | ||
if (process.env.NODE_ENV !== 'production') { | ||
const hasConsole = typeof console !== 'undefined' | ||
warn = (msg) => { | ||
if (hasConsole) { | ||
console.warn(`[VueTypes warn]: ${msg}`) | ||
} | ||
} | ||
} | ||
export { warn } |
/** | ||
* vue-types v0.5.0 | ||
* vue-types v0.5.2 | ||
* Copyright (c) 2016 Marco Solazzi | ||
@@ -230,2 +230,92 @@ * MIT License | ||
/* 1 */ | ||
/***/ function(module, exports) { | ||
"use strict"; | ||
'use strict'; | ||
/* eslint-disable no-unused-vars */ | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
var propIsEnumerable = Object.prototype.propertyIsEnumerable; | ||
function toObject(val) { | ||
if (val === null || val === undefined) { | ||
throw new TypeError('Object.assign cannot be called with null or undefined'); | ||
} | ||
return Object(val); | ||
} | ||
function shouldUseNative() { | ||
try { | ||
if (!Object.assign) { | ||
return false; | ||
} | ||
// Detect buggy property enumeration order in older V8 versions. | ||
// https://bugs.chromium.org/p/v8/issues/detail?id=4118 | ||
var test1 = new String('abc'); // eslint-disable-line | ||
test1[5] = 'de'; | ||
if (Object.getOwnPropertyNames(test1)[0] === '5') { | ||
return false; | ||
} | ||
// https://bugs.chromium.org/p/v8/issues/detail?id=3056 | ||
var test2 = {}; | ||
for (var i = 0; i < 10; i++) { | ||
test2['_' + String.fromCharCode(i)] = i; | ||
} | ||
var order2 = Object.getOwnPropertyNames(test2).map(function (n) { | ||
return test2[n]; | ||
}); | ||
if (order2.join('') !== '0123456789') { | ||
return false; | ||
} | ||
// https://bugs.chromium.org/p/v8/issues/detail?id=3056 | ||
var test3 = {}; | ||
'abcdefghijklmnopqrst'.split('').forEach(function (letter) { | ||
test3[letter] = letter; | ||
}); | ||
if (Object.keys(Object.assign({}, test3)).join('') !== | ||
'abcdefghijklmnopqrst') { | ||
return false; | ||
} | ||
return true; | ||
} catch (e) { | ||
// We don't expect any of the above to throw, but better to be safe. | ||
return false; | ||
} | ||
} | ||
module.exports = shouldUseNative() ? Object.assign : function (target, source) { | ||
var from; | ||
var to = toObject(target); | ||
var symbols; | ||
for (var s = 1; s < arguments.length; s++) { | ||
from = Object(arguments[s]); | ||
for (var key in from) { | ||
if (hasOwnProperty.call(from, key)) { | ||
to[key] = from[key]; | ||
} | ||
} | ||
if (Object.getOwnPropertySymbols) { | ||
symbols = Object.getOwnPropertySymbols(from); | ||
for (var i = 0; i < symbols.length; i++) { | ||
if (propIsEnumerable.call(from, symbols[i])) { | ||
to[symbols[i]] = from[symbols[i]]; | ||
} | ||
} | ||
} | ||
} | ||
return to; | ||
}; | ||
/***/ }, | ||
/* 2 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
@@ -242,4 +332,8 @@ | ||
var _utils = __webpack_require__(2); | ||
var _objectAssign = __webpack_require__(1); | ||
var _objectAssign2 = _interopRequireDefault(_objectAssign); | ||
var _utils = __webpack_require__(3); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -251,3 +345,4 @@ | ||
return (0, _utils.toType)({ | ||
type: null | ||
type: null, | ||
name: 'any' | ||
}); | ||
@@ -259,2 +354,3 @@ }, | ||
type: Function, | ||
name: 'function', | ||
default: _utils.noop | ||
@@ -267,2 +363,3 @@ }); | ||
type: Boolean, | ||
name: 'boolean', | ||
default: true | ||
@@ -275,2 +372,3 @@ }); | ||
type: String, | ||
name: 'string', | ||
default: '' | ||
@@ -283,2 +381,3 @@ }); | ||
type: Number, | ||
name: 'number', | ||
default: 0 | ||
@@ -291,2 +390,3 @@ }); | ||
type: Array, | ||
name: 'array', | ||
default: Array | ||
@@ -299,2 +399,3 @@ }); | ||
type: Object, | ||
name: 'object', | ||
default: Object | ||
@@ -307,2 +408,3 @@ }); | ||
type: Number, | ||
name: 'integer', | ||
validator: function validator(value) { | ||
@@ -316,9 +418,16 @@ return (0, _utils.isInteger)(value); | ||
custom: function custom(validator) { | ||
if (typeof validator !== 'function') { | ||
throw new TypeError('You must provide a function as argument'); | ||
custom: function custom(validatorFn) { | ||
var warnMsg = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'custom validation failed'; | ||
if (typeof validatorFn !== 'function') { | ||
throw new TypeError('[VueTypes error]: You must provide a function as argument'); | ||
} | ||
return (0, _utils.toType)({ | ||
validator: validator | ||
name: validatorFn.name || '<<anonymous function>>', | ||
validator: function validator() { | ||
var valid = validatorFn.apply(undefined, arguments); | ||
if (!valid) (0, _utils.warn)(warnMsg); | ||
return valid; | ||
} | ||
}); | ||
@@ -328,7 +437,20 @@ }, | ||
if (!(0, _utils.isArray)(arr)) { | ||
throw new TypeError('You must provide an array as argument'); | ||
throw new TypeError('[VueTypes error]: You must provide an array as argument'); | ||
} | ||
var msg = 'value should be one of "' + arr.join('", "') + '"'; | ||
var allowedTypes = arr.reduce(function (ret, v) { | ||
if (v !== null && v !== undefined) { | ||
ret.indexOf(v.constructor) === -1 && ret.push(v.constructor); | ||
} | ||
return ret; | ||
}, []); | ||
return this.custom(function (value) { | ||
return arr.indexOf(value) !== -1; | ||
return (0, _utils.toType)({ | ||
name: 'oneOf', | ||
type: allowedTypes.length > 0 ? allowedTypes : null, | ||
validator: function validator(value) { | ||
var valid = arr.indexOf(value) !== -1; | ||
if (!valid) (0, _utils.warn)(msg); | ||
return valid; | ||
} | ||
}); | ||
@@ -338,2 +460,3 @@ }, | ||
return (0, _utils.toType)({ | ||
name: 'instanceOf', | ||
type: instanceConstructor | ||
@@ -344,21 +467,29 @@ }); | ||
if (!(0, _utils.isArray)(arr)) { | ||
throw new TypeError('You must provide an array as argument'); | ||
throw new TypeError('[VueTypes error]: You must provide an array as argument'); | ||
} | ||
var nativeChecks = arr.map(function (type) { | ||
var hasCustomValidators = false; | ||
var nativeChecks = arr.reduce(function (ret, type, i) { | ||
if ((0, _lodash2.default)(type)) { | ||
if (type.name === 'oneOf') { | ||
return ret.concat(type.type || []); | ||
} | ||
if (type.type && !(0, _utils.isFunction)(type.validator)) { | ||
return type.type; | ||
if ((0, _utils.isArray)(type.type)) return ret.concat(type.type); | ||
ret.push(type.type); | ||
} else if ((0, _utils.isFunction)(type.validator)) { | ||
hasCustomValidators = true; | ||
} | ||
return null; | ||
return ret; | ||
} | ||
return type; | ||
}).filter(function (type) { | ||
return !!type; | ||
}); | ||
ret.push(type); | ||
return ret; | ||
}, []); | ||
if (nativeChecks.length === arr.length) { | ||
if (!hasCustomValidators) { | ||
// we got just native objects (ie: Array, Object) | ||
// delegate to Vue native prop check | ||
return (0, _utils.toType)({ | ||
name: 'oneOfType', | ||
type: nativeChecks | ||
@@ -368,6 +499,20 @@ }); | ||
return this.custom(function (value) { | ||
return arr.some(function (type) { | ||
return (0, _utils.validateType)(type, value); | ||
var typesStr = arr.map(function (type) { | ||
if (type && (0, _utils.isArray)(type.type)) { | ||
return type.type.map(_utils.getType); | ||
} | ||
return (0, _utils.getType)(type); | ||
}).reduce(function (ret, type) { | ||
return ret.concat((0, _utils.isArray)(type) ? type : [type]); | ||
}, []).join('", "'); | ||
return this.custom(function oneOfType(value) { | ||
var valid = arr.some(function (type) { | ||
if (type.name === 'oneOf') { | ||
return type.type ? (0, _utils.validateType)(type.type, value, true) : true; | ||
} | ||
return (0, _utils.validateType)(type, value, true); | ||
}); | ||
if (!valid) (0, _utils.warn)('value type should be one of "' + typesStr + '"'); | ||
return valid; | ||
}); | ||
@@ -377,7 +522,10 @@ }, | ||
return (0, _utils.toType)({ | ||
name: 'arrayOf', | ||
type: Array, | ||
validator: function validator(values) { | ||
return values.every(function (value) { | ||
var valid = values.every(function (value) { | ||
return (0, _utils.validateType)(type, value); | ||
}); | ||
if (!valid) (0, _utils.warn)('value must be an array of \'' + (0, _utils.getType)(type) + '\''); | ||
return valid; | ||
} | ||
@@ -388,7 +536,10 @@ }); | ||
return (0, _utils.toType)({ | ||
name: 'objectOf', | ||
type: Object, | ||
validator: function validator(obj) { | ||
return Object.keys(obj).every(function (key) { | ||
var valid = Object.keys(obj).every(function (key) { | ||
return (0, _utils.validateType)(type, obj[key]); | ||
}); | ||
if (!valid) (0, _utils.warn)('value must be an object of \'' + (0, _utils.getType)(type) + '\''); | ||
return valid; | ||
} | ||
@@ -403,5 +554,8 @@ }); | ||
return (0, _utils.toType)({ | ||
var type = (0, _utils.toType)({ | ||
name: 'shape', | ||
type: Object, | ||
validator: function validator(value) { | ||
var _this = this; | ||
if (!(0, _lodash2.default)(value)) { | ||
@@ -416,2 +570,3 @@ return false; | ||
})) { | ||
(0, _utils.warn)('at least one of required properties "' + requiredKeys.join('", "') + '" is not present'); | ||
return false; | ||
@@ -422,2 +577,4 @@ } | ||
if (keys.indexOf(key) === -1) { | ||
if (_this.isLoose === true) return true; | ||
(0, _utils.warn)('object is missing "' + key + '" property'); | ||
return false; | ||
@@ -430,2 +587,15 @@ } | ||
}); | ||
Object.defineProperty(type, 'loose', { | ||
get: function get() { | ||
var t = (0, _objectAssign2.default)({ isLoose: true }, this); | ||
(0, _utils.withRequired)(t); | ||
(0, _utils.withDefault)(t); | ||
return t; | ||
}, | ||
enumerable: false | ||
}); | ||
return type; | ||
} | ||
@@ -438,3 +608,3 @@ }; | ||
/***/ }, | ||
/* 2 */ | ||
/* 3 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
@@ -446,6 +616,4 @@ | ||
exports.__esModule = true; | ||
exports.validateType = exports.toType = exports.withRequired = exports.withDefault = exports.isFunction = exports.isArray = exports.isInteger = exports.has = exports.noop = exports.hasOwn = undefined; | ||
exports.warn = exports.validateType = exports.toType = exports.withRequired = exports.withDefault = exports.isFunction = exports.isArray = exports.isInteger = exports.has = exports.noop = exports.getNativeType = exports.getType = exports.hasOwn = undefined; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _lodash = __webpack_require__(0); | ||
@@ -455,3 +623,3 @@ | ||
var _objectAssign = __webpack_require__(3); | ||
var _objectAssign = __webpack_require__(1); | ||
@@ -469,7 +637,14 @@ var _objectAssign2 = _interopRequireDefault(_objectAssign); | ||
// https://github.com/vuejs/vue/blob/dev/src/core/util/props.js#L159 | ||
var getType = function getType(fn) { | ||
var match = fn && fn.toString().match(FN_MATCH_REGEXP); | ||
var getType = exports.getType = function getType(fn) { | ||
var type = fn !== null && fn !== undefined ? fn.type ? fn.type : fn : null; | ||
var match = type && type.toString().match(FN_MATCH_REGEXP); | ||
return match && match[1]; | ||
}; | ||
var getNativeType = exports.getNativeType = function getNativeType(value) { | ||
if (value === null || value === undefined) return null; | ||
var match = value.constructor.toString().match(FN_MATCH_REGEXP); | ||
return match && match[1]; | ||
}; | ||
/** | ||
@@ -514,3 +689,3 @@ * No-op function | ||
* | ||
* @param {any} val - Value to check | ||
* @param {any} value - Value to check | ||
* @returns {boolean} | ||
@@ -531,3 +706,3 @@ */ | ||
if (!validateType(this, def)) { | ||
console.warn('default value not allowed here', def); // eslint-disable-line no-console | ||
warn('invalid default value', def); | ||
return type; | ||
@@ -585,8 +760,11 @@ } | ||
* @param {*} value - Value to check | ||
* @param {boolean} silent - Silence warnings | ||
* @returns {boolean} | ||
*/ | ||
var validateType = exports.validateType = function validateType(type, value) { | ||
var silent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
var typeToCheck = type; | ||
var valid = true; | ||
var expectedType = void 0; | ||
if (!(0, _lodash2.default)(type)) { | ||
@@ -597,12 +775,21 @@ typeToCheck = { type: type }; | ||
if (hasOwn.call(typeToCheck, 'type') && typeToCheck.type !== null) { | ||
var expectedType = getType(typeToCheck.type); | ||
if (isArray(typeToCheck.type)) { | ||
valid = typeToCheck.type.some(function (type) { | ||
return validateType(type, value, true); | ||
}); | ||
expectedType = typeToCheck.type.map(function (type) { | ||
return getType(type); | ||
}).join(' or '); | ||
} else { | ||
expectedType = getType(typeToCheck); | ||
if (expectedType === 'Array') { | ||
valid = isArray(value); | ||
} else if (expectedType === 'Object') { | ||
valid = (0, _lodash2.default)(value); | ||
} else if (expectedType === 'String' || expectedType === 'Number' || expectedType === 'Boolean' || expectedType === 'Function') { | ||
valid = (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === expectedType.toLowerCase(); | ||
} else { | ||
valid = value instanceof typeToCheck.type; | ||
if (expectedType === 'Array') { | ||
valid = isArray(value); | ||
} else if (expectedType === 'Object') { | ||
valid = (0, _lodash2.default)(value); | ||
} else if (expectedType === 'String' || expectedType === 'Number' || expectedType === 'Boolean' || expectedType === 'Function') { | ||
valid = getNativeType(value) === expectedType; | ||
} else { | ||
valid = value instanceof typeToCheck.type; | ||
} | ||
} | ||
@@ -612,2 +799,3 @@ } | ||
if (!valid) { | ||
silent === false && warn('value "' + value + '" should be of type \'' + expectedType + '\''); | ||
return false; | ||
@@ -617,3 +805,5 @@ } | ||
if (hasOwn.call(typeToCheck, 'validator') && isFunction(typeToCheck.validator)) { | ||
return typeToCheck.validator(value); | ||
valid = typeToCheck.validator(value); | ||
if (!valid && silent === false) warn('custom validation failed'); | ||
return valid; | ||
} | ||
@@ -623,92 +813,17 @@ return valid; | ||
/***/ }, | ||
/* 3 */ | ||
/***/ function(module, exports) { | ||
var warn = noop; | ||
"use strict"; | ||
'use strict'; | ||
/* eslint-disable no-unused-vars */ | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
var propIsEnumerable = Object.prototype.propertyIsEnumerable; | ||
function toObject(val) { | ||
if (val === null || val === undefined) { | ||
throw new TypeError('Object.assign cannot be called with null or undefined'); | ||
} | ||
return Object(val); | ||
if (true) { | ||
(function () { | ||
var hasConsole = typeof console !== 'undefined'; | ||
exports.warn = warn = function warn(msg) { | ||
if (hasConsole) { | ||
console.warn('[VueTypes warn]: ' + msg); | ||
} | ||
}; | ||
})(); | ||
} | ||
function shouldUseNative() { | ||
try { | ||
if (!Object.assign) { | ||
return false; | ||
} | ||
exports.warn = warn; | ||
// Detect buggy property enumeration order in older V8 versions. | ||
// https://bugs.chromium.org/p/v8/issues/detail?id=4118 | ||
var test1 = new String('abc'); // eslint-disable-line | ||
test1[5] = 'de'; | ||
if (Object.getOwnPropertyNames(test1)[0] === '5') { | ||
return false; | ||
} | ||
// https://bugs.chromium.org/p/v8/issues/detail?id=3056 | ||
var test2 = {}; | ||
for (var i = 0; i < 10; i++) { | ||
test2['_' + String.fromCharCode(i)] = i; | ||
} | ||
var order2 = Object.getOwnPropertyNames(test2).map(function (n) { | ||
return test2[n]; | ||
}); | ||
if (order2.join('') !== '0123456789') { | ||
return false; | ||
} | ||
// https://bugs.chromium.org/p/v8/issues/detail?id=3056 | ||
var test3 = {}; | ||
'abcdefghijklmnopqrst'.split('').forEach(function (letter) { | ||
test3[letter] = letter; | ||
}); | ||
if (Object.keys(Object.assign({}, test3)).join('') !== | ||
'abcdefghijklmnopqrst') { | ||
return false; | ||
} | ||
return true; | ||
} catch (e) { | ||
// We don't expect any of the above to throw, but better to be safe. | ||
return false; | ||
} | ||
} | ||
module.exports = shouldUseNative() ? Object.assign : function (target, source) { | ||
var from; | ||
var to = toObject(target); | ||
var symbols; | ||
for (var s = 1; s < arguments.length; s++) { | ||
from = Object(arguments[s]); | ||
for (var key in from) { | ||
if (hasOwnProperty.call(from, key)) { | ||
to[key] = from[key]; | ||
} | ||
} | ||
if (Object.getOwnPropertySymbols) { | ||
symbols = Object.getOwnPropertySymbols(from); | ||
for (var i = 0; i < symbols.length; i++) { | ||
if (propIsEnumerable.call(from, symbols[i])) { | ||
to[symbols[i]] = from[symbols[i]]; | ||
} | ||
} | ||
} | ||
} | ||
return to; | ||
}; | ||
/***/ }, | ||
@@ -718,3 +833,3 @@ /* 4 */ | ||
module.exports = __webpack_require__(1); | ||
module.exports = __webpack_require__(2); | ||
@@ -721,0 +836,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.VueTypes=e():t.VueTypes=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var n={};return e.m=t,e.c=n,e.i=function(t){return t},e.d=function(t,e,n){Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:n})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=4)}([function(t,e){function n(t){var e=!1;if(null!=t&&"function"!=typeof t.toString)try{e=!!(t+"")}catch(t){}return e}function r(t,e){return function(n){return t(e(n))}}function o(t){return!!t&&"object"==typeof t}function u(t){if(!o(t)||y.call(t)!=i||n(t))return!1;var e=s(t);if(null===e)return!0;var r=l.call(e,"constructor")&&e.constructor;return"function"==typeof r&&r instanceof r&&f.call(r)==p}var i="[object Object]",c=Function.prototype,a=Object.prototype,f=c.toString,l=a.hasOwnProperty,p=f.call(Object),y=a.toString,s=r(Object.getPrototypeOf,Object);t.exports=u},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}e.__esModule=!0;var o=n(0),u=r(o),i=n(2),c={get any(){return(0,i.toType)({type:null})},get func(){return(0,i.toType)({type:Function,default:i.noop})},get bool(){return(0,i.toType)({type:Boolean,default:!0})},get string(){return(0,i.toType)({type:String,default:""})},get number(){return(0,i.toType)({type:Number,default:0})},get array(){return(0,i.toType)({type:Array,default:Array})},get object(){return(0,i.toType)({type:Object,default:Object})},get integer(){return(0,i.toType)({type:Number,validator:function(t){return(0,i.isInteger)(t)},default:0})},custom:function(t){if("function"!=typeof t)throw new TypeError("You must provide a function as argument");return(0,i.toType)({validator:t})},oneOf:function(t){if(!(0,i.isArray)(t))throw new TypeError("You must provide an array as argument");return this.custom(function(e){return t.indexOf(e)!==-1})},instanceOf:function(t){return(0,i.toType)({type:t})},oneOfType:function(t){if(!(0,i.isArray)(t))throw new TypeError("You must provide an array as argument");var e=t.map(function(t){return(0,u.default)(t)?t.type&&!(0,i.isFunction)(t.validator)?t.type:null:t}).filter(function(t){return!!t});return e.length===t.length?(0,i.toType)({type:e}):this.custom(function(e){return t.some(function(t){return(0,i.validateType)(t,e)})})},arrayOf:function(t){return(0,i.toType)({type:Array,validator:function(e){return e.every(function(e){return(0,i.validateType)(t,e)})}})},objectOf:function(t){return(0,i.toType)({type:Object,validator:function(e){return Object.keys(e).every(function(n){return(0,i.validateType)(t,e[n])})}})},shape:function(t){var e=Object.keys(t),n=e.filter(function(e){return t[e]&&t[e].required===!0});return(0,i.toType)({type:Object,validator:function(r){if(!(0,u.default)(r))return!1;var o=Object.keys(r);return!(n.length>0&&n.some(function(t){return o.indexOf(t)===-1}))&&o.every(function(n){if(e.indexOf(n)===-1)return!1;var o=t[n];return(0,i.validateType)(o,r[n])})}})}};e.default=c,t.exports=e.default},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}e.__esModule=!0,e.validateType=e.toType=e.withRequired=e.withDefault=e.isFunction=e.isArray=e.isInteger=e.has=e.noop=e.hasOwn=void 0;var o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},u=n(0),i=r(u),c=n(3),a=r(c),f=Object.prototype,l=f.toString,p=e.hasOwn=f.hasOwnProperty,y=/^\s*function (\w+)/,s=function(t){var e=t&&t.toString().match(y);return e&&e[1]},b=(e.noop=function(){},e.has=function(t,e){return p.call(t,e)},e.isInteger=Number.isInteger||function(t){return"number"==typeof t&&isFinite(t)&&Math.floor(t)===t},e.isArray=Array.isArray||function(t){return"[object Array]"===l.call(t)}),d=e.isFunction=function(t){return"[object Function]"===l.call(t)},j=e.withDefault=function(t){Object.defineProperty(t,"def",{value:function(e){if(!v(this,e))return console.warn("default value not allowed here",e),t;var n=(0,a.default)({},this,{default:b(e)||(0,i.default)(e)?function(){return e}:e});return p.call(n,"required")||O(n),n},enumerable:!1,writable:!1})},O=e.withRequired=function(t){Object.defineProperty(t,"isRequired",{get:function(){var t=(0,a.default)({required:!0},this);return j(t),t},enumerable:!1})},v=(e.toType=function(t){return O(t),j(t),t},e.validateType=function(t,e){var n=t,r=!0;if((0,i.default)(t)||(n={type:t}),p.call(n,"type")&&null!==n.type){var u=s(n.type);r="Array"===u?b(e):"Object"===u?(0,i.default)(e):"String"===u||"Number"===u||"Boolean"===u||"Function"===u?("undefined"==typeof e?"undefined":o(e))===u.toLowerCase():e instanceof n.type}return!!r&&(p.call(n,"validator")&&d(n.validator)?n.validator(e):r)})},function(t,e){"use strict";function n(t){if(null===t||void 0===t)throw new TypeError("Object.assign cannot be called with null or undefined");return Object(t)}function r(){try{if(!Object.assign)return!1;var t=new String("abc");if(t[5]="de","5"===Object.getOwnPropertyNames(t)[0])return!1;for(var e={},n=0;n<10;n++)e["_"+String.fromCharCode(n)]=n;var r=Object.getOwnPropertyNames(e).map(function(t){return e[t]});if("0123456789"!==r.join(""))return!1;var o={};return"abcdefghijklmnopqrst".split("").forEach(function(t){o[t]=t}),"abcdefghijklmnopqrst"===Object.keys(Object.assign({},o)).join("")}catch(t){return!1}}var o=Object.prototype.hasOwnProperty,u=Object.prototype.propertyIsEnumerable;t.exports=r()?Object.assign:function(t,e){for(var r,i,c=n(t),a=1;a<arguments.length;a++){r=Object(arguments[a]);for(var f in r)o.call(r,f)&&(c[f]=r[f]);if(Object.getOwnPropertySymbols){i=Object.getOwnPropertySymbols(r);for(var l=0;l<i.length;l++)u.call(r,i[l])&&(c[i[l]]=r[i[l]])}}return c}},function(t,e,n){t.exports=n(1)}])}); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.VueTypes=t():e.VueTypes=t()}(this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};return t.m=e,t.c=n,t.i=function(e){return e},t.d=function(e,t,n){Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:n})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=4)}([function(e,t){function n(e){var t=!1;if(null!=e&&"function"!=typeof e.toString)try{t=!!(e+"")}catch(e){}return t}function r(e,t){return function(n){return e(t(n))}}function o(e){return!!e&&"object"==typeof e}function u(e){if(!o(e)||y.call(e)!=i||n(e))return!1;var t=s(e);if(null===t)return!0;var r=p.call(t,"constructor")&&t.constructor;return"function"==typeof r&&r instanceof r&&f.call(r)==l}var i="[object Object]",a=Function.prototype,c=Object.prototype,f=a.toString,p=c.hasOwnProperty,l=f.call(Object),y=c.toString,s=r(Object.getPrototypeOf,Object);e.exports=u},function(e,t){"use strict";function n(e){if(null===e||void 0===e)throw new TypeError("Object.assign cannot be called with null or undefined");return Object(e)}function r(){try{if(!Object.assign)return!1;var e=new String("abc");if(e[5]="de","5"===Object.getOwnPropertyNames(e)[0])return!1;for(var t={},n=0;n<10;n++)t["_"+String.fromCharCode(n)]=n;var r=Object.getOwnPropertyNames(t).map(function(e){return t[e]});if("0123456789"!==r.join(""))return!1;var o={};return"abcdefghijklmnopqrst".split("").forEach(function(e){o[e]=e}),"abcdefghijklmnopqrst"===Object.keys(Object.assign({},o)).join("")}catch(e){return!1}}var o=Object.prototype.hasOwnProperty,u=Object.prototype.propertyIsEnumerable;e.exports=r()?Object.assign:function(e,t){for(var r,i,a=n(e),c=1;c<arguments.length;c++){r=Object(arguments[c]);for(var f in r)o.call(r,f)&&(a[f]=r[f]);if(Object.getOwnPropertySymbols){i=Object.getOwnPropertySymbols(r);for(var p=0;p<i.length;p++)u.call(r,i[p])&&(a[i[p]]=r[i[p]])}}return a}},function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}t.__esModule=!0;var o=n(0),u=r(o),i=n(1),a=r(i),c=n(3),f={get any(){return(0,c.toType)({type:null,name:"any"})},get func(){return(0,c.toType)({type:Function,name:"function",default:c.noop})},get bool(){return(0,c.toType)({type:Boolean,name:"boolean",default:!0})},get string(){return(0,c.toType)({type:String,name:"string",default:""})},get number(){return(0,c.toType)({type:Number,name:"number",default:0})},get array(){return(0,c.toType)({type:Array,name:"array",default:Array})},get object(){return(0,c.toType)({type:Object,name:"object",default:Object})},get integer(){return(0,c.toType)({type:Number,name:"integer",validator:function(e){return(0,c.isInteger)(e)},default:0})},custom:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"custom validation failed";if("function"!=typeof e)throw new TypeError("[VueTypes error]: You must provide a function as argument");return(0,c.toType)({name:e.name||"<<anonymous function>>",validator:function(){var n=e.apply(void 0,arguments);return n||(0,c.warn)(t),n}})},oneOf:function(e){if(!(0,c.isArray)(e))throw new TypeError("[VueTypes error]: You must provide an array as argument");var t='value should be one of "'+e.join('", "')+'"',n=e.reduce(function(e,t){return null!==t&&void 0!==t&&e.indexOf(t.constructor)===-1&&e.push(t.constructor),e},[]);return(0,c.toType)({name:"oneOf",type:n.length>0?n:null,validator:function(n){var r=e.indexOf(n)!==-1;return r||(0,c.warn)(t),r}})},instanceOf:function(e){return(0,c.toType)({name:"instanceOf",type:e})},oneOfType:function(e){if(!(0,c.isArray)(e))throw new TypeError("[VueTypes error]: You must provide an array as argument");var t=!1,n=e.reduce(function(e,n,r){if((0,u.default)(n)){if("oneOf"===n.name)return e.concat(n.type||[]);if(n.type&&!(0,c.isFunction)(n.validator)){if((0,c.isArray)(n.type))return e.concat(n.type);e.push(n.type)}else(0,c.isFunction)(n.validator)&&(t=!0);return e}return e.push(n),e},[]);if(!t)return(0,c.toType)({name:"oneOfType",type:n});var r=e.map(function(e){return e&&(0,c.isArray)(e.type)?e.type.map(c.getType):(0,c.getType)(e)}).reduce(function(e,t){return e.concat((0,c.isArray)(t)?t:[t])},[]).join('", "');return this.custom(function(t){var n=e.some(function(e){return"oneOf"===e.name?!e.type||(0,c.validateType)(e.type,t,!0):(0,c.validateType)(e,t,!0)});return n||(0,c.warn)('value type should be one of "'+r+'"'),n})},arrayOf:function(e){return(0,c.toType)({name:"arrayOf",type:Array,validator:function(t){var n=t.every(function(t){return(0,c.validateType)(e,t)});return n||(0,c.warn)("value must be an array of '"+(0,c.getType)(e)+"'"),n}})},objectOf:function(e){return(0,c.toType)({name:"objectOf",type:Object,validator:function(t){var n=Object.keys(t).every(function(n){return(0,c.validateType)(e,t[n])});return n||(0,c.warn)("value must be an object of '"+(0,c.getType)(e)+"'"),n}})},shape:function(e){var t=Object.keys(e),n=t.filter(function(t){return e[t]&&e[t].required===!0}),r=(0,c.toType)({name:"shape",type:Object,validator:function(r){var o=this;if(!(0,u.default)(r))return!1;var i=Object.keys(r);return n.length>0&&n.some(function(e){return i.indexOf(e)===-1})?((0,c.warn)('at least one of required properties "'+n.join('", "')+'" is not present'),!1):i.every(function(n){if(t.indexOf(n)===-1)return o.isLoose===!0||((0,c.warn)('object is missing "'+n+'" property'),!1);var u=e[n];return(0,c.validateType)(u,r[n])})}});return Object.defineProperty(r,"loose",{get:function(){var e=(0,a.default)({isLoose:!0},this);return(0,c.withRequired)(e),(0,c.withDefault)(e),e},enumerable:!1}),r}};t.default=f,e.exports=t.default},function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}t.__esModule=!0,t.warn=t.validateType=t.toType=t.withRequired=t.withDefault=t.isFunction=t.isArray=t.isInteger=t.has=t.noop=t.getNativeType=t.getType=t.hasOwn=void 0;var o=n(0),u=r(o),i=n(1),a=r(i),c=Object.prototype,f=c.toString,p=t.hasOwn=c.hasOwnProperty,l=/^\s*function (\w+)/,y=t.getType=function(e){var t=null!==e&&void 0!==e?e.type?e.type:e:null,n=t&&t.toString().match(l);return n&&n[1]},s=t.getNativeType=function(e){if(null===e||void 0===e)return null;var t=e.constructor.toString().match(l);return t&&t[1]},d=t.noop=function(){},b=(t.has=function(e,t){return p.call(e,t)},t.isInteger=Number.isInteger||function(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e},t.isArray=Array.isArray||function(e){return"[object Array]"===f.call(e)}),v=t.isFunction=function(e){return"[object Function]"===f.call(e)},j=t.withDefault=function(e){Object.defineProperty(e,"def",{value:function(t){if(!m(this,t))return g("invalid default value",t),e;var n=(0,a.default)({},this,{default:b(t)||(0,u.default)(t)?function(){return t}:t});return p.call(n,"required")||O(n),n},enumerable:!1,writable:!1})},O=t.withRequired=function(e){Object.defineProperty(e,"isRequired",{get:function(){var e=(0,a.default)({required:!0},this);return j(e),e},enumerable:!1})},m=(t.toType=function(e){return O(e),j(e),e},t.validateType=function e(t,n){var r=arguments.length>2&&void 0!==arguments[2]&&arguments[2],o=t,i=!0,a=void 0;return(0,u.default)(t)||(o={type:t}),p.call(o,"type")&&null!==o.type&&(b(o.type)?(i=o.type.some(function(t){return e(t,n,!0)}),a=o.type.map(function(e){return y(e)}).join(" or ")):(a=y(o),i="Array"===a?b(n):"Object"===a?(0,u.default)(n):"String"===a||"Number"===a||"Boolean"===a||"Function"===a?s(n)===a:n instanceof o.type)),i?p.call(o,"validator")&&v(o.validator)?(i=o.validator(n),i||r!==!1||g("custom validation failed"),i):i:(r===!1&&g('value "'+n+"\" should be of type '"+a+"'"),!1)}),g=d;t.warn=g},function(e,t,n){e.exports=n(2)}])}); |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
74615
1850
284
3