Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 82 additions & 43 deletions packages/contentstack-audit/src/modules/content-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,20 @@ export default class ContentType {
this.gfSchema = gfSchema;
this.moduleName = this.validateModules(moduleName!, this.config.moduleConfig);
this.fileName = config.moduleConfig[this.moduleName].fileName;
this.folderPath = resolve(sanitizePath(config.basePath), sanitizePath(config.moduleConfig[this.moduleName].dirName));
this.folderPath = resolve(
sanitizePath(config.basePath),
sanitizePath(config.moduleConfig[this.moduleName].dirName),
);
}

validateModules(moduleName: keyof typeof auditConfig.moduleConfig, moduleConfig: Record<string, unknown>): keyof typeof auditConfig.moduleConfig {
validateModules(
moduleName: keyof typeof auditConfig.moduleConfig,
moduleConfig: Record<string, unknown>,
): keyof typeof auditConfig.moduleConfig {
if (Object.keys(moduleConfig).includes(moduleName)) {
return moduleName;
}
return 'content-types'
return 'content-types';
}
/**
* The `run` function checks if a folder path exists, sets the schema based on the module name,
Expand Down Expand Up @@ -121,7 +127,7 @@ export default class ContentType {
if (existsSync(extensionPath)) {
try {
this.extensions = Object.keys(JSON.parse(readFileSync(extensionPath, 'utf8')));
} catch (error) { }
} catch (error) {}
}

if (existsSync(marketplacePath)) {
Expand All @@ -134,7 +140,7 @@ export default class ContentType {
) as string[];
this.extensions.push(...metaData);
}
} catch (error) { }
} catch (error) {}
}
}

Expand Down Expand Up @@ -270,19 +276,19 @@ export default class ContentType {

return missingRefs.length
? [
{
tree,
data_type,
missingRefs,
display_name,
ct_uid: this.currentUid,
name: this.currentTitle,
treeStr: tree
.map(({ name }) => name)
.filter((val) => val)
.join(' ➜ '),
},
]
{
tree,
data_type,
missingRefs,
display_name,
ct_uid: this.currentUid,
name: this.currentTitle,
treeStr: tree
.map(({ name }) => name)
.filter((val) => val)
.join(' ➜ '),
},
]
: [];
}

Expand All @@ -297,18 +303,35 @@ export default class ContentType {
*/
async validateGlobalField(tree: Record<string, unknown>[], field: GlobalFieldDataType): Promise<void> {
// NOTE Any GlobalField related logic can be added here
if (!field.schema && !this.fix) {
this.missingRefs[this.currentUid].push({
tree,
ct_uid: this.currentUid,
name: this.currentTitle,
data_type: field.data_type,
display_name: field.display_name,
missingRefs: 'Empty schema found',
treeStr: tree.map(({ name }) => name).join(' ➜ '),
});
if (this.moduleName === 'global-fields') {
let { reference_to } = field;
const refExist = find(this.schema, { uid: reference_to });
if (!refExist) {
this.missingRefs[this.currentUid].push({
tree,
ct: this.currentUid,
name: this.currentTitle,
data_type: field.data_type,
display_name: field.display_name,
missingRefs: 'Referred Global Field Does not Exist',
treeStr: tree.map(({ name }) => name).join(' ➜ '),
});
return void 0;
}
} else if (this.moduleName === 'content-types') {
if (!field.schema && !this.fix) {
this.missingRefs[this.currentUid].push({
tree,
ct_uid: this.currentUid,
name: this.currentTitle,
data_type: field.data_type,
display_name: field.display_name,
missingRefs: 'Empty schema found',
treeStr: tree.map(({ name }) => name).join(' ➜ '),
});

return void 0;
return void 0;
}
}

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

return missingRefs.length
? [
{
tree,
data_type,
missingRefs,
display_name,
ct_uid: this.currentUid,
name: this.currentTitle,
treeStr: tree
.map(({ name }) => name)
.filter((val) => val)
.join(' ➜ '),
},
]
{
tree,
data_type,
missingRefs,
display_name,
ct_uid: this.currentUid,
name: this.currentTitle,
treeStr: tree
.map(({ name }) => name)
.filter((val) => val)
.join(' ➜ '),
},
]
: [];
}

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

if (gfSchema) {
Expand All @@ -520,6 +543,22 @@ export default class ContentType {
treeStr: tree.map(({ name }) => name).join(' ➜ '),
});
}
} else if (!field.schema && this.moduleName === 'global-fields') {
const gfSchema = find(this.gfSchema, { uid: field.reference_to })?.schema;
if (gfSchema) {
field.schema = gfSchema as GlobalFieldSchemaTypes[];

this.missingRefs[this.currentUid].push({
tree,
data_type,
display_name,
fixStatus: 'Fixed',
ct_uid: this.currentUid,
name: this.currentTitle,
missingRefs: 'Referred Global Field Does not exist',
treeStr: tree.map(({ name }) => name).join(' ➜ '),
});
}
}

