@@ -68,7 +68,50 @@ module.exports = {
68
68
refute ( this . model . preValidate ( { name : 'name' } ) ) ;
69
69
}
70
70
}
71
- }
72
- }
71
+ } ,
72
+
73
+ "when model has dependancies between validation functions" : {
74
+ setUp : function ( ) {
75
+ var CARD_TYPES = {
76
+ VISA : 0 ,
77
+ AMEX : 1
78
+ } ;
79
+ var Model = Backbone . Model . extend ( {
80
+ validation : {
81
+ card_type : {
82
+ required : true
83
+ } ,
84
+ security_code : function ( value , attr , computedState ) {
85
+ var requiredLength = ( computedState . card_type === CARD_TYPES . AMEX ? 4 : 3 ) ;
86
+ if ( value && _ . isString ( value ) && value . length !== requiredLength ) {
87
+ return 'Please enter a valid security code.' ;
88
+ }
89
+ }
90
+ }
91
+ } ) ;
92
+ Model . CARD_TYPES = CARD_TYPES ;
93
+ this . ModelDefinition = Model ;
94
+ this . model = new Model ( ) ;
95
+ Backbone . Validation . bind ( new Backbone . View ( { model : this . model } ) ) ;
96
+ } ,
97
+
98
+ "and pre-validating hash of attributes" : {
99
+ "returns error object when value is not valid" : function ( ) {
100
+ var result = this . model . preValidate ( { card_type : this . ModelDefinition . CARD_TYPES . VISA , security_code : '1234' } ) ;
101
+ assert ( result . security_code ) ;
102
+ refute ( result . card_type ) ;
103
+ } ,
73
104
105
+ "returns error object when values are not valid" : function ( ) {
106
+ var result = this . model . preValidate ( { card_type : '' , security_code : '12345' } ) ;
107
+ assert ( result . card_type ) ;
108
+ assert ( result . security_code ) ;
109
+ } ,
110
+
111
+ "returns nothing when value is valid" : function ( ) {
112
+ refute ( this . model . preValidate ( { card_type : this . ModelDefinition . CARD_TYPES . AMEX , security_code : '1234' } ) ) ;
113
+ }
114
+ }
115
+ }
116
+ }
74
117
}
0 commit comments