Comparing version 1.6.0 to 1.7.0-beta.1
@@ -88,4 +88,33 @@ "use strict"; | ||
getter = _props$getter === void 0 ? false : _props$getter, | ||
type = _objectWithoutPropertiesLoose(_props, ["name", "validate", "getter"]); | ||
opts = _objectWithoutPropertiesLoose(_props, ["name", "validate", "getter"]); | ||
if ((0, _utils.has)(VueTypes, name)) { | ||
throw new TypeError("[VueTypes error]: Type \"" + name + "\" already defined"); | ||
} | ||
var type = opts.type, | ||
_opts$validator = opts.validator, | ||
validator = _opts$validator === void 0 ? _utils.stubTrue : _opts$validator; | ||
if (type && type._vueTypes_name) { | ||
// we are using as base type a vue-type object | ||
opts.type = type.type; // inherit the base types | ||
opts.required = type.required; // inherit the required flag | ||
opts.default = type.default; // inherit the default flag | ||
validate = false; // we don't allow validate method on this kind of types | ||
if ((0, _utils.isFunction)(type.validator)) { | ||
opts.validator = function () { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return type.validator.apply(type, args) && validator.apply(this, args); | ||
}; | ||
} | ||
} | ||
var descriptor; | ||
@@ -96,3 +125,3 @@ | ||
get: function get() { | ||
return (0, _utils.toType)(name, Object.assign({}, type), validate); | ||
return (0, _utils.toType)(name, Object.assign({}, opts), validate); | ||
}, | ||
@@ -103,13 +132,13 @@ enumerable: true, | ||
} else { | ||
var validator = type.validator; | ||
var _validator = opts.validator; | ||
descriptor = { | ||
value: function value() { | ||
var ret = (0, _utils.toType)(name, Object.assign({}, type), validate); | ||
var ret = (0, _utils.toType)(name, Object.assign({}, opts), validate); | ||
if (validator) { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
if (_validator) { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
ret.validator = validator.bind.apply(validator, [ret].concat(args)); | ||
ret.validator = _validator.bind.apply(_validator, [ret].concat(args)); | ||
} | ||
@@ -116,0 +145,0 @@ |
@@ -12,3 +12,3 @@ "use strict"; | ||
exports.validateType = validateType; | ||
exports.warn = exports.isFunction = exports.isArray = exports.isInteger = exports.has = exports.hasOwn = void 0; | ||
exports.warn = exports.isFunction = exports.isArray = exports.isInteger = exports.has = exports.stubTrue = exports.hasOwn = void 0; | ||
@@ -45,2 +45,10 @@ var _isPlainObject = _interopRequireDefault(require("is-plain-object")); | ||
/** | ||
* A function that always returns true | ||
*/ | ||
var stubTrue = function stubTrue() { | ||
return true; | ||
}; | ||
/** | ||
* Checks for a own property in an object | ||
@@ -54,2 +62,4 @@ * | ||
exports.stubTrue = stubTrue; | ||
var has = function has(obj, prop) { | ||
@@ -195,2 +205,10 @@ return hasOwn.call(obj, prop); | ||
withValidate(obj); | ||
} else { | ||
Object.defineProperty(obj, 'validate', { | ||
value: function value() { | ||
warn(name + " - \"validate\" method not supported on this type"); | ||
return this; | ||
}, | ||
enumerable: false | ||
}); | ||
} | ||
@@ -197,0 +215,0 @@ |
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | ||
import isPlainObject from 'is-plain-object'; | ||
import { toType, getType, isFunction, validateType, isInteger, isArray, warn } from './utils'; | ||
import { toType, getType, isFunction, validateType, isInteger, isArray, warn, has, stubTrue } from './utils'; | ||
import { setDefaults } from './sensibles'; | ||
@@ -78,4 +78,33 @@ var VueTypes = { | ||
getter = _props$getter === void 0 ? false : _props$getter, | ||
type = _objectWithoutPropertiesLoose(_props, ["name", "validate", "getter"]); | ||
opts = _objectWithoutPropertiesLoose(_props, ["name", "validate", "getter"]); | ||
if (has(VueTypes, name)) { | ||
throw new TypeError("[VueTypes error]: Type \"" + name + "\" already defined"); | ||
} | ||
var type = opts.type, | ||
_opts$validator = opts.validator, | ||
validator = _opts$validator === void 0 ? stubTrue : _opts$validator; | ||
if (type && type._vueTypes_name) { | ||
// we are using as base type a vue-type object | ||
opts.type = type.type; // inherit the base types | ||
opts.required = type.required; // inherit the required flag | ||
opts.default = type.default; // inherit the default flag | ||
validate = false; // we don't allow validate method on this kind of types | ||
if (isFunction(type.validator)) { | ||
opts.validator = function () { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return type.validator.apply(type, args) && validator.apply(this, args); | ||
}; | ||
} | ||
} | ||
var descriptor; | ||
@@ -86,3 +115,3 @@ | ||
get: function get() { | ||
return toType(name, Object.assign({}, type), validate); | ||
return toType(name, Object.assign({}, opts), validate); | ||
}, | ||
@@ -93,13 +122,13 @@ enumerable: true, | ||
} else { | ||
var validator = type.validator; | ||
var _validator = opts.validator; | ||
descriptor = { | ||
value: function value() { | ||
var ret = toType(name, Object.assign({}, type), validate); | ||
var ret = toType(name, Object.assign({}, opts), validate); | ||
if (validator) { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
if (_validator) { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
ret.validator = validator.bind.apply(validator, [ret].concat(args)); | ||
ret.validator = _validator.bind.apply(_validator, [ret].concat(args)); | ||
} | ||
@@ -106,0 +135,0 @@ |
@@ -24,2 +24,9 @@ import isPlainObject from 'is-plain-object'; | ||
/** | ||
* A function that always returns true | ||
*/ | ||
export var stubTrue = function stubTrue() { | ||
return true; | ||
}; | ||
/** | ||
* Checks for a own property in an object | ||
@@ -157,2 +164,10 @@ * | ||
withValidate(obj); | ||
} else { | ||
Object.defineProperty(obj, 'validate', { | ||
value: function value() { | ||
warn(name + " - \"validate\" method not supported on this type"); | ||
return this; | ||
}, | ||
enumerable: false | ||
}); | ||
} | ||
@@ -159,0 +174,0 @@ |
{ | ||
"name": "vue-types", | ||
"version": "1.6.0", | ||
"version": "1.7.0-beta.1", | ||
"description": "Prop types utility for Vue", | ||
@@ -5,0 +5,0 @@ "author": "Marco Solazzi", |
218
README.md
@@ -5,20 +5,19 @@ # vue-types | ||
<!-- TOC depthTo:3 --> | ||
- [Introduction](#introduction) | ||
- [When to use](#when-to-use) | ||
- [When to use](#when-to-use) | ||
- [Installation](#installation) | ||
- [NPM package](#npm-package) | ||
- [CDN delivered `<script>`](#cdn-delivered-script) | ||
- [NPM package](#npm-package) | ||
- [CDN delivered `<script>`](#cdn-delivered-script) | ||
- [Usage with `eslint-plugin-vue`](#usage-with-eslint-plugin-vue) | ||
- [Production build](#production-build) | ||
- [Webpack](#webpack) | ||
- [Rollup](#rollup) | ||
- [Webpack](#webpack) | ||
- [Rollup](#rollup) | ||
- [Documentation](#documentation) | ||
- [Native Types](#native-types) | ||
- [Native Types Configuration](#native-types-configuration) | ||
- [Custom Types](#custom-types) | ||
- [Extending VueTypes](#extending-vuetypes) | ||
- [Utilities](#utilities) | ||
- [Native Types](#native-types) | ||
- [Native Types Configuration](#native-types-configuration) | ||
- [Custom Types](#custom-types) | ||
- [Extending VueTypes](#extending-vuetypes) | ||
- [Utilities](#utilities) | ||
- [License](#license) | ||
@@ -46,7 +45,7 @@ | ||
type: Number, | ||
default: 10 | ||
default: 10, | ||
}, | ||
name: { | ||
type: String, | ||
required: true | ||
required: true, | ||
}, | ||
@@ -57,10 +56,10 @@ age: { | ||
return Number.isInteger(value) | ||
} | ||
}, | ||
}, | ||
nationality: String | ||
nationality: String, | ||
}, | ||
methods: { | ||
// ... | ||
} | ||
}; | ||
}, | ||
} | ||
``` | ||
@@ -71,5 +70,4 @@ | ||
```js | ||
import VueTypes from 'vue-types' | ||
import VueTypes from 'vue-types'; | ||
export default { | ||
@@ -81,11 +79,10 @@ props: { | ||
// No need for `default` or `required` key, so keep it simple | ||
nationality: String | ||
nationality: String, | ||
}, | ||
methods: { | ||
// ... | ||
} | ||
}, | ||
} | ||
``` | ||
## Installation | ||
@@ -95,3 +92,3 @@ | ||
``` bash | ||
```bash | ||
npm install vue-types --save | ||
@@ -105,2 +102,3 @@ # or | ||
add the following script tags before your code | ||
```html | ||
@@ -144,5 +142,8 @@ <script src="https://unpkg.com/vue-types"></script> | ||
alias: { | ||
'vue-types': process.env.NODE_ENV === 'production' ? 'vue-types/es/shim.js' : undefined | ||
} | ||
} | ||
'vue-types': | ||
process.env.NODE_ENV === 'production' | ||
? 'vue-types/es/shim.js' | ||
: undefined, | ||
}, | ||
}, | ||
} | ||
@@ -177,6 +178,6 @@ ``` | ||
* a default value (not available in `.any` and `.symbol`). | ||
* a `.def(any)` method to reassign the default value for the current prop. The passed-in value will be validated against the type configuration in order to prevent invalid values. | ||
* a `isRequired` flag to set the `required: true` key. | ||
* a `validate(function)` method to set a custom validator function (not available in `.integer`). | ||
- a default value (not available in `.any` and `.symbol`). | ||
- a `.def(any)` method to reassign the default value for the current prop. The passed-in value will be validated against the type configuration in order to prevent invalid values. | ||
- a `isRequired` flag to set the `required: true` key. | ||
- a `validate(function)` method to set a custom validator function (not available in `.integer`). | ||
@@ -209,3 +210,3 @@ ```js | ||
* default: an empty array | ||
- default: an empty array | ||
@@ -218,3 +219,3 @@ _Note: [Vue prop validation](https://vuejs.org/v2/guide/components-props.html#Prop-Validation) requires Array definitions to provide default value as a factory function. `VueTypes.array.def()` accepts both factory functions and arrays. In the latter case, VueTypes will convert the value to a factory function for you._ | ||
* default: `true` | ||
- default: `true` | ||
@@ -225,3 +226,3 @@ #### `VueTypes.func` | ||
* default: an empty function | ||
- default: an empty function | ||
@@ -232,3 +233,3 @@ #### `VueTypes.number` | ||
* default: `0` | ||
- default: `0` | ||
@@ -239,3 +240,3 @@ #### `VueTypes.integer` | ||
* default: `0` | ||
- default: `0` | ||
@@ -246,3 +247,3 @@ #### `VueTypes.object` | ||
* default: an empty object | ||
- default: an empty object | ||
@@ -255,3 +256,3 @@ _Note: [Vue prop validation](https://vuejs.org/v2/guide/components-props.html#Prop-Validation) requires Object definitions to provide default value as a factory function. `VueTypes.object.def()` accepts both factory functions and plain objects. In the latter case, VueTypes will convert the value to a factory function for you._ | ||
* default: `''` | ||
- default: `''` | ||
@@ -266,3 +267,3 @@ #### `VueTypes.symbol` | ||
* default: none | ||
- default: none | ||
@@ -283,3 +284,3 @@ ### Native Types Configuration | ||
VueTypes.sensibleDefaults = { | ||
string: 'mystringdefault' | ||
string: 'mystringdefault', | ||
//... | ||
@@ -312,6 +313,6 @@ } | ||
* **doesn't have** any sensible default value | ||
* **doesn't have** a `validate` method | ||
* has a `.def()` method to assign a default value on the current prop | ||
* has an `isRequired` flag to set the `required: true` key | ||
- **doesn't have** any sensible default value | ||
- **doesn't have** a `validate` method | ||
- has a `.def()` method to assign a default value on the current prop | ||
- has an `isRequired` flag to set the `required: true` key | ||
@@ -339,4 +340,4 @@ ```js | ||
props: { | ||
user: VueTypes.instanceOf(Person) | ||
} | ||
user: VueTypes.instanceOf(Person), | ||
}, | ||
} | ||
@@ -354,4 +355,4 @@ ``` | ||
props: { | ||
genre: VueTypes.oneOf(['action', 'thriller']) | ||
} | ||
genre: VueTypes.oneOf(['action', 'thriller']), | ||
}, | ||
} | ||
@@ -370,5 +371,5 @@ ``` | ||
VueTypes.integer, | ||
VueTypes.instanceOf(Person) | ||
]) | ||
} | ||
VueTypes.instanceOf(Person), | ||
]), | ||
}, | ||
} | ||
@@ -384,4 +385,4 @@ ``` | ||
props: { | ||
theProp: VueTypes.arrayOf(String) | ||
} | ||
theProp: VueTypes.arrayOf(String), | ||
}, | ||
} | ||
@@ -400,4 +401,4 @@ | ||
props: { | ||
userData: VueTypes.objectOf(String) | ||
} | ||
userData: VueTypes.objectOf(String), | ||
}, | ||
} | ||
@@ -411,3 +412,3 @@ | ||
Validates that a prop is an object taking on a particular shape. Accepts both simple and `vue-types` types. You can set shape's properties as `required` but (obviously) you cannot use `.def()`. On the other hand you can use `def()` to set a default value for the shape itself. Like `VueTypes.array` and `VueTypes.object`, you can pass to `.def()` either a factory function returning an object or a plain object. | ||
Validates that a prop is an object taking on a particular shape. Accepts both simple and `vue-types` types. You can set shape's properties as `required` but (obviously) you cannot use `.def()`. On the other hand you can use `def()` to set a default value for the shape itself. Like `VueTypes.array` and `VueTypes.object`, you can pass to `.def()` either a factory function returning an object or a plain object. | ||
@@ -420,5 +421,5 @@ ```js | ||
age: VueTypes.integer, | ||
id: VueTypes.integer.isRequired | ||
}).def(() => ({ name: 'John' })) | ||
} | ||
id: VueTypes.integer.isRequired, | ||
}).def(() => ({ name: 'John' })), | ||
}, | ||
} | ||
@@ -439,9 +440,9 @@ | ||
name: String, | ||
id: VueTypes.integer.isRequired | ||
id: VueTypes.integer.isRequired, | ||
}), | ||
userDataLoose: VueTypes.shape({ | ||
name: String, | ||
id: VueTypes.integer.isRequired | ||
}).loose | ||
} | ||
id: VueTypes.integer.isRequired, | ||
}).loose, | ||
}, | ||
} | ||
@@ -459,11 +460,10 @@ | ||
```js | ||
function minLength(value) { | ||
return typeof value === 'string' && value.length >= 6 | ||
} | ||
return typeof value === 'string' && value.length >= 6 | ||
} | ||
export default { | ||
props: { | ||
theProp: VueTypes.custom(minLength) | ||
} | ||
theProp: VueTypes.custom(minLength), | ||
}, | ||
} | ||
@@ -481,4 +481,4 @@ | ||
function minLength(value) { | ||
return typeof value === 'string' && value.length >= 6 | ||
} | ||
return typeof value === 'string' && value.length >= 6 | ||
} | ||
@@ -489,5 +489,5 @@ export default { | ||
minLength, | ||
'theProp is not a string or is too short' | ||
) | ||
} | ||
'theProp is not a string or is too short', | ||
), | ||
}, | ||
} | ||
@@ -505,2 +505,3 @@ ``` | ||
Examples: | ||
```js | ||
@@ -512,3 +513,3 @@ // as an accessor type | ||
type: Number, | ||
validator: (v) => v < 0 | ||
validator: (v) => v < 0, | ||
}) | ||
@@ -522,3 +523,3 @@ | ||
type: Number, | ||
validator: (v) => v < 0 | ||
validator: (v) => v < 0, | ||
}) | ||
@@ -536,3 +537,3 @@ | ||
type: String, | ||
validator: (max, v) => v.length <= max | ||
validator: (max, v) => v.length <= max, | ||
}) | ||
@@ -546,2 +547,40 @@ | ||
#### Inherit from VueTypes types | ||
You can set a previously set type as _parent_ for a new one by setting it as the new type's `type` property. This feature can be useful to create named aliases: | ||
```js | ||
const shape = VueTypes.shape({ name: String, age: Number }) | ||
VueTypes.extend({ | ||
name: 'user', | ||
getter: true, | ||
type: shape, | ||
}) | ||
console.log(VueTypes.user.type) // Object | ||
const data = { name: 'John', surname: 'Doe' } | ||
console.log(VueTypes.utils.validate(data, VueTypes.user)) // true | ||
``` | ||
Custom validators will be executed before the parent's one: | ||
```js | ||
// ... | ||
VueTypes.extend({ | ||
name: 'userDoe', | ||
getter: true, | ||
type: shape, | ||
validator(value) { | ||
return value && value.surname === 'Doe' | ||
}, | ||
}) | ||
const data = { name: 'John', surname: 'Smith' } | ||
console.log(VueTypes.utils.validate(data, VueTypes.userDoe)) // false | ||
``` | ||
**Note:** Types created with this method don't support the `validate` method even if their parent type supports it (like `VueTypes.string` or `VueTypes.number`). | ||
#### Typescript | ||
@@ -568,3 +607,3 @@ | ||
type: String, | ||
validator: (max: number, v: string) => v.length <= max | ||
validator: (max: number, v: string) => v.length <= max, | ||
}) | ||
@@ -580,14 +619,14 @@ | ||
<template> | ||
<!-- template here --> | ||
<!-- template here --> | ||
</template> | ||
<script lang="ts"> | ||
import Vue from "vue"; | ||
import VueTypes from "./prop-types"; | ||
import Vue from 'vue' | ||
import VueTypes from './prop-types' | ||
export default Vue.extend({ | ||
name: "MyComponent", | ||
props: { | ||
msg: VueTypes.maxLength(2) | ||
} | ||
}); | ||
export default Vue.extend({ | ||
name: 'MyComponent', | ||
props: { | ||
msg: VueTypes.maxLength(2), | ||
}, | ||
}) | ||
</script> | ||
@@ -617,3 +656,3 @@ ``` | ||
return value.length === 'John' | ||
} | ||
}, | ||
} | ||
@@ -635,3 +674,3 @@ | ||
return value.length > 10 | ||
} | ||
}, | ||
} | ||
@@ -643,6 +682,5 @@ | ||
props: { | ||
password: passwordType.isRequired | ||
} | ||
password: passwordType.isRequired, | ||
}, | ||
} | ||
``` | ||
@@ -649,0 +687,0 @@ |
@@ -10,2 +10,4 @@ import isPlainObject from 'is-plain-object' | ||
warn, | ||
has, | ||
stubTrue, | ||
} from './utils' | ||
@@ -108,3 +110,21 @@ import { setDefaults } from './sensibles' | ||
extend(props = {}) { | ||
const { name, validate = false, getter = false, ...type } = props | ||
let { name, validate = false, getter = false, ...opts } = props | ||
if (has(VueTypes, name)) { | ||
throw new TypeError(`[VueTypes error]: Type "${name}" already defined`) | ||
} | ||
const { type, validator = stubTrue } = opts | ||
if (type && type._vueTypes_name) { | ||
// we are using as base type a vue-type object | ||
opts.type = type.type // inherit the base types | ||
opts.required = type.required // inherit the required flag | ||
opts.default = type.default // inherit the default flag | ||
validate = false // we don't allow validate method on this kind of types | ||
if (isFunction(type.validator)) { | ||
opts.validator = function(...args) { | ||
return type.validator.apply(type, args) && validator.apply(this, args) | ||
} | ||
} | ||
} | ||
let descriptor | ||
@@ -114,3 +134,3 @@ if (getter) { | ||
get() { | ||
return toType(name, Object.assign({}, type), validate) | ||
return toType(name, Object.assign({}, opts), validate) | ||
}, | ||
@@ -121,6 +141,6 @@ enumerable: true, | ||
} else { | ||
const { validator } = type | ||
const { validator } = opts | ||
descriptor = { | ||
value(...args) { | ||
const ret = toType(name, Object.assign({}, type), validate) | ||
const ret = toType(name, Object.assign({}, opts), validate) | ||
if (validator) { | ||
@@ -127,0 +147,0 @@ ret.validator = validator.bind(ret, ...args) |
@@ -29,2 +29,7 @@ import isPlainObject from 'is-plain-object' | ||
/** | ||
* A function that always returns true | ||
*/ | ||
export const stubTrue = () => true | ||
/** | ||
* Checks for a own property in an object | ||
@@ -156,2 +161,10 @@ * | ||
withValidate(obj) | ||
} else { | ||
Object.defineProperty(obj, 'validate', { | ||
value() { | ||
warn(`${name} - "validate" method not supported on this type`) | ||
return this | ||
}, | ||
enumerable: false, | ||
}) | ||
} | ||
@@ -158,0 +171,0 @@ |
/*! vue-types - v1.6.0 | ||
/*! vue-types - v1.7.0-beta.1 | ||
* https://github.com/dwightjack/vue-types | ||
@@ -97,2 +97,20 @@ * Copyright (c) 2019 - Marco Solazzi; | ||
/** | ||
* A function that always returns true | ||
*/ | ||
var stubTrue = function stubTrue() { | ||
return true; | ||
}; | ||
/** | ||
* Checks for a own property in an object | ||
* | ||
* @param {object} obj - Object | ||
* @param {string} prop - Property to check | ||
* @returns {boolean} | ||
*/ | ||
var has = function has(obj, prop) { | ||
return hasOwn.call(obj, prop); | ||
}; | ||
/** | ||
* Determines whether the passed value is an integer. Uses `Number.isInteger` if available | ||
@@ -219,2 +237,10 @@ * | ||
withValidate(obj); | ||
} else { | ||
Object.defineProperty(obj, 'validate', { | ||
value: function value() { | ||
warn(name + " - \"validate\" method not supported on this type"); | ||
return this; | ||
}, | ||
enumerable: false | ||
}); | ||
} | ||
@@ -424,4 +450,33 @@ | ||
getter = _props$getter === void 0 ? false : _props$getter, | ||
type = _objectWithoutPropertiesLoose(_props, ["name", "validate", "getter"]); | ||
opts = _objectWithoutPropertiesLoose(_props, ["name", "validate", "getter"]); | ||
if (has(VueTypes, name)) { | ||
throw new TypeError("[VueTypes error]: Type \"" + name + "\" already defined"); | ||
} | ||
var type = opts.type, | ||
_opts$validator = opts.validator, | ||
validator = _opts$validator === void 0 ? stubTrue : _opts$validator; | ||
if (type && type._vueTypes_name) { | ||
// we are using as base type a vue-type object | ||
opts.type = type.type; // inherit the base types | ||
opts.required = type.required; // inherit the required flag | ||
opts.default = type.default; // inherit the default flag | ||
validate = false; // we don't allow validate method on this kind of types | ||
if (isFunction(type.validator)) { | ||
opts.validator = function () { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return type.validator.apply(type, args) && validator.apply(this, args); | ||
}; | ||
} | ||
} | ||
var descriptor; | ||
@@ -432,3 +487,3 @@ | ||
get: function get() { | ||
return toType(name, Object.assign({}, type), validate); | ||
return toType(name, Object.assign({}, opts), validate); | ||
}, | ||
@@ -439,13 +494,13 @@ enumerable: true, | ||
} else { | ||
var validator = type.validator; | ||
var _validator = opts.validator; | ||
descriptor = { | ||
value: function value() { | ||
var ret = toType(name, Object.assign({}, type), validate); | ||
var ret = toType(name, Object.assign({}, opts), validate); | ||
if (validator) { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
if (_validator) { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
ret.validator = validator.bind.apply(validator, [ret].concat(args)); | ||
ret.validator = _validator.bind.apply(_validator, [ret].concat(args)); | ||
} | ||
@@ -452,0 +507,0 @@ |
@@ -1,2 +0,2 @@ | ||
/*! vue-types - v1.6.0 | ||
/*! vue-types - v1.7.0-beta.1 | ||
* https://github.com/dwightjack/vue-types | ||
@@ -13,3 +13,3 @@ * Copyright (c) 2019 - Marco Solazzi; | ||
*/ | ||
function r(e){return!0=== | ||
function n(e){return!0=== | ||
/*! | ||
@@ -21,3 +21,3 @@ * isobject <https://github.com/jonschlinkert/isobject> | ||
*/ | ||
function(e){return null!=e&&"object"==typeof e&&!1===Array.isArray(e)}(e)&&"[object Object]"===Object.prototype.toString.call(e)}function f(e){var t,n;return!1!==r(e)&&("function"==typeof(t=e.constructor)&&(!1!==r(n=t.prototype)&&!1!==n.hasOwnProperty("isPrototypeOf")))}e=e&&e.hasOwnProperty("default")?e.default:e;var t=Object.prototype,n=t.toString,c=t.hasOwnProperty,s=/^\s*function (\w+)/;function l(e){var t=null!=e?e.type?e.type:e:null,n=t&&t.toString().match(s);return n&&n[1]}function y(){}var u=Number.isInteger||function(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e},p=Array.isArray||function(e){return"[object Array]"===n.call(e)},d=function(e){return"[object Function]"===n.call(e)};function v(e,t,n){return void 0===n&&(n=!1),Object.defineProperty(t,"_vueTypes_name",{enumerable:!1,writable:!1,value:e}),function(e){Object.defineProperty(e,"def",{value:function(e){return(void 0!==e||this.default)&&(d(e)||b(this,e)?p(e)?this.default=function(){return[].concat(e)}:f(e)?this.default=function(){return Object.assign({},e)}:this.default=e:m(this._vueTypes_name+' - invalid default value: "'+e+'"',e)),this},enumerable:!1,writable:!1})}(function(e){return Object.defineProperty(e,"isRequired",{get:function(){return this.required=!0,this},enumerable:!1})}(t)),n&&function(e){Object.defineProperty(e,"validate",{value:function(e){return this.validator=e.bind(this),this},enumerable:!1})}(t),d(t.validator)&&(t.validator=t.validator.bind(t)),t}function b(e,t,n){void 0===n&&(n=!1);var r,u=e,o=!0;f(e)||(u={type:e});var i,a=u._vueTypes_name?u._vueTypes_name+" - ":"";if(c.call(u,"type")&&null!==u.type){if(void 0===u.type)throw new TypeError("[VueTypes error]: Setting type to undefined is not allowed.");if(!u.required&&void 0===t)return o;p(u.type)?(o=u.type.some(function(e){return b(e,t,!0)}),r=u.type.map(function(e){return l(e)}).join(" or ")):o="Array"===(r=l(u))?p(t):"Object"===r?f(t):"String"===r||"Number"===r||"Boolean"===r||"Function"===r?function(e){if(null==e)return null;var t=e.constructor.toString().match(s);return t&&t[1]}(t)===r:t instanceof u.type}return o?(c.call(u,"validator")&&d(u.validator)&&(n&&(i=m,m=y),o=u.validator(t),i&&(m=i),o||!1!==n||m(a+"custom validation failed")),o):(!1===n&&m(a+'value "'+t+'" should be of type "'+r+'"'),!1)}var o,i,m=y,a={get any(){return v("any",{type:null},!0)},get func(){return v("function",{type:Function},!0).def(a.sensibleDefaults.func)},get bool(){return v("boolean",{type:Boolean},!0).def(a.sensibleDefaults.bool)},get string(){return v("string",{type:String},!0).def(a.sensibleDefaults.string)},get number(){return v("number",{type:Number},!0).def(a.sensibleDefaults.number)},get array(){return v("array",{type:Array},!0).def(a.sensibleDefaults.array)},get object(){return v("object",{type:Object},!0).def(a.sensibleDefaults.object)},get integer(){return v("integer",{type:Number,validator:function(e){return u(e)}}).def(a.sensibleDefaults.integer)},get symbol(){return v("symbol",{type:null,validator:function(e){return"symbol"==typeof e}},!0)},extend:function(e){void 0===e&&(e={});var t,n=e,u=n.name,r=n.validate,o=void 0!==r&&r,i=n.getter,a=void 0!==i&&i,f=function(e,t){if(null==e)return{};var n,r,u={},o=Object.keys(e);for(r=0;r<o.length;r++)n=o[r],0<=t.indexOf(n)||(u[n]=e[n]);return u}(n,["name","validate","getter"]);if(a)t={get:function(){return v(u,Object.assign({},f),o)},enumerable:!0,configurable:!1};else{var c=f.validator;t={value:function(){var e=v(u,Object.assign({},f),o);if(c){for(var t=arguments.length,n=new Array(t),r=0;r<t;r++)n[r]=arguments[r];e.validator=c.bind.apply(c,[e].concat(n))}return e},writable:!1,enumerable:!0,configurable:!1}}return Object.defineProperty(this,u,t)},custom:function(n,r){if(void 0===r&&(r="custom validation failed"),"function"!=typeof n)throw new TypeError("[VueTypes error]: You must provide a function as argument");return v(n.name||"<<anonymous function>>",{validator:function(e){var t=n(e);return t||m(this._vueTypes_name+" - "+r),t}})},oneOf:function(n){if(!p(n))throw new TypeError("[VueTypes error]: You must provide an array as argument");var r='oneOf - value should be one of "'+n.join('", "')+'"',e=n.reduce(function(e,t){return null!=t&&-1===e.indexOf(t.constructor)&&e.push(t.constructor),e},[]);return v("oneOf",{type:0<e.length?e:null,validator:function(e){var t=-1!==n.indexOf(e);return t||m(r),t}})},instanceOf:function(e){return v("instanceOf",{type:e})},oneOfType:function(n){if(!p(n))throw new TypeError("[VueTypes error]: You must provide an array as argument");var r=!1,e=n.reduce(function(e,t){if(f(t)){if("oneOf"===t._vueTypes_name)return e.concat(t.type||[]);if(t.type&&!d(t.validator)){if(p(t.type))return e.concat(t.type);e.push(t.type)}else d(t.validator)&&(r=!0);return e}return e.push(t),e},[]);if(!r)return v("oneOfType",{type:e});var u=n.map(function(e){return e&&p(e.type)?e.type.map(l):l(e)}).reduce(function(e,t){return e.concat(p(t)?t:[t])},[]).join('", "');return this.custom(function(t){var e=n.some(function(e){return"oneOf"===e._vueTypes_name?!e.type||b(e.type,t,!0):b(e,t,!0)});return e||m('oneOfType - value type should be one of "'+u+'"'),e})},arrayOf:function(n){return v("arrayOf",{type:Array,validator:function(e){var t=e.every(function(e){return b(n,e)});return t||m('arrayOf - value must be an array of "'+l(n)+'"'),t}})},objectOf:function(n){return v("objectOf",{type:Object,validator:function(t){var e=Object.keys(t).every(function(e){return b(n,t[e])});return e||m('objectOf - value must be an object of "'+l(n)+'"'),e}})},shape:function(u){var o=Object.keys(u),e=o.filter(function(e){return u[e]&&!0===u[e].required}),t=v("shape",{type:Object,validator:function(t){var n=this;if(!f(t))return!1;var r=Object.keys(t);return 0<e.length&&e.some(function(e){return-1===r.indexOf(e)})?(m('shape - at least one of required properties "'+e.join('", "')+'" is not present'),!1):r.every(function(e){return-1===o.indexOf(e)?!0===n._vueTypes_isLoose||(m('shape - object is missing "'+e+'" property'),!1):b(u[e],t[e])})}});return Object.defineProperty(t,"_vueTypes_isLoose",{enumerable:!1,writable:!0,value:!1}),Object.defineProperty(t,"loose",{get:function(){return this._vueTypes_isLoose=!0,this},enumerable:!1}),t}};return o=a,i={func:function(){},bool:!0,string:"",number:0,array:function(){return[]},object:function(){return{}},integer:0},Object.defineProperty(o,"sensibleDefaults",{enumerable:!1,set:function(e){i=!1===e?{}:!0===e?{func:function(){},bool:!0,string:"",number:0,array:function(){return[]},object:function(){return{}},integer:0}:e},get:function(){return i}}),a.utils={validate:function(e,t){return b(t,e,!0)},toType:v},a}); | ||
function(e){return null!=e&&"object"==typeof e&&!1===Array.isArray(e)}(e)&&"[object Object]"===Object.prototype.toString.call(e)}function f(e){var t,r;return!1!==n(e)&&("function"==typeof(t=e.constructor)&&(!1!==n(r=t.prototype)&&!1!==r.hasOwnProperty("isPrototypeOf")))}e=e&&e.hasOwnProperty("default")?e.default:e;var t=Object.prototype,r=t.toString,p=t.hasOwnProperty,c=/^\s*function (\w+)/;function l(e){var t=null!=e?e.type?e.type:e:null,r=t&&t.toString().match(c);return r&&r[1]}function s(){}function d(){return!0}var u=Number.isInteger||function(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e},y=Array.isArray||function(e){return"[object Array]"===r.call(e)},v=function(e){return"[object Function]"===r.call(e)};function b(e,t,r){return void 0===r&&(r=!1),Object.defineProperty(t,"_vueTypes_name",{enumerable:!1,writable:!1,value:e}),function(e){Object.defineProperty(e,"def",{value:function(e){return(void 0!==e||this.default)&&(v(e)||m(this,e)?y(e)?this.default=function(){return[].concat(e)}:f(e)?this.default=function(){return Object.assign({},e)}:this.default=e:h(this._vueTypes_name+' - invalid default value: "'+e+'"',e)),this},enumerable:!1,writable:!1})}(function(e){return Object.defineProperty(e,"isRequired",{get:function(){return this.required=!0,this},enumerable:!1})}(t)),r?function(e){Object.defineProperty(e,"validate",{value:function(e){return this.validator=e.bind(this),this},enumerable:!1})}(t):Object.defineProperty(t,"validate",{value:function(){return h(e+' - "validate" method not supported on this type'),this},enumerable:!1}),v(t.validator)&&(t.validator=t.validator.bind(t)),t}function m(e,t,r){void 0===r&&(r=!1);var n,u=e,o=!0;f(e)||(u={type:e});var i,a=u._vueTypes_name?u._vueTypes_name+" - ":"";if(p.call(u,"type")&&null!==u.type){if(void 0===u.type)throw new TypeError("[VueTypes error]: Setting type to undefined is not allowed.");if(!u.required&&void 0===t)return o;y(u.type)?(o=u.type.some(function(e){return m(e,t,!0)}),n=u.type.map(function(e){return l(e)}).join(" or ")):o="Array"===(n=l(u))?y(t):"Object"===n?f(t):"String"===n||"Number"===n||"Boolean"===n||"Function"===n?function(e){if(null==e)return null;var t=e.constructor.toString().match(c);return t&&t[1]}(t)===n:t instanceof u.type}return o?(p.call(u,"validator")&&v(u.validator)&&(r&&(i=h,h=s),o=u.validator(t),i&&(h=i),o||!1!==r||h(a+"custom validation failed")),o):(!1===r&&h(a+'value "'+t+'" should be of type "'+n+'"'),!1)}var o,i,h=s,O={get any(){return b("any",{type:null},!0)},get func(){return b("function",{type:Function},!0).def(O.sensibleDefaults.func)},get bool(){return b("boolean",{type:Boolean},!0).def(O.sensibleDefaults.bool)},get string(){return b("string",{type:String},!0).def(O.sensibleDefaults.string)},get number(){return b("number",{type:Number},!0).def(O.sensibleDefaults.number)},get array(){return b("array",{type:Array},!0).def(O.sensibleDefaults.array)},get object(){return b("object",{type:Object},!0).def(O.sensibleDefaults.object)},get integer(){return b("integer",{type:Number,validator:function(e){return u(e)}}).def(O.sensibleDefaults.integer)},get symbol(){return b("symbol",{type:null,validator:function(e){return"symbol"==typeof e}},!0)},extend:function(e){void 0===e&&(e={});var t=e,u=t.name,r=t.validate,o=void 0!==r&&r,n=t.getter,i=void 0!==n&&n,a=function(e,t){if(null==e)return{};var r,n,u={},o=Object.keys(e);for(n=0;n<o.length;n++)r=o[n],0<=t.indexOf(r)||(u[r]=e[r]);return u}(t,["name","validate","getter"]);if(function(e,t){return p.call(e,t)}(O,u))throw new TypeError('[VueTypes error]: Type "'+u+'" already defined');var f,c=a.type,l=a.validator,s=void 0===l?d:l;if(c&&c._vueTypes_name&&(a.type=c.type,a.required=c.required,a.default=c.default,o=!1,v(c.validator)&&(a.validator=function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];return c.validator.apply(c,t)&&s.apply(this,t)})),i)f={get:function(){return b(u,Object.assign({},a),o)},enumerable:!0,configurable:!1};else{var y=a.validator;f={value:function(){var e=b(u,Object.assign({},a),o);if(y){for(var t=arguments.length,r=new Array(t),n=0;n<t;n++)r[n]=arguments[n];e.validator=y.bind.apply(y,[e].concat(r))}return e},writable:!1,enumerable:!0,configurable:!1}}return Object.defineProperty(this,u,f)},custom:function(r,n){if(void 0===n&&(n="custom validation failed"),"function"!=typeof r)throw new TypeError("[VueTypes error]: You must provide a function as argument");return b(r.name||"<<anonymous function>>",{validator:function(e){var t=r(e);return t||h(this._vueTypes_name+" - "+n),t}})},oneOf:function(r){if(!y(r))throw new TypeError("[VueTypes error]: You must provide an array as argument");var n='oneOf - value should be one of "'+r.join('", "')+'"',e=r.reduce(function(e,t){return null!=t&&-1===e.indexOf(t.constructor)&&e.push(t.constructor),e},[]);return b("oneOf",{type:0<e.length?e:null,validator:function(e){var t=-1!==r.indexOf(e);return t||h(n),t}})},instanceOf:function(e){return b("instanceOf",{type:e})},oneOfType:function(r){if(!y(r))throw new TypeError("[VueTypes error]: You must provide an array as argument");var n=!1,e=r.reduce(function(e,t){if(f(t)){if("oneOf"===t._vueTypes_name)return e.concat(t.type||[]);if(t.type&&!v(t.validator)){if(y(t.type))return e.concat(t.type);e.push(t.type)}else v(t.validator)&&(n=!0);return e}return e.push(t),e},[]);if(!n)return b("oneOfType",{type:e});var u=r.map(function(e){return e&&y(e.type)?e.type.map(l):l(e)}).reduce(function(e,t){return e.concat(y(t)?t:[t])},[]).join('", "');return this.custom(function(t){var e=r.some(function(e){return"oneOf"===e._vueTypes_name?!e.type||m(e.type,t,!0):m(e,t,!0)});return e||h('oneOfType - value type should be one of "'+u+'"'),e})},arrayOf:function(r){return b("arrayOf",{type:Array,validator:function(e){var t=e.every(function(e){return m(r,e)});return t||h('arrayOf - value must be an array of "'+l(r)+'"'),t}})},objectOf:function(r){return b("objectOf",{type:Object,validator:function(t){var e=Object.keys(t).every(function(e){return m(r,t[e])});return e||h('objectOf - value must be an object of "'+l(r)+'"'),e}})},shape:function(u){var o=Object.keys(u),e=o.filter(function(e){return u[e]&&!0===u[e].required}),t=b("shape",{type:Object,validator:function(t){var r=this;if(!f(t))return!1;var n=Object.keys(t);return 0<e.length&&e.some(function(e){return-1===n.indexOf(e)})?(h('shape - at least one of required properties "'+e.join('", "')+'" is not present'),!1):n.every(function(e){return-1===o.indexOf(e)?!0===r._vueTypes_isLoose||(h('shape - object is missing "'+e+'" property'),!1):m(u[e],t[e])})}});return Object.defineProperty(t,"_vueTypes_isLoose",{enumerable:!1,writable:!0,value:!1}),Object.defineProperty(t,"loose",{get:function(){return this._vueTypes_isLoose=!0,this},enumerable:!1}),t}};return o=O,i={func:function(){},bool:!0,string:"",number:0,array:function(){return[]},object:function(){return{}},integer:0},Object.defineProperty(o,"sensibleDefaults",{enumerable:!1,set:function(e){i=!1===e?{}:!0===e?{func:function(){},bool:!0,string:"",number:0,array:function(){return[]},object:function(){return{}},integer:0}:e},get:function(){return i}}),O.utils={validate:function(e,t){return m(t,e,!0)},toType:b},O}); | ||
//# sourceMappingURL=vue-types.min.js.map |
@@ -1,2 +0,2 @@ | ||
/*! vue-types - v1.6.0 | ||
/*! vue-types - v1.7.0-beta.1 | ||
* https://github.com/dwightjack/vue-types | ||
@@ -3,0 +3,0 @@ * Copyright (c) 2019 - Marco Solazzi; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
186215
2917
653
3