forked from VoliJS/Type-R
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.ts
More file actions
50 lines (40 loc) · 1.28 KB
/
validation.ts
File metadata and controls
50 lines (40 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
export interface ChildrenErrors {
[ key : string ] : ValidationError | any
}
export interface Validatable {
/** @internal */
_validateNested( errors : ChildrenErrors ) : number;
validate( self : any ) : any
get( key : string ) : any
}
// Validation error object.
export class ValidationError {
// Invalid nested object keys
nested : ChildrenErrors
length : number
// Local error
error : any
constructor( obj : Validatable ){
this.length = obj._validateNested( this.nested = {} );
if( this.error = obj.validate( obj ) ){
this.length++;
}
}
each( iteratee : ( value : any, key : string ) => void ) : void {
const { error, nested } = this;
if( error ) iteratee( error, null );
for( const key in nested ){
iteratee( nested[ key ], key );
}
}
eachError( iteratee : ( error : any, key : string, object : Validatable ) => void, object : Validatable ) : void {
this.each( ( value : any, key : string ) => {
if( value instanceof ValidationError ){
(<ValidationError>value).eachError( iteratee, object.get( key ) );
}
else{
iteratee( value, key, object );
}
});
}
}