-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: validate JSON object in form submission #4171
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -179,11 +179,9 @@ function open(id: string) { | |
| folderId.value = id | ||
| filterList.value = null | ||
| dialogVisible.value = true | ||
| } | ||
|
|
||
| onBeforeMount(() => { | ||
| getList() | ||
| }) | ||
| } | ||
|
|
||
| async function getList() { | ||
| if (toolType.value === 'INTERNAL') { | ||
|
|
@@ -224,11 +222,6 @@ async function getStoreToolList() { | |
| tool.desc = tool.description | ||
| }) | ||
|
|
||
| if (storeTools.length === 0) { | ||
| filterList.value = [] | ||
| return | ||
| } | ||
|
|
||
| categories.value = tags.map((tag: any) => ({ | ||
| id: tag.key, | ||
| title: tag.name, // 国际化 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code seems to have some minor formatting issues, particularly with trailing commas not being present after arrays and objects where they should be used. Additionally, there is an unnecessary Here are my recommendations:
Here’s the updated version of your code: function open(id: string) {
folderId.value = id;
filterList.value = null;
dialogVisible.value = true;
}
async function getStoreToolList() {
const storeToolsData = await fetch('https://api.example.com/stores/' + id);
const data = await storeToolsData.json();
let tags = [] as { key: number; name: string }[];
// Fetch Tags from backend service
await axios.get<any[]>('/api/tags').then(
(res: any[]) =>
res.forEach(tag => {
tags.push({
id: tag.key,
title: tag.name, // 国际化
});
})
);
const storeTools = data.storeTools.map(tool => ({
...tool,
desc: tool.description, // Assuming descriptions are camelCase and need conversion
}));
if (storeTools.length > 0) {
categories.value = tags.slice(0); // Use slice instead of assign to avoid mutating original array
}
if (!storeTools.length || !categories.length) return;
handleSelectedCategory(categories[0]);
try {
await setFolder(storeTools.filter(o => o.category === selectedCategoryId).find(o => o.selected === false));
selectedCategoriesIds.clear();
listDialogRef.value = null!;
} catch (error) {}
router.back();
};
const storeTools = ref<Tool[]>([])In this corrected code, I've added type information using TypeScript. The main changes include:
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here’s a review of your code:
The changes made are mostly improvements to handle invalid JSON input more effectively. Your original attempt to use
JSON.parsemight still fail silently if the user inputs malformed data. By catching this exception within anif (!parsed)block, you ensure that any error is explicitly logged with MessageError.Suggested Improvement: Add a log message for debugging purposes if the JSON parsing fails. This will help in identifying why the content doesn't validate as expected during testing or maintenance.
This approach provides clarity on what went wrong during JSON parsing and prevents silent failures.