1
- var __extends = ( this && this . __extends ) || function ( d , b ) {
2
- for ( var p in b ) if ( b . hasOwnProperty ( p ) ) d [ p ] = b [ p ] ;
3
- function __ ( ) { this . constructor = d ; }
4
- d . prototype = b === null ? Object . create ( b ) : ( __ . prototype = b . prototype , new __ ( ) ) ;
5
- } ;
6
1
import { ViewResources } from 'aurelia-templating' ;
7
2
import { Validator } from '../validator' ;
8
3
import { ValidateResult } from '../validate-result' ;
@@ -12,14 +7,12 @@ import { ValidationMessageProvider } from './validation-messages';
12
7
* Validates.
13
8
* Responsible for validating objects and properties.
14
9
*/
15
- var StandardValidator = ( function ( _super ) {
16
- __extends ( StandardValidator , _super ) ;
17
- function StandardValidator ( messageProvider , resources ) {
18
- var _this = _super . call ( this ) || this ;
19
- _this . messageProvider = messageProvider ;
20
- _this . lookupFunctions = resources . lookupFunctions ;
21
- _this . getDisplayName = messageProvider . getDisplayName . bind ( messageProvider ) ;
22
- return _this ;
10
+ export class StandardValidator extends Validator {
11
+ constructor ( messageProvider , resources ) {
12
+ super ( ) ;
13
+ this . messageProvider = messageProvider ;
14
+ this . lookupFunctions = resources . lookupFunctions ;
15
+ this . getDisplayName = messageProvider . getDisplayName . bind ( messageProvider ) ;
23
16
}
24
17
/**
25
18
* Validates the specified property.
@@ -28,92 +21,88 @@ var StandardValidator = (function (_super) {
28
21
* @param rules Optional. If unspecified, the rules will be looked up using the metadata
29
22
* for the object created by ValidationRules....on(class/object)
30
23
*/
31
- StandardValidator . prototype . validateProperty = function ( object , propertyName , rules ) {
24
+ validateProperty ( object , propertyName , rules ) {
32
25
return this . validate ( object , propertyName , rules || null ) ;
33
- } ;
26
+ }
34
27
/**
35
28
* Validates all rules for specified object and it's properties.
36
29
* @param object The object to validate.
37
30
* @param rules Optional. If unspecified, the rules will be looked up using the metadata
38
31
* for the object created by ValidationRules....on(class/object)
39
32
*/
40
- StandardValidator . prototype . validateObject = function ( object , rules ) {
33
+ validateObject ( object , rules ) {
41
34
return this . validate ( object , null , rules || null ) ;
42
- } ;
35
+ }
43
36
/**
44
37
* Determines whether a rule exists in a set of rules.
45
38
* @param rules The rules to search.
46
39
* @parem rule The rule to find.
47
40
*/
48
- StandardValidator . prototype . ruleExists = function ( rules , rule ) {
49
- var i = rules . length ;
41
+ ruleExists ( rules , rule ) {
42
+ let i = rules . length ;
50
43
while ( i -- ) {
51
44
if ( rules [ i ] . indexOf ( rule ) !== - 1 ) {
52
45
return true ;
53
46
}
54
47
}
55
48
return false ;
56
- } ;
57
- StandardValidator . prototype . getMessage = function ( rule , object , value ) {
58
- var expression = rule . message || this . messageProvider . getMessage ( rule . messageKey ) ;
59
- var _a = rule . property , propertyName = _a . name , displayName = _a . displayName ;
49
+ }
50
+ getMessage ( rule , object , value ) {
51
+ const expression = rule . message || this . messageProvider . getMessage ( rule . messageKey ) ;
52
+ let { name : propertyName , displayName } = rule . property ;
60
53
if ( propertyName !== null ) {
61
54
displayName = this . messageProvider . getDisplayName ( propertyName , displayName ) ;
62
55
}
63
- var overrideContext = {
56
+ const overrideContext = {
64
57
$displayName : displayName ,
65
58
$propertyName : propertyName ,
66
59
$value : value ,
67
60
$object : object ,
68
61
$config : rule . config ,
69
62
$getDisplayName : this . getDisplayName
70
63
} ;
71
- return expression . evaluate ( { bindingContext : object , overrideContext : overrideContext } , this . lookupFunctions ) ;
72
- } ;
73
- StandardValidator . prototype . validateRuleSequence = function ( object , propertyName , ruleSequence , sequence , results ) {
74
- var _this = this ;
64
+ return expression . evaluate ( { bindingContext : object , overrideContext } , this . lookupFunctions ) ;
65
+ }
66
+ validateRuleSequence ( object , propertyName , ruleSequence , sequence , results ) {
75
67
// are we validating all properties or a single property?
76
- var validateAllProperties = propertyName === null || propertyName === undefined ;
77
- var rules = ruleSequence [ sequence ] ;
78
- var allValid = true ;
68
+ const validateAllProperties = propertyName === null || propertyName === undefined ;
69
+ const rules = ruleSequence [ sequence ] ;
70
+ let allValid = true ;
79
71
// validate each rule.
80
- var promises = [ ] ;
81
- var _loop_1 = function ( i ) {
82
- var rule = rules [ i ] ;
72
+ const promises = [ ] ;
73
+ for ( let i = 0 ; i < rules . length ; i ++ ) {
74
+ const rule = rules [ i ] ;
83
75
// is the rule related to the property we're validating.
84
76
if ( ! validateAllProperties && rule . property . name !== propertyName ) {
85
- return " continue" ;
77
+ continue ;
86
78
}
87
79
// is this a conditional rule? is the condition met?
88
80
if ( rule . when && ! rule . when ( object ) ) {
89
- return " continue" ;
81
+ continue ;
90
82
}
91
83
// validate.
92
- var value = rule . property . name === null ? object : object [ rule . property . name ] ;
93
- var promiseOrBoolean = rule . condition ( value , object ) ;
84
+ const value = rule . property . name === null ? object : object [ rule . property . name ] ;
85
+ let promiseOrBoolean = rule . condition ( value , object ) ;
94
86
if ( ! ( promiseOrBoolean instanceof Promise ) ) {
95
87
promiseOrBoolean = Promise . resolve ( promiseOrBoolean ) ;
96
88
}
97
- promises . push ( promiseOrBoolean . then ( function ( valid ) {
98
- var message = valid ? null : _this . getMessage ( rule , object , value ) ;
89
+ promises . push ( promiseOrBoolean . then ( valid => {
90
+ const message = valid ? null : this . getMessage ( rule , object , value ) ;
99
91
results . push ( new ValidateResult ( rule , object , rule . property . name , valid , message ) ) ;
100
92
allValid = allValid && valid ;
101
93
return valid ;
102
94
} ) ) ;
103
- } ;
104
- for ( var i = 0 ; i < rules . length ; i ++ ) {
105
- _loop_1 ( i ) ;
106
95
}
107
96
return Promise . all ( promises )
108
- . then ( function ( ) {
97
+ . then ( ( ) => {
109
98
sequence ++ ;
110
99
if ( allValid && sequence < ruleSequence . length ) {
111
- return _this . validateRuleSequence ( object , propertyName , ruleSequence , sequence , results ) ;
100
+ return this . validateRuleSequence ( object , propertyName , ruleSequence , sequence , results ) ;
112
101
}
113
102
return results ;
114
103
} ) ;
115
- } ;
116
- StandardValidator . prototype . validate = function ( object , propertyName , rules ) {
104
+ }
105
+ validate ( object , propertyName , rules ) {
117
106
// rules specified?
118
107
if ( ! rules ) {
119
108
// no. attempt to locate the rules.
@@ -124,8 +113,6 @@ var StandardValidator = (function (_super) {
124
113
return Promise . resolve ( [ ] ) ;
125
114
}
126
115
return this . validateRuleSequence ( object , propertyName , rules , 0 , [ ] ) ;
127
- } ;
128
- return StandardValidator ;
129
- } ( Validator ) ) ;
130
- export { StandardValidator } ;
116
+ }
117
+ }
131
118
StandardValidator . inject = [ ValidationMessageProvider , ViewResources ] ;
0 commit comments