@@ -24,6 +24,7 @@ function success<T>(t: T): ValidationResult<T> {
24
24
value : t ,
25
25
} ;
26
26
}
27
+
27
28
function failure < T > ( errors : IValidationError [ ] ) : ValidationResult < T > {
28
29
return {
29
30
success : false ,
@@ -100,33 +101,24 @@ export function object<S>(schema: { [P in keyof S]: Validator<S[P]> }): Validato
100
101
}
101
102
102
103
if ( ! validationFailed ) {
103
- return {
104
- success : true ,
105
- // when we get here, all properties in S have been validated and assigned, so
106
- // this type assertion is okay.
107
- value : scrutinee as { [ P in keyof S ] : S [ P ] } ,
108
- } ;
104
+ // when we get here, all properties in S have been validated and assigned, so
105
+ // this type assertion is okay.
106
+ return success ( scrutinee as { [ P in keyof S ] : S [ P ] } ) ;
109
107
}
110
108
111
- return {
112
- success : false ,
113
- errors,
114
- } ;
109
+ return failure ( errors ) ;
115
110
} ;
116
111
}
117
112
118
113
export function array < S > ( memberValidator : Validator < S > ) : Validator < S [ ] > {
119
114
return ( scrutinee ) => {
120
115
if ( ! ( scrutinee instanceof Array ) ) {
121
- return {
122
- success : false ,
123
- errors : [
124
- {
125
- path : [ ] ,
126
- message : 'expected an array' ,
127
- } ,
128
- ] ,
129
- } ;
116
+ return failure ( [
117
+ {
118
+ path : [ ] ,
119
+ message : 'expected an array' ,
120
+ } ,
121
+ ] ) ;
130
122
}
131
123
132
124
const errors : IValidationError [ ] = [ ] ;
@@ -147,26 +139,17 @@ export function array<S>(memberValidator: Validator<S>): Validator<S[]> {
147
139
}
148
140
149
141
if ( ! validationFailed ) {
150
- return {
151
- success : true ,
152
- value : scrutinee ,
153
- } ;
142
+ return success ( scrutinee ) ;
154
143
}
155
144
156
- return {
157
- success : false ,
158
- errors,
159
- } ;
145
+ return failure ( errors ) ;
160
146
} ;
161
147
}
162
148
163
149
export function optional < T > ( validator : Validator < T > ) : Validator < T | null > {
164
150
return ( scrutinee ) => {
165
151
if ( scrutinee === null ) {
166
- return {
167
- success : true ,
168
- value : null ,
169
- } ;
152
+ return success ( null ) ;
170
153
}
171
154
return validator ( scrutinee ) ;
172
155
} ;
0 commit comments