mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2026-08-24 15:22:20 +10:00
Filter invalid style intents to preserve valid custom styles (#3241)
* Filter invalid style intents to preserve valid custom styles * fix: preserve valid custom style rules --------- Co-authored-by: Amruth Pillai <im.amruth@gmail.com>
This commit is contained in:
co-authored by
Amruth Pillai
parent
9085a199cf
commit
689e7e24d4
@@ -569,7 +569,47 @@ export const styleRuleSchema = z.strictObject({
|
||||
slots: styleRuleSlotsSchema.describe("The semantic style slots configured by this rule."),
|
||||
});
|
||||
|
||||
export const styleRulesSchema = z.array(styleRuleSchema).catch([]);
|
||||
const filterStyleIntent = (intent: unknown): StyleIntent | undefined => {
|
||||
const styleIntentShape = styleIntentSchema.shape;
|
||||
if (typeof intent !== "object" || intent === null) return undefined;
|
||||
const filteredIntent = Object.entries(intent).filter(([key, value]) => {
|
||||
const fieldSchema = styleIntentSchema.shape[key as keyof typeof styleIntentShape];
|
||||
if (!fieldSchema) return false;
|
||||
return fieldSchema.safeParse(value).success;
|
||||
});
|
||||
return filteredIntent.length > 0 ? (Object.fromEntries(filteredIntent) as StyleIntent) : undefined;
|
||||
};
|
||||
|
||||
export const styleRulesSchema = z
|
||||
.array(z.unknown())
|
||||
.transform((arr) =>
|
||||
arr
|
||||
.map((item) => {
|
||||
const base = z
|
||||
.strictObject({
|
||||
id: z.string().min(1),
|
||||
label: z.string().catch(""),
|
||||
enabled: z.boolean().catch(true),
|
||||
target: styleRuleTargetSchema,
|
||||
slots: z.partialRecord(styleSlotSchema, z.unknown()),
|
||||
})
|
||||
.safeParse(item);
|
||||
|
||||
if (!base.success) return undefined;
|
||||
|
||||
const cleanedSlots = Object.fromEntries(
|
||||
Object.entries(base.data.slots)
|
||||
.map(([slot, intent]) => [slot, filterStyleIntent(intent)])
|
||||
.filter((entry): entry is [string, StyleIntent] => entry[1] !== undefined),
|
||||
);
|
||||
|
||||
if (Object.keys(cleanedSlots).length === 0) return undefined;
|
||||
|
||||
return { ...base.data, slots: cleanedSlots };
|
||||
})
|
||||
.filter((rule): rule is StyleRule => rule !== undefined),
|
||||
)
|
||||
.catch([]);
|
||||
|
||||
export type StyleRule = z.infer<typeof styleRuleSchema>;
|
||||
export type StyleRuleTarget = z.infer<typeof styleRuleTargetSchema>;
|
||||
|
||||
Reference in New Issue
Block a user