Skip to content

Commit 70ab56c

Browse files
committed
Merge branch 'hotfix/60-shim'
2 parents ff18e8d + 2e7458f commit 70ab56c

File tree

3 files changed

+53
-31
lines changed

3 files changed

+53
-31
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-types",
3-
"version": "1.6.0",
3+
"version": "1.6.1-beta.1",
44
"description": "Prop types utility for Vue",
55
"author": "Marco Solazzi",
66
"license": "MIT",

src/shim.js

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@ import Vue from 'vue'
22
import isPlainObject from 'is-plain-object'
33
import { setDefaults } from './sensibles'
44

5+
const dfn = Object.defineProperty
6+
57
const isArray =
68
Array.isArray ||
79
function(value) {
810
return Object.prototype.toString.call(value) === '[object Array]'
911
}
1012

11-
const type = (props) =>
12-
Object.assign(
13-
{
14-
def(v) {
13+
function type(name, props, validable = false) {
14+
const descriptors = {
15+
_vueTypes_name: {
16+
value: name,
17+
},
18+
def: {
19+
value(v) {
1520
if (v === undefined && !this.default) {
1621
return this
1722
}
@@ -24,16 +29,30 @@ const type = (props) =>
2429
}
2530
return this
2631
},
27-
get isRequired() {
32+
},
33+
isRequired: {
34+
get() {
2835
this.required = true
2936
return this
3037
},
31-
validator() {
32-
return true
33-
},
3438
},
39+
}
40+
41+
if (validable) {
42+
descriptors.validate = {
43+
value() {},
44+
}
45+
}
46+
return Object.assign(
47+
Object.defineProperties(
48+
{
49+
validator: () => true,
50+
},
51+
descriptors,
52+
),
3553
props,
3654
)
55+
}
3756

3857
const vueTypes = setDefaults({
3958
utils: {
@@ -47,7 +66,6 @@ const typeMap = {
4766
bool: Boolean,
4867
string: String,
4968
number: Number,
50-
integer: Number,
5169
array: Array,
5270
object: Object,
5371
arrayOf: Array,
@@ -74,44 +92,47 @@ const methods = [
7492
'objectOf',
7593
]
7694

77-
function createValidator(root, name, getter = false, props) {
95+
function createValidator(root, name, props, getter = false, validable = false) {
7896
const prop = getter ? 'get' : 'value'
7997
const descr = {
8098
[prop]: () =>
81-
type(props).def(getter ? vueTypes.sensibleDefaults[name] : undefined),
99+
type(name, props, validable).def(
100+
getter ? vueTypes.sensibleDefaults[name] : undefined,
101+
),
82102
}
83103

84-
return Object.defineProperty(root, name, descr)
104+
return dfn(root, name, descr)
85105
}
86106

87-
getters.forEach((p) =>
88-
createValidator(vueTypes, p, true, {
89-
type: typeMap[p] || null,
90-
validate() {},
91-
}),
92-
)
93-
methods.forEach((p) =>
94-
createValidator(vueTypes, p, false, { type: typeMap[p] || null }),
95-
)
96-
createValidator(vueTypes, 'integer', true, { type: Number }) // does not have a validate method
97-
98-
Object.defineProperty(vueTypes, 'shape', {
107+
function recurseValidator(root, getter, validable) {
108+
return (name) =>
109+
createValidator(
110+
root,
111+
name,
112+
{ type: typeMap[name] || null },
113+
getter,
114+
validable,
115+
)
116+
}
117+
118+
getters.forEach(recurseValidator(vueTypes, true, true))
119+
methods.forEach(recurseValidator(vueTypes, false))
120+
121+
createValidator(vueTypes, 'integer', { type: Number }, true) // does not have a validate method
122+
123+
dfn(vueTypes, 'shape', {
99124
value() {
100-
return Object.defineProperty(type({ type: Object }), 'loose', {
125+
return dfn(type('shape', { type: Object }), 'loose', {
101126
get() {
102127
return this
103128
},
104-
enumerable: false,
105129
})
106130
},
107131
})
108132

109133
vueTypes.extend = function extend(props) {
110134
const { name, validate, getter = false, type = null } = props
111-
return createValidator(vueTypes, name, getter, {
112-
type,
113-
validate: validate ? () => {} : undefined,
114-
})
135+
return createValidator(vueTypes, name, { type }, getter, !!validate)
115136
}
116137

117138
/* eslint-disable no-console */

src/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ export function withValidate(type) {
139139
*
140140
* @param {string} name - Type internal name
141141
* @param {object} obj - Object to enhance
142+
* @param {boolean} [validateFn=false] - add the `validate()` method to the type object
142143
* @returns {object}
143144
*/
144145
export function toType(name, obj, validateFn = false) {

0 commit comments

Comments
 (0)