/
StaticViewConfigValidator.js
159 lines (145 loc) · 3.76 KB
/
StaticViewConfigValidator.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/
import {type ViewConfig} from '../Renderer/shims/ReactNativeTypes';
type Difference =
| {
type: 'missing',
path: Array<string>,
nativeValue: mixed,
}
| {
type: 'unequal',
path: Array<string>,
nativeValue: mixed,
staticValue: mixed,
}
| {
type: 'unexpected',
path: Array<string>,
staticValue: mixed,
};
type ValidationResult = ValidResult | InvalidResult;
type ValidResult = {
type: 'valid',
};
type InvalidResult = {
type: 'invalid',
differences: Array<Difference>,
};
/**
* During the migration from native view configs to static view configs, this is
* used to validate that the two are equivalent.
*/
export function validate(
name: string,
nativeViewConfig: ViewConfig,
staticViewConfig: ViewConfig,
): ValidationResult {
const differences = [];
accumulateDifferences(
differences,
[],
{
bubblingEventTypes: nativeViewConfig.bubblingEventTypes,
directEventTypes: nativeViewConfig.directEventTypes,
uiViewClassName: nativeViewConfig.uiViewClassName,
validAttributes: nativeViewConfig.validAttributes,
},
{
bubblingEventTypes: staticViewConfig.bubblingEventTypes,
directEventTypes: staticViewConfig.directEventTypes,
uiViewClassName: staticViewConfig.uiViewClassName,
validAttributes: staticViewConfig.validAttributes,
},
);
if (differences.length === 0) {
return {type: 'valid'};
}
return {
type: 'invalid',
differences,
};
}
export function stringifyValidationResult(
name: string,
validationResult: InvalidResult,
): string {
const {differences} = validationResult;
return [
`StaticViewConfigValidator: Invalid static view config for '${name}'.`,
'',
...differences.map(difference => {
const {type, path} = difference;
switch (type) {
case 'missing':
return `- '${path.join('.')}' is missing.`;
case 'unequal':
return `- '${path.join('.')}' is the wrong value.`;
case 'unexpected':
return `- '${path.join('.')}' is present but not expected to be.`;
}
}),
'',
].join('\n');
}
function accumulateDifferences(
differences: Array<Difference>,
path: Array<string>,
nativeObject: {...},
staticObject: {...},
): void {
for (const nativeKey in nativeObject) {
const nativeValue = nativeObject[nativeKey];
if (!staticObject.hasOwnProperty(nativeKey)) {
differences.push({
path: [...path, nativeKey],
type: 'missing',
nativeValue,
});
continue;
}
const staticValue = staticObject[nativeKey];
const nativeValueIfObject = ifObject(nativeValue);
if (nativeValueIfObject != null) {
const staticValueIfObject = ifObject(staticValue);
if (staticValueIfObject != null) {
path.push(nativeKey);
accumulateDifferences(
differences,
path,
nativeValueIfObject,
staticValueIfObject,
);
path.pop();
continue;
}
}
if (nativeValue !== staticValue) {
differences.push({
path: [...path, nativeKey],
type: 'unequal',
nativeValue,
staticValue,
});
}
}
for (const staticKey in staticObject) {
if (!nativeObject.hasOwnProperty(staticKey)) {
differences.push({
path: [...path, staticKey],
type: 'unexpected',
staticValue: staticObject[staticKey],
});
}
}
}
function ifObject(value: mixed): ?{...} {
return typeof value === 'object' && !Array.isArray(value) ? value : null;
}