return refExist ? field : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,192 @@
}
],
"description": ""
},
{
"title": "Nested Global Field 2",
"uid": "nested_global_field_2",
"schema": [
{
"data_type": "text",
"display_name": "Single Line Textbox",
"uid": "single_line",
"mandatory": false,
"non_localizable": false,
"multiple": false,
"unique": false
},
{
"data_type": "global_field",
"display_name": "Global",
"uid": "global_field",
"reference_to": "nested_global_field_1",
"mandatory": false,
"non_localizable": false,
"multiple": false,
"unique": false
}
],
"description": ""
},
{
"title": "Nested Global Field 3",
"uid": "nested_global_field_3",
"schema": [
{
"data_type": "text",
"display_name": "Single Line Textbox",
"uid": "single_line",
"mandatory": false,
"non_localizable": false,
"multiple": false,
"unique": false
},
{
"data_type": "global_field",
"display_name": "Global",
"uid": "global_field",
"reference_to": "nested_global_field_2",
"mandatory": false,
"non_localizable": false,
"multiple": false,
"unique": false
}
],
"description": ""
},
{
"title": "Nested Global Field 4",
"uid": "nested_global_field_4",
"schema": [
{
"data_type": "text",
"display_name": "Single Line Textbox",
"uid": "single_line",
"mandatory": false,
"non_localizable": false,
"multiple": false,
"unique": false
},
{
"data_type": "global_field",
"display_name": "Global",
"uid": "global_field",
"reference_to": "nested_global_field_2",
"mandatory": false,
"non_localizable": false,
"multiple": false,
"unique": false
}
],
"description": ""
},
{
"title": "Nested Global Fields 999",
"uid": "nested_global_field999",
"schema": [
{
"data_type": "text",
"display_name": "Single Line Textbox",
"uid": "single_line",
"field_metadata": { "description": "", "default_value": "", "version": 3 },
"format": "",
"error_messages": { "format": "" },
"mandatory": false,
"multiple": false,
"non_localizable": false,
"unique": false
},
{
"data_type": "global_field",
"display_name": "Global",
"reference_to": "nested_global_field_2",
"field_metadata": { "description": "" },
"uid": "global_field",
"mandatory": false,
"multiple": false,
"non_localizable": false,
"unique": false
}
],
"description": ""
},
{
"title": "Nested Global Fields eight",
"uid": "nested_global_field888",
"schema": [
{
"data_type": "text",
"display_name": "Single Line Textbox",
"uid": "single_line",
"field_metadata": { "description": "", "default_value": "", "version": 3 },
"format": "",
"error_messages": { "format": "" },
"mandatory": false,
"multiple": false,
"non_localizable": false,
"unique": false
},
{
"data_type": "global_field",
"display_name": "Global",
"reference_to": "nested_global_field_2",
"field_metadata": { "description": "" },
"uid": "global_field",
"mandatory": false,
"multiple": false,
"non_localizable": false,
"unique": false
}
],
"description": ""
},
{
"title": "sample_global_field_1",
"uid": "sample_global_field_1",
"schema": [
{
"data_type": "text",
"display_name": "Single Line Textbox",
"uid": "single_line",
"field_metadata": { "description": "", "default_value": "" },
"format": "",
"error_messages": { "format": "" },
"mandatory": false,
"multiple": false,
"non_localizable": false,
"unique": false
}
],
"description": ""
},
{
"title": "global_field_sample_2",
"uid": "global_field_sample_2",
"schema": [
{
"data_type": "group",
"display_name": "Group",
"field_metadata": { "description": "", "instruction": "" },
"schema": [
{
"data_type": "global_field",
"display_name": "Global",
"reference_to": "nested_global_field_1",
"field_metadata": { "description": "" },
"uid": "global_field",
"mandatory": false,
"multiple": false,
"non_localizable": false,
"unique": false
}
],
"uid": "group",
"mandatory": false,
"multiple": false,
"non_localizable": false,
"unique": false
}
],
"description": ""
}
]
Loading
Loading