Skip to content

Commit d75ce43

Browse files
authored
Merge pull request #1736 from contentstack/feat/DX-1883
Feat: Added audit and audit fix for nested global field in CT
2 parents c83c810 + 0c68537 commit d75ce43

File tree

3 files changed

+323
-43
lines changed

3 files changed

+323
-43
lines changed

packages/contentstack-audit/src/modules/content-types.ts

Lines changed: 82 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,20 @@ export default class ContentType {
5353
this.gfSchema = gfSchema;
5454
this.moduleName = this.validateModules(moduleName!, this.config.moduleConfig);
5555
this.fileName = config.moduleConfig[this.moduleName].fileName;
56-
this.folderPath = resolve(sanitizePath(config.basePath), sanitizePath(config.moduleConfig[this.moduleName].dirName));
56+
this.folderPath = resolve(
57+
sanitizePath(config.basePath),
58+
sanitizePath(config.moduleConfig[this.moduleName].dirName),
59+
);
5760
}
5861

59-
validateModules(moduleName: keyof typeof auditConfig.moduleConfig, moduleConfig: Record<string, unknown>): keyof typeof auditConfig.moduleConfig {
62+
validateModules(
63+
moduleName: keyof typeof auditConfig.moduleConfig,
64+
moduleConfig: Record<string, unknown>,
65+
): keyof typeof auditConfig.moduleConfig {
6066
if (Object.keys(moduleConfig).includes(moduleName)) {
6167
return moduleName;
6268
}
63-
return 'content-types'
69+
return 'content-types';
6470
}
6571
/**
6672
* The `run` function checks if a folder path exists, sets the schema based on the module name,
@@ -121,7 +127,7 @@ export default class ContentType {
121127
if (existsSync(extensionPath)) {
122128
try {
123129
this.extensions = Object.keys(JSON.parse(readFileSync(extensionPath, 'utf8')));
124-
} catch (error) { }
130+
} catch (error) {}
125131
}
126132

127133
if (existsSync(marketplacePath)) {
@@ -134,7 +140,7 @@ export default class ContentType {
134140
) as string[];
135141
this.extensions.push(...metaData);
136142
}
137-
} catch (error) { }
143+
} catch (error) {}
138144
}
139145
}
140146

@@ -270,19 +276,19 @@ export default class ContentType {
270276

271277
return missingRefs.length
272278
? [
273-
{
274-
tree,
275-
data_type,
276-
missingRefs,
277-
display_name,
278-
ct_uid: this.currentUid,
279-
name: this.currentTitle,
280-
treeStr: tree
281-
.map(({ name }) => name)
282-
.filter((val) => val)
283-
.join(' ➜ '),
284-
},
285-
]
279+
{
280+
tree,
281+
data_type,
282+
missingRefs,
283+
display_name,
284+
ct_uid: this.currentUid,
285+
name: this.currentTitle,
286+
treeStr: tree
287+
.map(({ name }) => name)
288+
.filter((val) => val)
289+
.join(' ➜ '),
290+
},
291+
]
286292
: [];
287293
}
288294

@@ -297,18 +303,35 @@ export default class ContentType {
297303
*/
298304
async validateGlobalField(tree: Record<string, unknown>[], field: GlobalFieldDataType): Promise<void> {
299305
// NOTE Any GlobalField related logic can be added here
300-
if (!field.schema && !this.fix) {
301-
this.missingRefs[this.currentUid].push({
302-
tree,
303-
ct_uid: this.currentUid,
304-
name: this.currentTitle,
305-
data_type: field.data_type,
306-
display_name: field.display_name,
307-
missingRefs: 'Empty schema found',
308-
treeStr: tree.map(({ name }) => name).join(' ➜ '),
309-
});
306+
if (this.moduleName === 'global-fields') {
307+
let { reference_to } = field;
308+
const refExist = find(this.schema, { uid: reference_to });
309+
if (!refExist) {
310+
this.missingRefs[this.currentUid].push({
311+
tree,
312+
ct: this.currentUid,
313+
name: this.currentTitle,
314+
data_type: field.data_type,
315+
display_name: field.display_name,
316+
missingRefs: 'Referred Global Field Does not Exist',
317+
treeStr: tree.map(({ name }) => name).join(' ➜ '),
318+
});
319+
return void 0;
320+
}
321+
} else if (this.moduleName === 'content-types') {
322+
if (!field.schema && !this.fix) {
323+
this.missingRefs[this.currentUid].push({
324+
tree,
325+
ct_uid: this.currentUid,
326+
name: this.currentTitle,
327+
data_type: field.data_type,
328+
display_name: field.display_name,
329+
missingRefs: 'Empty schema found',
330+
treeStr: tree.map(({ name }) => name).join(' ➜ '),
331+
});
310332

311-
return void 0;
333+
return void 0;
334+
}
312335
}
313336

314337
await this.lookForReference(tree, field);
@@ -398,19 +421,19 @@ export default class ContentType {
398421

399422
return missingRefs.length
400423
? [
401-
{
402-
tree,
403-
data_type,
404-
missingRefs,
405-
display_name,
406-
ct_uid: this.currentUid,
407-
name: this.currentTitle,
408-
treeStr: tree
409-
.map(({ name }) => name)
410-
.filter((val) => val)
411-
.join(' ➜ '),
412-
},
413-
]
424+
{
425+
tree,
426+
data_type,
427+
missingRefs,
428+
display_name,
429+
ct_uid: this.currentUid,
430+
name: this.currentTitle,
431+
treeStr: tree
432+
.map(({ name }) => name)
433+
.filter((val) => val)
434+
.join(' ➜ '),
435+
},
436+
]
414437
: [];
415438
}
416439

@@ -503,7 +526,7 @@ export default class ContentType {
503526
missingRefs: [reference_to],
504527
treeStr: tree.map(({ name }) => name).join(' ➜ '),
505528
});
506-
} else if (!field.schema) {
529+
} else if (!field.schema && this.moduleName === 'content-types') {
507530
const gfSchema = find(this.gfSchema, { uid: field.reference_to })?.schema;
508531

509532
if (gfSchema) {
@@ -520,6 +543,22 @@ export default class ContentType {
520543
treeStr: tree.map(({ name }) => name).join(' ➜ '),
521544
});
522545
}
546+
} else if (!field.schema && this.moduleName === 'global-fields') {
547+
const gfSchema = find(this.gfSchema, { uid: field.reference_to })?.schema;
548+
if (gfSchema) {
549+
field.schema = gfSchema as GlobalFieldSchemaTypes[];
550+
551+
this.missingRefs[this.currentUid].push({
552+
tree,
553+
data_type,
554+
display_name,
555+
fixStatus: 'Fixed',
556+
ct_uid: this.currentUid,
557+
name: this.currentTitle,
558+
missingRefs: 'Referred Global Field Does not exist',
559+
treeStr: tree.map(({ name }) => name).join(' ➜ '),
560+
});
561+
}
523562
}
524563

