-
Notifications
You must be signed in to change notification settings - Fork 29
/
ValidatorErrors.ts
107 lines (91 loc) · 4.02 KB
/
ValidatorErrors.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { List, Map, Record } from 'immutable'
export type ValidatorErrorsSingle = List<Map<string, any>>
export type ValidatorErrorsValue = Map<string, ValidatorErrorsSingle>
export interface ValidatorErrorsInterface {
errCount: number
errors: ValidatorErrorsValue
childErrors: Map<string, any>
errorsToJS: () => { [key: string]: { [key: string]: any }[] }
getErrors: () => ValidatorErrorsValue
addError: (type: string, context?: Map<string, any>) => ValidatorErrorsType
addErrors: (errors: ValidatorErrorsType) => ValidatorErrorsType
addChildErrors: (errors: ValidatorErrorsType) => ValidatorErrorsType
addErrorsToChild: (errors: ValidatorErrorsType) => ValidatorErrorsType
hasError: (type?: string) => boolean
getError: (type: string) => ValidatorErrorsSingle
}
export type onErrorHandler = (errors: ValidatorErrorsType | undefined) => void
export type onErrors = (cb: onErrorHandler) => void
export type ValidatorErrorsType = Record<ValidatorErrorsInterface> & ValidatorErrorsInterface
export class ValidatorErrors extends Record({
errors: Map({}),
childErrors: Map({}),
errCount: 0,
}) {
errorsToJS(): { [key: string]: { [key: string]: any }[] } {
return this.get('errors').toJS() as { [key: string]: { [key: string]: any }[] }
}
getErrors(): ValidatorErrorsValue {
return this.get('errors') as ValidatorErrorsValue
}
getChildErrors(): ValidatorErrorsValue {
return this.get('childErrors') as ValidatorErrorsValue
}
addError(type: string, context: Map<string, any> = Map()): ValidatorErrors {
let typeErrors = (this.get('errors') as ValidatorErrorsValue)?.get(type) as ValidatorErrorsSingle
if (!List.isList(typeErrors)) {
typeErrors = List()
}
typeErrors = typeErrors.push(context)
return this
.setIn(['errors', type], typeErrors)
.set('errCount', this.errCount + 1)
}
private addChildError(type: string, context: Map<string, any> = Map()): ValidatorErrors {
let typeErrors = this.getIn(['childErrors', type]) as ValidatorErrorsSingle
if (!List.isList(typeErrors)) {
typeErrors = List()
}
typeErrors = typeErrors.push(context)
return this.setIn(['childErrors', type], typeErrors)
// incrementing the global errCount not the children
.set('errCount', this.errCount + 1)
}
addChildErrors(errors: ValidatorErrors): ValidatorErrors {
return errors.getChildErrors().keySeq().reduce((errs, type) => {
return errors.getChildError(type).reduce((errs, error) => {
return errs.addChildError(type, error)
}, errs)
}, this as ValidatorErrors)
}
addErrors(errors: ValidatorErrors): ValidatorErrors {
return errors.getErrors().keySeq().reduce((errs, type) => {
return errors.getError(type).reduce((errs, error) => {
return errs.addError(type, error)
}, errs)
}, this as ValidatorErrors)
}
addErrorsToChild(errors: ValidatorErrors): ValidatorErrors {
return errors.getErrors().keySeq().reduce((errs, type) => {
return errors.getError(type).reduce((errs, error) => {
return errs.addChildError(type, error)
}, errs)
}, this as ValidatorErrors)
}
hasError(type?: string): boolean {
const typeErrors = this.getIn(type ? ['errors', type] : ['errors'])
return Boolean(
(List.isList(typeErrors) && typeErrors.size) ||
(Map.isMap(typeErrors) && typeErrors.keySeq().size)
)
}
getError(type: string): ValidatorErrorsSingle {
return this.getIn(['errors', type]) as ValidatorErrorsSingle || List()
}
getChildError(type: string): ValidatorErrorsSingle {
return this.getIn(['childErrors', type]) as ValidatorErrorsSingle || List()
}
}
export const createValidatorErrors = (): ValidatorErrorsType => {
return new ValidatorErrors() as unknown as ValidatorErrorsType
}