0
# Schema Management
1
2
Combine, validate, and manage React Native component schemas with utilities for merging multiple schema files and ensuring correctness.
3
4
## Capabilities
5
6
### Schema Validation
7
8
Validate React Native schemas to ensure they follow the correct format and don't contain conflicts.
9
10
```javascript { .api }
11
/**
12
* Validate a schema and throw an error if invalid
13
* @param schema - Schema object to validate
14
* @throws Error with detailed validation failure information
15
*/
16
function validate(schema: SchemaType): void;
17
18
/**
19
* Get validation errors without throwing
20
* @param schema - Schema object to validate
21
* @returns Array of error messages, empty if valid
22
*/
23
function getErrors(schema: SchemaType): Array<string>;
24
```
25
26
**Usage Example:**
27
28
```javascript
29
const SchemaValidator = require('@react-native/codegen/lib/SchemaValidator');
30
31
try {
32
SchemaValidator.validate(schema);
33
console.log('Schema is valid');
34
} catch (error) {
35
console.error('Schema validation failed:', error.message);
36
}
37
38
// Or check errors without throwing
39
const errors = SchemaValidator.getErrors(schema);
40
if (errors.length > 0) {
41
console.error('Validation errors:', errors);
42
}
43
```
44
45
### Schema Combination
46
47
Combine multiple JavaScript/TypeScript files into a single schema file.
48
49
```javascript { .api }
50
/**
51
* Combine schemas from multiple files and write to output file
52
* @param fileList - Array of file paths to combine
53
* @param platform - Platform filter (ios/android) or null for all platforms
54
* @param outfile - Path where combined schema should be written
55
* @param excludeRegExp - Regular expression to exclude files, or null
56
* @param libraryName - Name for the combined library, or null
57
*/
58
function combineSchemasInFileListAndWriteToFile(
59
fileList: Array<string>,
60
platform: string | null,
61
outfile: string,
62
excludeRegExp: RegExp | null,
63
libraryName: string | null
64
): void;
65
66
/**
67
* Combine schemas from multiple files into a single schema object
68
* @param files - Array of file paths to combine
69
* @param libraryName - Name for the combined library, or null
70
* @returns Combined schema object
71
*/
72
function combineSchemas(
73
files: Array<string>,
74
libraryName: string | null
75
): SchemaType;
76
```
77
78
**Usage Example:**
79
80
```javascript
81
const { combineSchemasInFileListAndWriteToFile } = require('@react-native/codegen/lib/cli/combine/combine-js-to-schema');
82
83
// Combine multiple component files into one schema
84
combineSchemasInFileListAndWriteToFile(
85
['./Component1.js', './Component2.tsx', './Module.ts'],
86
'ios', // Platform filter
87
'./combined-schema.json',
88
/test/, // Exclude test files
89
'MyLibrary'
90
);
91
```
92
93
### File Filtering Utilities
94
95
Utilities for filtering and processing files during schema combination.
96
97
```javascript { .api }
98
/**
99
* Filter function to identify JavaScript/TypeScript files suitable for parsing
100
* @param filename - File path to check
101
* @returns true if file should be included in schema generation
102
*/
103
function filterJSFile(filename: string): boolean;
104
```
105
106
**Usage Example:**
107
108
```javascript
109
const { filterJSFile } = require('@react-native/codegen/lib/cli/combine/combine-utils');
110
const glob = require('glob');
111
112
// Find all parseable files in a directory
113
const allFiles = glob.sync('src/**/*');
114
const jsFiles = allFiles.filter(filterJSFile);
115
console.log('Files to parse:', jsFiles);
116
```
117
118
## Schema Validation Rules
119
120
The validation system checks for several types of errors:
121
122
### Component Name Conflicts
123
124
```javascript
125
// This would cause a validation error:
126
// File1.js exports component "Button"
127
// File2.js also exports component "Button"
128
```
129
130
Components with the same name across different modules are not allowed, as this creates ambiguity in the generated code.
131
132
### Platform Exclusions
133
134
Schemas can specify platform exclusions to prevent generation for specific platforms:
135
136
```javascript { .api }
137
interface ComponentSchema {
138
type: 'Component';
139
components: { [componentName: string]: ComponentInfo };
140
/** Platforms where this component should not be generated */
141
excludedPlatforms?: Array<PlatformType>;
142
}
143
144
interface NativeModuleSchema {
145
type: 'NativeModule';
146
aliasMap: NativeModuleAliasMap;
147
enumMap: NativeModuleEnumMap;
148
spec: NativeModuleSpec;
149
/** Platforms where this module should not be generated */
150
excludedPlatforms?: Array<PlatformType>;
151
}
152
153
type PlatformType = 'iOS' | 'android';
154
```
155
156
### Schema Structure Requirements
157
158
Valid schemas must have:
159
- At least one module definition
160
- Proper component or module type specification
161
- Valid type annotations for all properties
162
- Consistent naming conventions
163
164
## Advanced Schema Operations
165
166
### Platform-Specific Filtering
167
168
When combining schemas, you can filter by platform to create platform-specific builds:
169
170
```javascript
171
// Generate iOS-only schema
172
combineSchemasInFileListAndWriteToFile(
173
files,
174
'ios', // Only include iOS-compatible components
175
'ios-schema.json',
176
null,
177
'MyLibraryIOS'
178
);
179
180
// Generate Android-only schema
181
combineSchemasInFileListAndWriteToFile(
182
files,
183
'android', // Only include Android-compatible components
184
'android-schema.json',
185
null,
186
'MyLibraryAndroid'
187
);
188
```
189
190
### Selective File Inclusion
191
192
Use regular expressions to exclude specific file patterns:
193
194
```javascript
195
const testFileRegex = /__tests__|\.test\.|\.spec\./;
196
197
combineSchemasInFileListAndWriteToFile(
198
files,
199
null,
200
'production-schema.json',
201
testFileRegex, // Exclude test files
202
'MyLibrary'
203
);
204
```
205
206
### Schema Merging Logic
207
208
When combining schemas, the system:
209
210
1. **Parses each file** using the appropriate parser (Flow/TypeScript)
211
2. **Filters by platform** if specified
212
3. **Merges module definitions** from all files
213
4. **Validates uniqueness** of component/module names
214
5. **Applies exclusion rules** based on regex patterns
215
6. **Outputs combined schema** in standardized format
216
217
Conflicting definitions with the same name cause validation errors and must be resolved by renaming one of the conflicting items.