@@ -11,6 +11,60 @@ import {
1111 groupFieldsBySection
1212} from './utils' ;
1313
14+ /**
15+ * Validates V2 schema structure for known issues
16+ */
17+ const validateV2Schema = ( schema : V2Schema ) : void => {
18+ const invalidFields : string [ ] = [ ] ;
19+ const supportedFieldTypes = [ 'TEXT' , 'NUMERIC' , 'DATE_TIME' , 'CHOICE_LIST' , 'LOCATION' , 'COLLECTION' , 'ATTACHMENT' ] ;
20+
21+ // Check each field for validity
22+ Object . entries ( schema . json . properties ) . forEach ( ( [ fieldName , property ] ) => {
23+ const uiField = schema . ui . fields [ fieldName ] ;
24+
25+ // Skip deprecated fields without UI definitions (they won't be rendered anyway)
26+ if ( ! uiField && property . deprecated ) {
27+ return ;
28+ }
29+
30+ if ( ! uiField ) {
31+ invalidFields . push ( `${ fieldName } : missing UI field definition` ) ;
32+ return ;
33+ }
34+
35+ // Check for unsupported field types
36+ if ( ! supportedFieldTypes . includes ( uiField . type ) ) {
37+ invalidFields . push ( `${ fieldName } : unsupported field type '${ uiField . type } '` ) ;
38+ return ;
39+ }
40+
41+ // Check CHOICE_LIST fields for valid structure (not content)
42+ if ( uiField . type === 'CHOICE_LIST' ) {
43+ let hasValidStructure = false ;
44+
45+ if ( property . type === 'array' && property . items ?. anyOf ) {
46+ // Check for oneOf arrays in anyOf items for array types (no $ref support)
47+ hasValidStructure = property . items . anyOf . some ( ( anyOfItem : any ) =>
48+ anyOfItem . oneOf && Array . isArray ( anyOfItem . oneOf ) // Empty arrays are valid
49+ ) ;
50+ } else if ( property . anyOf ) {
51+ // Check direct anyOf structure for string types (no $ref support)
52+ hasValidStructure = property . anyOf . some ( ( anyOfItem : any ) =>
53+ anyOfItem . oneOf && Array . isArray ( anyOfItem . oneOf ) // Empty arrays are valid
54+ ) ;
55+ }
56+
57+ if ( ! hasValidStructure ) {
58+ invalidFields . push ( `${ fieldName } : CHOICE_LIST field requires embedded oneOf arrays - $ref not supported` ) ;
59+ }
60+ }
61+ } ) ;
62+
63+ if ( invalidFields . length > 0 ) {
64+ throw new Error ( `Invalid V2 schema structure: ${ invalidFields . join ( ', ' ) } ` ) ;
65+ }
66+ } ;
67+
1468/**
1569 * Generates a JSONForms-compatible UI schema from a EarthRanger V2 schema format
1670 *
@@ -21,8 +75,12 @@ import {
2175 *
2276 * @param schema - EarthRanger V2 schema with json and ui properties
2377 * @returns JSONForms UI schema with single-column VerticalLayout for React Native
78+ * @throws Error if schema has invalid structure or unsupported field configurations
2479 */
2580export const generateUISchema = ( schema : V2Schema ) : JSONFormsUISchema => {
81+ // Validate schema structure first
82+ validateV2Schema ( schema ) ;
83+
2684 // Get all visible (non-deprecated) fields
2785 const visibleFields = getVisibleFields ( schema ) ;
2886
0 commit comments