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

fix the manual add datapoint dialog #199

Merged
merged 1 commit into from
Nov 12, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const CreateDatapointsSchema = z.object({
datapoints: z.array(z.object({
data: z.unknown(),
target: z.any().optional(),
metadata: z.record(z.any()).optional(),
metadata: z.any().optional(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using z.any() for metadata is too permissive. Consider using a more specific schema to ensure data integrity.

Suggested change
metadata: z.any().optional(),
metadata: z.object().optional(),

})),
sourceSpanId: z.string().optional(),
});
Expand All @@ -49,7 +49,7 @@ export async function POST(
const body = await req.json();

// Validate request body
const parseResult = CreateDatapointsSchema.required().safeParse(body);
const parseResult = CreateDatapointsSchema.safeParse(body);
if (!parseResult.success) {
return new Response(
JSON.stringify({
Expand Down
20 changes: 17 additions & 3 deletions frontend/components/dataset/manual-add-datapoint-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ export default function ManualAddDatapointDialog({
const [isLoading, setIsLoading] = useState(false);
const [data, setData] = useState(DEFAULT_DATA);

const isValidJson = useCallback(() => {
dinmukhamedm marked this conversation as resolved.
Show resolved Hide resolved
try {
const parsed = JSON.parse(data);
return parsed.data;
} catch (e) {
return false;
}
}, [data]);

const showError = useCallback((message: string) => {
toast({
title: 'Add datapoint error',
Expand All @@ -49,14 +58,14 @@ export default function ManualAddDatapointDialog({
{
method: 'POST',
body: JSON.stringify({
datapoints: [JSON.parse(data)]
datapoints: [JSON.parse(data)],
}),
cache: 'no-cache'
}
);

if (res.status != 200) {
showError('Error adding datapoint');
showError((await res.json())['details']);
setIsLoading(false);
return;
}
Expand Down Expand Up @@ -94,9 +103,14 @@ export default function ManualAddDatapointDialog({
<div className="border rounded-md max-h-[300px] overflow-y-auto">
<CodeEditor value={data} onChange={setData} language="json" />
</div>
{!isValidJson() && (
<div className="text-red-500">
Please enter a valid JSON map with a &dquote;data&dquote; field
</div>
)}
<DialogFooter className="mt-4">
<Button
disabled={isLoading}
disabled={isLoading || !isValidJson()}
onClick={async () => await addDatapoint()}
>
{isLoading && <Loader2 className="animate-spin h-4 w-4 mr-2" />}
Expand Down