525564
return refExist ? field : null;

packages/contentstack-audit/test/unit/mock/contents/global_fields/globalfields.json

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,192 @@
131131
}
132132
],
133133
"description": ""
134+
},
135+
{
136+
"title": "Nested Global Field 2",
137+
"uid": "nested_global_field_2",
138+
"schema": [
139+
{
140+
"data_type": "text",
141+
"display_name": "Single Line Textbox",
142+
"uid": "single_line",
143+
"mandatory": false,
144+
"non_localizable": false,
145+
"multiple": false,
146+
"unique": false
147+
},
148+
{
149+
"data_type": "global_field",
150+
"display_name": "Global",
151+
"uid": "global_field",
152+
"reference_to": "nested_global_field_1",
153+
"mandatory": false,
154+
"non_localizable": false,
155+
"multiple": false,
156+
"unique": false
157+
}
158+
],
159+
"description": ""
160+
},
161+
{
162+
"title": "Nested Global Field 3",
163+
"uid": "nested_global_field_3",
164+
"schema": [
165+
{
166+
"data_type": "text",
167+
"display_name": "Single Line Textbox",
168+
"uid": "single_line",
169+
"mandatory": false,
170+
"non_localizable": false,
171+
"multiple": false,
172+
"unique": false
173+
},
174+
{
175+
"data_type": "global_field",
176+
"display_name": "Global",
177+
"uid": "global_field",
178+
"reference_to": "nested_global_field_2",
179+
"mandatory": false,
180+
"non_localizable": false,
181+
"multiple": false,
182+
"unique": false
183+
}
184+
],
185+
"description": ""
186+
},
187+
{
188+
"title": "Nested Global Field 4",
189+
"uid": "nested_global_field_4",
190+
"schema": [
191+
{
192+
"data_type": "text",
193+
"display_name": "Single Line Textbox",
194+
"uid": "single_line",
195+
"mandatory": false,
196+
"non_localizable": false,
197+
"multiple": false,
198+
"unique": false
199+
},
200+
{
201+
"data_type": "global_field",
202+
"display_name": "Global",
203+
"uid": "global_field",
204+
"reference_to": "nested_global_field_2",
205+
"mandatory": false,
206+
"non_localizable": false,
207+
"multiple": false,
208+
"unique": false
209+
}
210+
],
211+
"description": ""
212+
},
213+
{
214+
"title": "Nested Global Fields 999",
215+
"uid": "nested_global_field999",
216+
"schema": [
217+
{
218+
"data_type": "text",
219+
"display_name": "Single Line Textbox",
220+
"uid": "single_line",
221+
"field_metadata": { "description": "", "default_value": "", "version": 3 },
222+
"format": "",
223+
"error_messages": { "format": "" },
224+
"mandatory": false,
225+
"multiple": false,
226+
"non_localizable": false,
227+
"unique": false
228+
},
229+
{
230+
"data_type": "global_field",
231+
"display_name": "Global",
232+
"reference_to": "nested_global_field_2",
233+
"field_metadata": { "description": "" },
234+
"uid": "global_field",
235+
"mandatory": false,
236+
"multiple": false,
237+
"non_localizable": false,
238+
"unique": false
239+
}
240+
],
241+
"description": ""
242+
},
243+
{
244+
"title": "Nested Global Fields eight",
245+
"uid": "nested_global_field888",
246+
"schema": [
247+
{
248+
"data_type": "text",
249+
"display_name": "Single Line Textbox",
250+
"uid": "single_line",
251+
"field_metadata": { "description": "", "default_value": "", "version": 3 },
252+
"format": "",
253+
"error_messages": { "format": "" },
254+
"mandatory": false,
255+
"multiple": false,
256+
"non_localizable": false,
257+
"unique": false
258+
},
259+
{
260+
"data_type": "global_field",
261+
"display_name": "Global",
262+
"reference_to": "nested_global_field_2",
263+
"field_metadata": { "description": "" },
264+
"uid": "global_field",
265+
"mandatory": false,
266+
"multiple": false,
267+
"non_localizable": false,
268+
"unique": false
269+
}
270+
],
271+
"description": ""
272+
},
273+
{
274+
"title": "sample_global_field_1",
275+
"uid": "sample_global_field_1",
276+
"schema": [
277+
{
278+
"data_type": "text",
279+
"display_name": "Single Line Textbox",
280+
"uid": "single_line",
281+
"field_metadata": { "description": "", "default_value": "" },
282+
"format": "",
283+
"error_messages": { "format": "" },
284+
"mandatory": false,
285+
"multiple": false,
286+
"non_localizable": false,
287+
"unique": false
288+
}
289+
],
290+
"description": ""
291+
},
292+
{
293+
"title": "global_field_sample_2",
294+
"uid": "global_field_sample_2",
295+
"schema": [
296+
{
297+
"data_type": "group",
298+
"display_name": "Group",
299+
"field_metadata": { "description": "", "instruction": "" },
300+
"schema": [
301+
{
302+
"data_type": "global_field",
303+
"display_name": "Global",
304+
"reference_to": "nested_global_field_1",
305+
"field_metadata": { "description": "" },
306+
"uid": "global_field",
307+
"mandatory": false,
308+
"multiple": false,
309+
"non_localizable": false,
310+
"unique": false
311+
}
312+
],
313+
"uid": "group",
314+
"mandatory": false,
315+
"multiple": false,
316+
"non_localizable": false,
317+
"unique": false
318+
}
319+
],
320+
"description": ""
134321
}
135322
]

0 commit comments

Comments
 (0)