@@ -3,37 +3,50 @@ import Errors from 'ember-validations/errors';
33import Base from 'ember-validations/validators/base' ;
44import getOwner from 'ember-getowner-polyfill' ;
55
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' ) ) ;
1526
1627 return get ( filteredValidators , 'length' ) === 0 ;
1728 } ) ,
18- isInvalid : Ember . computed . not ( 'isValid' )
29+
30+ isInvalid : not ( 'isValid' )
1931} ) ;
2032
21- var pushValidatableObject = function ( model , property ) {
22- var content = get ( model , property ) ;
33+ const pushValidatableObject = function ( model , property ) {
34+ let content = get ( model , property ) ;
2335
2436 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 } ` } ) ) ;
2740 } else {
2841 model . validators . pushObject ( content ) ;
2942 }
3043} ;
3144
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 ;
3750
3851 if ( service ) {
3952 cache = get ( service , 'cache' ) ;
@@ -44,19 +57,19 @@ var lookupValidator = function(validatorName) {
4457 if ( cache [ validatorName ] ) {
4558 validators = validators . concat ( cache [ validatorName ] ) ;
4659 } 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 } ` ) ;
4962
5063 if ( local || remote ) {
5164 validators = validators . concat ( [ local , remote ] ) ;
5265 } else {
53- var base = owner . resolveRegistration ( ' validator:' + validatorName ) ;
66+ let base = owner . resolveRegistration ( ` validator:${ validatorName } ` ) ;
5467
5568 if ( base ) {
5669 validators = validators . concat ( [ base ] ) ;
5770 } 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 } ` ) ;
6073
6174 if ( local || remote ) {
6275 validators = validators . concat ( [ local , remote ] ) ;
@@ -67,46 +80,64 @@ var lookupValidator = function(validatorName) {
6780 cache [ validatorName ] = validators ;
6881 }
6982
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+ } ) ;
7186
7287 return validators ;
7388} ;
7489
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 ( ) {
7796 return this . _validate ( ) ;
7897 } ,
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' )
84105} ) ;
85106
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 ) ;
89112 this . errors = Errors . create ( ) ;
90113 this . dependentValidationKeys = { } ;
91- this . validators = Ember . A ( ) ;
114+ this . validators = emberArray ( ) ;
115+
92116 if ( get ( this , 'validations' ) === undefined ) {
93117 this . validations = { } ;
94118 }
119+
95120 this . buildValidators ( ) ;
96- this . validators . forEach ( function ( validator ) {
121+
122+ this . validators . forEach ( ( validator ) => {
97123 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 ) => {
100127 if ( validator . property === sender . property ) {
101128 errors . addObjects ( validator . errors ) ;
102129 }
103- } , this ) ;
104- set ( this , 'errors.' + sender . property , errors ) ;
130+ } ) ;
131+
132+ set ( this , `errors.${ sender . property } ` , errors ) ;
105133 } ) ;
106- } , this ) ;
134+ } ) ;
135+
136+ this . _validate ( ) ;
107137 } ,
108- buildValidators : function ( ) {
109- var property ;
138+
139+ buildValidators ( ) {
140+ let property ;
110141
111142 for ( property in this . validations ) {
112143 if ( this . validations [ property ] . constructor === Object ) {
@@ -116,57 +147,66 @@ export default Ember.Mixin.create(setValidityMixin, {
116147 }
117148 }
118149 } ,
119- buildRuleValidator : function ( property ) {
120- var pushValidator = function ( validator ) {
150+
151+ buildRuleValidator ( property ) {
152+ let pushValidator = ( validator , validatorName ) => {
121153 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 ] } ) ) ;
123155 }
124156 } ;
125157
126158 if ( this . validations [ property ] . callback ) {
127159 this . validations [ property ] = { inline : this . validations [ property ] } ;
128160 }
129161
130- var createInlineClass = function ( callback ) {
162+ let createInlineClass = ( callback ) => {
131163 return Base . extend ( {
132- call : function ( ) {
133- var errorMessage = this . callback . call ( this ) ;
164+ call ( ) {
165+ let errorMessage = this . callback . call ( this ) ;
134166
135167 if ( errorMessage ) {
136168 this . errors . pushObject ( errorMessage ) ;
137169 }
138170 } ,
139- callback : callback
171+
172+ callback
140173 } ) ;
141174 } ;
142175
143- for ( var validatorName in this . validations [ property ] ) {
176+ Object . keys ( this . validations [ property ] ) . forEach ( ( validatorName ) => {
144177 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 ) ;
146180 } 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+ } ) ;
148184 }
149- }
185+ } ) ;
150186 } ,
151- buildObjectValidator : function ( property ) {
152- if ( Ember . isNone ( get ( this , property ) ) ) {
187+
188+ buildObjectValidator ( property ) {
189+ if ( isNone ( get ( this , property ) ) ) {
153190 this . addObserver ( property , this , pushValidatableObject ) ;
154191 } else {
155192 pushValidatableObject ( this , property ) ;
156193 }
157194 } ,
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+
162200 if ( vals . indexOf ( false ) > - 1 ) {
163- return Ember . RSVP . reject ( errors ) ;
201+ return reject ( errors ) ;
164202 }
203+
165204 return errors ;
166205 } ) ;
167206 } ,
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+ }
172212} ) ;
0 commit comments