Skip to content

Commit 2f9e5a7

Browse files
committed
Field Rule update
1 parent 3f47f78 commit 2f9e5a7

1 file changed

Lines changed: 66 additions & 68 deletions

File tree

packages/contentstack-audit/src/modules/field_rules.ts

Lines changed: 66 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -165,75 +165,73 @@ export default class FieldRule {
165165
}
166166

167167
fixFieldRules(schema: Record<string, unknown>): void {
168-
if (Array.isArray(schema.field_rules)) {
169-
let count = 0;
170-
schema.field_rules = schema.field_rules
171-
.map((fr: FieldRuleStruct) => {
172-
fr.actions = fr.actions
173-
?.map((actions: { action: string; target_field: string }) => {
174-
if (!this.schemaMap.includes(actions.target_field)) {
175-
this.log(
176-
$t(auditMsg.FIELD_RULE_TARGET_ABSENT, {
177-
target_field: actions.target_field,
178-
ctUid: schema.uid as string,
179-
}),
180-
'error',
181-
);
182-
183-
this.addMissingReferences(actions, 'Fixed');
184-
this.log(
185-
$t(auditFixMsg.FIELD_RULE_FIX_MESSAGE, { num: count.toString(), ctUid: schema.uid as string }),
186-
'info',
187-
);
188-
this.log(
189-
$t(auditMsg.FIELD_RULE_TARGET_SCAN_MESSAGE, { num: count.toString(), ctUid: schema.uid as string }),
190-
'info',
191-
);
192-
return null;
193-
} else {
194-
this.log(
195-
$t(auditMsg.FIELD_RULE_TARGET_SCAN_MESSAGE, { num: count.toString(), ctUid: schema.uid as string }),
196-
'info',
197-
);
198-
return actions;
199-
}
200-
})
201-
.filter((v): v is { action: string; target_field: string } => v !== undefined);
202-
203-
fr.conditions = fr.conditions
204-
?.map((actions: { operand_field: any }) => {
205-
if (!this.schemaMap.includes(actions.operand_field)) {
206-
this.log($t(auditMsg.FIELD_RULE_CONDITION_ABSENT, { condition_field: actions.operand_field }), 'error');
207-
this.addMissingReferences(actions, 'Fixed');
208-
this.log(
209-
$t(auditFixMsg.FIELD_RULE_FIX_MESSAGE, { num: count.toString(), ctUid: schema.uid as string }),
210-
'info',
211-
);
212-
this.log(
213-
$t(auditMsg.FIELD_RULE_CONDITION_SCAN_MESSAGE, { num: count.toString(), ctUid: schema.uid as string }),
214-
'info',
215-
);
216-
return;
217-
} else {
218-
this.log(
219-
$t(auditMsg.FIELD_RULE_CONDITION_SCAN_MESSAGE, {
220-
num: count.toString(),
221-
ctUid: schema.uid as string,
222-
}),
223-
'info',
224-
);
225-
return actions;
226-
}
227-
})
228-
.filter((v): v is { value: string; operand_field: string; operator: string } => v !== undefined);
229-
230-
count = count + 1;
231-
if (fr.actions?.length && fr.conditions?.length) {
232-
return fr;
168+
if (!Array.isArray(schema.field_rules)) return;
169+
170+
schema.field_rules = schema.field_rules
171+
.map((fr: FieldRuleStruct, index: number) => {
172+
const validActions = fr.actions?.filter(action => {
173+
const isValid = this.schemaMap.includes(action.target_field);
174+
const logMsg = isValid
175+
? auditMsg.FIELD_RULE_TARGET_SCAN_MESSAGE
176+
: auditMsg.FIELD_RULE_TARGET_ABSENT;
177+
178+
this.log(
179+
$t(logMsg, {
180+
num: index.toString(),
181+
ctUid: schema.uid as string,
182+
...(action.target_field && { target_field: action.target_field })
183+
}),
184+
isValid ? 'info' : 'error'
185+
);
186+
187+
if (!isValid) {
188+
this.addMissingReferences(action, 'Fixed');
189+
this.log(
190+
$t(auditFixMsg.FIELD_RULE_FIX_MESSAGE, {
191+
num: index.toString(),
192+
ctUid: schema.uid as string
193+
}),
194+
'info'
195+
);
233196
}
234-
})
235-
.filter((v: any) => v);
236-
}
197+
return isValid;
198+
}) ?? [];
199+
200+
const validConditions = fr.conditions?.filter(condition => {
201+
const isValid = this.schemaMap.includes(condition.operand_field);
202+
const logMsg = isValid
203+
? auditMsg.FIELD_RULE_CONDITION_SCAN_MESSAGE
204+
: auditMsg.FIELD_RULE_CONDITION_ABSENT;
205+
206+
this.log(
207+
$t(logMsg, {
208+
num: index.toString(),
209+
ctUid: schema.uid as string,
210+
...(condition.operand_field && { condition_field: condition.operand_field })
211+
}),
212+
isValid ? 'info' : 'error'
213+
);
214+
215+
if (!isValid) {
216+
this.addMissingReferences(condition, 'Fixed');
217+
this.log(
218+
$t(auditFixMsg.FIELD_RULE_FIX_MESSAGE, {
219+
num: index.toString(),
220+
ctUid: schema.uid as string
221+
}),
222+
'info'
223+
);
224+
}
225+
return isValid;
226+
}) ?? [];
227+
228+
return (validActions.length && validConditions.length) ? {
229+
...fr,
230+
actions: validActions,
231+
conditions: validConditions
232+
} : null;
233+
})
234+
.filter(Boolean);
237235
}
238236

239237

0 commit comments

Comments
 (0)