|
1 | 1 | import Type from './types/Type' |
2 | | -import makeJSONError from './errorReporting/makeJSONError' |
3 | 2 | import { weakSetAdd, weakSetDelete, weakSetHas } from './cyclic' |
4 | 3 |
|
5 | 4 | export type IdentifierPath = Array<string | number | symbol> |
6 | 5 |
|
7 | 6 | export type ErrorTuple = [IdentifierPath, string, Type<any>] |
8 | 7 |
|
9 | 8 | export default class Validation<T> { |
10 | | - input: T |
| 9 | + readonly input: T |
11 | 10 |
|
12 | | - path: IdentifierPath = [] |
| 11 | + readonly path: IdentifierPath = [] |
13 | 12 |
|
14 | | - prefix = '' |
| 13 | + readonly prefix: string |
15 | 14 |
|
16 | | - errors: ErrorTuple[] = [] |
| 15 | + readonly errors: ErrorTuple[] = [] |
17 | 16 |
|
18 | 17 | // Tracks whether we're in validation of cyclic objects. |
19 | 18 | cyclic: WeakMap<Type<any>, WeakSet<any>> = new WeakMap() |
20 | 19 |
|
21 | | - constructor(input: T) { |
| 20 | + constructor(input: T, prefix = '', path?: IdentifierPath) { |
22 | 21 | this.input = input |
| 22 | + this.prefix = prefix |
| 23 | + if (path) this.path.push(...path) |
23 | 24 | } |
24 | 25 |
|
25 | 26 | inCycle(type: Type<any>, input: any): boolean { |
@@ -47,52 +48,8 @@ export default class Validation<T> { |
47 | 48 | } |
48 | 49 | } |
49 | 50 |
|
50 | | - hasErrors(path?: IdentifierPath): boolean { |
51 | | - if (path) { |
52 | | - for (const [candidate] of this.errors) { |
53 | | - if (matchPath(path, candidate)) { |
54 | | - return true |
55 | | - } |
56 | | - } |
57 | | - return false |
58 | | - } else { |
59 | | - return this.errors.length > 0 |
60 | | - } |
61 | | - } |
62 | | - |
63 | | - addError( |
64 | | - path: IdentifierPath, |
65 | | - expectedType: Type<any>, |
66 | | - message: string |
67 | | - ): this { |
68 | | - this.errors.push([path, message, expectedType]) |
69 | | - return this |
70 | | - } |
71 | | - |
72 | | - clearError(path: IdentifierPath | null | undefined): boolean { |
73 | | - let didClear = false |
74 | | - if (path) { |
75 | | - const errors = [] |
76 | | - for (const error of this.errors) { |
77 | | - if (matchPath(path, error[0])) { |
78 | | - didClear = true |
79 | | - } else { |
80 | | - errors.push(error) |
81 | | - } |
82 | | - } |
83 | | - this.errors = errors |
84 | | - } else { |
85 | | - didClear = this.errors.length > 0 |
86 | | - this.errors = [] |
87 | | - } |
88 | | - return didClear |
89 | | - } |
90 | | - |
91 | | - resolvePath(path: IdentifierPath): any { |
92 | | - return resolvePath(this.input, path) |
93 | | - } |
94 | | - toJSON(): any { |
95 | | - return makeJSONError(this) |
| 51 | + hasErrors(): boolean { |
| 52 | + return this.errors.length > 0 |
96 | 53 | } |
97 | 54 | } |
98 | 55 |
|
@@ -141,19 +98,3 @@ export function resolvePath(input: any, path: IdentifierPath): any { |
141 | 98 | } |
142 | 99 | return subject |
143 | 100 | } |
144 | | - |
145 | | -export function matchPath( |
146 | | - path: IdentifierPath, |
147 | | - candidate: IdentifierPath |
148 | | -): boolean { |
149 | | - const { length } = path |
150 | | - if (length > candidate.length) { |
151 | | - return false |
152 | | - } |
153 | | - for (let i = 0; i < length; i++) { |
154 | | - if (candidate[i] !== path[i]) { |
155 | | - return false |
156 | | - } |
157 | | - } |
158 | | - return true |
159 | | -} |
0 commit comments