Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace JSON.parse with parseJsonWithSchema #5881

Open
fehmer opened this issue Sep 13, 2024 · 2 comments
Open

Replace JSON.parse with parseJsonWithSchema #5881

fehmer opened this issue Sep 13, 2024 · 2 comments
Labels
frontend User interface or web stuff help wanted Extra attention is needed improvement Change or improvement

Comments

@fehmer
Copy link
Member

fehmer commented Sep 13, 2024

Replace JSON.parse with Misc.parseJsonWithSchema for type safety.

Example:

const customThemeUrlDataSchema = z.object({
c: CustomThemeColorsSchema,
i: z.string().optional(),
s: CustomBackgroundSizeSchema.optional(),
f: CustomBackgroundFilterSchema.optional(),
});
export function loadCustomThemeFromUrl(getOverride?: string): void {
const getValue = Misc.findGetParameter("customTheme", getOverride);
if (getValue === null) return;
let decoded: z.infer<typeof customThemeUrlDataSchema>;
try {
decoded = Misc.parseJsonWithSchema(
atob(getValue),
customThemeUrlDataSchema
);
} catch (e) {
console.log("Custom theme URL decoding failed", e);
Notifications.add(
"Failed to load theme from URL: " + (e as Error).message,
0
);
return;
}

@fehmer fehmer added bug Something isn't working improvement Change or improvement help wanted Extra attention is needed frontend User interface or web stuff and removed bug Something isn't working labels Sep 13, 2024
@Miodec
Copy link
Member

Miodec commented Sep 14, 2024

I think before we do this we should extract common util functions (like this one) to a package.

@rajneesh-18
Copy link

It should be go on the discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
frontend User interface or web stuff help wanted Extra attention is needed improvement Change or improvement
Projects
None yet
Development

No branches or pull requests

3 participants