@@ -19,39 +19,42 @@ const {
1919 mapValueInRange
2020} = rebound . MathUtil
2121
22+ const isNumber = ( value ) =>
23+ typeof value === 'number'
24+ const isFunction = ( value ) =>
25+ typeof value === 'function'
26+ const isBool = ( value ) =>
27+ value === true || value === false
28+ const isArray = ( value ) =>
29+ Array . isArray ( value )
30+
31+ const validate = ( validator ) => ( options , key , method ) => {
32+ if ( ! validator ( options [ key ] ) ) {
33+ throw new Error ( `Option "${ key } " of method "${ method } " was invalid` )
34+ }
35+ }
36+
2237const optionTypes = {
2338 pushView : {
24- view : React . PropTypes . element . isRequired ,
25- transition : React . PropTypes . oneOfType ( [
26- React . PropTypes . func ,
27- React . PropTypes . number
28- ] ) ,
29- onComplete : React . PropTypes . func
39+ view : validate ( React . isValidElement ) ,
40+ transition : validate ( x => isFunction ( x ) || isNumber ( x ) ) ,
41+ onComplete : validate ( isFunction )
3042 } ,
3143 popView : {
32- transition : React . PropTypes . oneOfType ( [
33- React . PropTypes . func ,
34- React . PropTypes . number
35- ] ) ,
36- onComplete : React . PropTypes . func
44+ transition : validate ( x => isFunction ( x ) || isNumber ( x ) ) ,
45+ onComplete : validate ( isFunction )
3746 } ,
3847 popToRootView : {
39- transition : React . PropTypes . oneOfType ( [
40- React . PropTypes . func ,
41- React . PropTypes . number
42- ] ) ,
43- onComplete : React . PropTypes . func
48+ transition : validate ( x => isFunction ( x ) || isNumber ( x ) ) ,
49+ onComplete : validate ( isFunction )
4450 } ,
4551 setViews : {
46- views : React . PropTypes . arrayOf (
47- React . PropTypes . element
48- ) . isRequired ,
49- preserveState : React . PropTypes . bool ,
50- transition : React . PropTypes . oneOfType ( [
51- React . PropTypes . func ,
52- React . PropTypes . number
53- ] ) ,
54- onComplete : React . PropTypes . func
52+ views : validate ( x => isArray ( x ) && x . reduce ( ( valid , e ) => {
53+ return valid === false ? false : React . isValidElement ( e )
54+ } , true ) === true ) ,
55+ preserveState : validate ( isBool ) ,
56+ transition : validate ( x => isFunction ( x ) || isNumber ( x ) ) ,
57+ onComplete : validate ( isFunction )
5558 }
5659}
5760
@@ -65,7 +68,7 @@ function checkOptions (method, options) {
6568 const optionType = optionTypes [ method ]
6669 Object . keys ( options ) . forEach ( key => {
6770 if ( optionType [ key ] ) {
68- const e = optionType [ key ] ( options , key , method , 'prop' )
71+ const e = optionType [ key ] ( options , key , method )
6972 if ( e ) throw e
7073 }
7174 } )
0 commit comments