@@ -2,16 +2,21 @@ import Vue from 'vue'
22import isPlainObject from 'is-plain-object'
33import { setDefaults } from './sensibles'
44
5+ const dfn = Object . defineProperty
6+
57const 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
3857const 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
109133vueTypes . 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 */
0 commit comments