@@ -3,37 +3,50 @@ import Errors from 'ember-validations/errors';
3
3
import Base from 'ember-validations/validators/base' ;
4
4
import getOwner from 'ember-getowner-polyfill' ;
5
5
6
- var get = Ember . get ;
7
- var set = Ember . set ;
8
-
9
- var setValidityMixin = Ember . Mixin . create ( {
10
- isValid :
Ember . computed ( '[email protected] ' , function ( ) {
11
- var compactValidators = get ( this , 'validators' ) . compact ( ) ;
12
- var filteredValidators = compactValidators . filter ( function ( validator ) {
13
- return ! get ( validator , 'isValid' ) ;
14
- } ) ;
6
+ const {
7
+ A : emberArray ,
8
+ ArrayProxy,
9
+ Mixin,
10
+ RSVP : { all, reject } ,
11
+ computed,
12
+ computed : { alias, not } ,
13
+ get,
14
+ inject : { service } ,
15
+ isArray,
16
+ isNone,
17
+ isPresent,
18
+ set,
19
+ warn
20
+ } = Ember ;
21
+
22
+ const setValidityMixin = Mixin . create ( {
23
+ isValid :
computed ( '[email protected] ' , function ( ) {
24
+ let compactValidators = get ( this , 'validators' ) . compact ( ) ;
25
+ let filteredValidators = compactValidators . filter ( ( validator ) => ! get ( validator , 'isValid' ) ) ;
15
26
16
27
return get ( filteredValidators , 'length' ) === 0 ;
17
28
} ) ,
18
- isInvalid : Ember . computed . not ( 'isValid' )
29
+
30
+ isInvalid : not ( 'isValid' )
19
31
} ) ;
20
32
21
- var pushValidatableObject = function ( model , property ) {
22
- var content = get ( model , property ) ;
33
+ const pushValidatableObject = function ( model , property ) {
34
+ let content = get ( model , property ) ;
23
35
24
36
model . removeObserver ( property , pushValidatableObject ) ;
25
- if ( Ember . isArray ( content ) ) {
26
- model . validators . pushObject ( ArrayValidatorProxy . create ( { model : model , property : property , contentBinding : 'model.' + property } ) ) ;
37
+
38
+ if ( isArray ( content ) ) {
39
+ model . validators . pushObject ( ArrayValidatorProxy . create ( { model, property, contentBinding : `model.${ property } ` } ) ) ;
27
40
} else {
28
41
model . validators . pushObject ( content ) ;
29
42
}
30
43
} ;
31
44
32
- var lookupValidator = function ( validatorName ) {
33
- var owner = getOwner ( this ) ;
34
- var service = owner . lookup ( 'service:validations ') ;
35
- var validators = [ ] ;
36
- var cache ;
45
+ const lookupValidator = function ( validatorName ) {
46
+ let owner = getOwner ( this ) ;
47
+ let service = get ( this , 'validationService ') ;
48
+ let validators = [ ] ;
49
+ let cache ;
37
50
38
51
if ( service ) {
39
52
cache = get ( service , 'cache' ) ;
@@ -44,19 +57,19 @@ var lookupValidator = function(validatorName) {
44
57
if ( cache [ validatorName ] ) {
45
58
validators = validators . concat ( cache [ validatorName ] ) ;
46
59
} else {
47
- var local = owner . resolveRegistration ( ' validator:local/' + validatorName ) ;
48
- var remote = owner . resolveRegistration ( ' validator:remote/' + validatorName ) ;
60
+ let local = owner . resolveRegistration ( ` validator:local/${ validatorName } ` ) ;
61
+ let remote = owner . resolveRegistration ( ` validator:remote/${ validatorName } ` ) ;
49
62
50
63
if ( local || remote ) {
51
64
validators = validators . concat ( [ local , remote ] ) ;
52
65
} else {
53
- var base = owner . resolveRegistration ( ' validator:' + validatorName ) ;
66
+ let base = owner . resolveRegistration ( ` validator:${ validatorName } ` ) ;
54
67
55
68
if ( base ) {
56
69
validators = validators . concat ( [ base ] ) ;
57
70
} else {
58
- local = owner . resolveRegistration ( ' ember-validations@validator:local/' + validatorName ) ;
59
- remote = owner . resolveRegistration ( ' ember-validations@validator:remote/' + validatorName ) ;
71
+ local = owner . resolveRegistration ( ` ember-validations@validator:local/${ validatorName } ` ) ;
72
+ remote = owner . resolveRegistration ( ` ember-validations@validator:remote/${ validatorName } ` ) ;
60
73
61
74
if ( local || remote ) {
62
75
validators = validators . concat ( [ local , remote ] ) ;
@@ -67,46 +80,64 @@ var lookupValidator = function(validatorName) {
67
80
cache [ validatorName ] = validators ;
68
81
}
69
82
70
- Ember . warn ( 'Could not find the "' + validatorName + '" validator.' , ! Ember . isEmpty ( validators ) , { id : 'ember-validations.faild-to-find-validator' } ) ;
83
+ warn ( `Could not find the "${ validatorName } " validator.` , isPresent ( validators ) , {
84
+ id : 'ember-validations.faild-to-find-validator'
85
+ } ) ;
71
86
72
87
return validators ;
73
88
} ;
74
89
75
- var ArrayValidatorProxy = Ember . ArrayProxy . extend ( setValidityMixin , {
76
- validate : function ( ) {
90
+ const ArrayValidatorProxy = ArrayProxy . extend ( setValidityMixin , {
91
+ init ( ) {
92
+ this . _validate ( ) ;
93
+ } ,
94
+
95
+ validate ( ) {
77
96
return this . _validate ( ) ;
78
97
} ,
79
- _validate : Ember . on ( 'init' , function ( ) {
80
- var promises = get ( this , 'content' ) . invoke ( '_validate' ) . without ( undefined ) ;
81
- return Ember . RSVP . all ( promises ) ;
82
- } ) ,
83
- validators : Ember . computed . alias ( 'content' )
98
+
99
+ _validate ( ) {
100
+ let promises = get ( this , 'content' ) . invoke ( '_validate' ) . without ( undefined ) ;
101
+ return all ( promises ) ;
102
+ } ,
103
+
104
+ validators : alias ( 'content' )
84
105
} ) ;
85
106
86
- export default Ember . Mixin . create ( setValidityMixin , {
87
- init : function ( ) {
88
- this . _super ( ) ;
107
+ export default Mixin . create ( setValidityMixin , {
108
+ validationService : service ( 'validations' ) ,
109
+
110
+ init ( ) {
111
+ this . _super ( ...arguments ) ;
89
112
this . errors = Errors . create ( ) ;
90
113
this . dependentValidationKeys = { } ;
91
- this . validators = Ember . A ( ) ;
114
+ this . validators = emberArray ( ) ;
115
+
92
116
if ( get ( this , 'validations' ) === undefined ) {
93
117
this . validations = { } ;
94
118
}
119
+
95
120
this . buildValidators ( ) ;
96
- this . validators . forEach ( function ( validator ) {
121
+
122
+ this . validators . forEach ( ( validator ) => {
97
123
validator . addObserver ( 'errors.[]' , this , function ( sender ) {
98
- var errors = Ember . A ( ) ;
99
- this . validators . forEach ( function ( validator ) {
124
+ let errors = emberArray ( ) ;
125
+
126
+ this . validators . forEach ( ( validator ) => {
100
127
if ( validator . property === sender . property ) {
101
128
errors . addObjects ( validator . errors ) ;
102
129
}
103
- } , this ) ;
104
- set ( this , 'errors.' + sender . property , errors ) ;
130
+ } ) ;
131
+
132
+ set ( this , `errors.${ sender . property } ` , errors ) ;
105
133
} ) ;
106
- } , this ) ;
134
+ } ) ;
135
+
136
+ this . _validate ( ) ;
107
137
} ,
108
- buildValidators : function ( ) {
109
- var property ;
138
+
139
+ buildValidators ( ) {
140
+ let property ;
110
141
111
142
for ( property in this . validations ) {
112
143
if ( this . validations [ property ] . constructor === Object ) {
@@ -116,57 +147,66 @@ export default Ember.Mixin.create(setValidityMixin, {
116
147
}
117
148
}
118
149
} ,
119
- buildRuleValidator : function ( property ) {
120
- var pushValidator = function ( validator ) {
150
+
151
+ buildRuleValidator ( property ) {
152
+ let pushValidator = ( validator , validatorName ) => {
121
153
if ( validator ) {
122
- this . validators . pushObject ( validator . create ( { model : this , property : property , options : this . validations [ property ] [ validatorName ] } ) ) ;
154
+ this . validators . pushObject ( validator . create ( { model : this , property, options : this . validations [ property ] [ validatorName ] } ) ) ;
123
155
}
124
156
} ;
125
157
126
158
if ( this . validations [ property ] . callback ) {
127
159
this . validations [ property ] = { inline : this . validations [ property ] } ;
128
160
}
129
161
130
- var createInlineClass = function ( callback ) {
162
+ let createInlineClass = ( callback ) => {
131
163
return Base . extend ( {
132
- call : function ( ) {
133
- var errorMessage = this . callback . call ( this ) ;
164
+ call ( ) {
165
+ let errorMessage = this . callback . call ( this ) ;
134
166
135
167
if ( errorMessage ) {
136
168
this . errors . pushObject ( errorMessage ) ;
137
169
}
138
170
} ,
139
- callback : callback
171
+
172
+ callback
140
173
} ) ;
141
174
} ;
142
175
143
- for ( var validatorName in this . validations [ property ] ) {
176
+ Object . keys ( this . validations [ property ] ) . forEach ( ( validatorName ) => {
144
177
if ( validatorName === 'inline' ) {
145
- pushValidator . call ( this , createInlineClass ( this . validations [ property ] [ validatorName ] . callback ) ) ;
178
+ let validator = createInlineClass ( this . validations [ property ] [ validatorName ] . callback ) ;
179
+ pushValidator ( validator , validatorName ) ;
146
180
} else if ( this . validations [ property ] . hasOwnProperty ( validatorName ) ) {
147
- lookupValidator . call ( this , validatorName ) . forEach ( pushValidator , this ) ;
181
+ lookupValidator . call ( this , validatorName ) . forEach ( ( validator ) => {
182
+ return pushValidator . call ( this , validator , validatorName ) ;
183
+ } ) ;
148
184
}
149
- }
185
+ } ) ;
150
186
} ,
151
- buildObjectValidator : function ( property ) {
152
- if ( Ember . isNone ( get ( this , property ) ) ) {
187
+
188
+ buildObjectValidator ( property ) {
189
+ if ( isNone ( get ( this , property ) ) ) {
153
190
this . addObserver ( property , this , pushValidatableObject ) ;
154
191
} else {
155
192
pushValidatableObject ( this , property ) ;
156
193
}
157
194
} ,
158
- validate : function ( ) {
159
- var self = this ;
160
- return this . _validate ( ) . then ( function ( vals ) {
161
- var errors = get ( self , 'errors' ) ;
195
+
196
+ validate ( ) {
197
+ return this . _validate ( ) . then ( ( vals ) => {
198
+ let errors = get ( this , 'errors' ) ;
199
+
162
200
if ( vals . indexOf ( false ) > - 1 ) {
163
- return Ember . RSVP . reject ( errors ) ;
201
+ return reject ( errors ) ;
164
202
}
203
+
165
204
return errors ;
166
205
} ) ;
167
206
} ,
168
- _validate : Ember . on ( 'init' , function ( ) {
169
- var promises = this . validators . invoke ( '_validate' ) . without ( undefined ) ;
170
- return Ember . RSVP . all ( promises ) ;
171
- } )
207
+
208
+ _validate ( ) {
209
+ let promises = this . validators . invoke ( '_validate' ) . without ( undefined ) ;
210
+ return all ( promises ) ;
211
+ }
172
212
} ) ;
0 commit comments