Skip to content

feat: add robust source form validation and error reporting #923

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .changeset/dry-ducks-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@hyperdx/common-utils": patch
"@hyperdx/app": patch
---

feat: add robust source form validation and error reporting
5 changes: 3 additions & 2 deletions packages/api/src/routers/api/__tests__/sources.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { SourceKind } from '@hyperdx/common-utils/dist/types';
import { SourceKind, TSourceUnion } from '@hyperdx/common-utils/dist/types';
import { Types } from 'mongoose';

import { getLoggedInAgent, getServer } from '@/fixtures';
import { Source } from '@/models/source';

const MOCK_SOURCE = {
const MOCK_SOURCE: Omit<Extract<TSourceUnion, { kind: 'log' }>, 'id'> = {
kind: SourceKind.Log,
name: 'Test Source',
connection: new Types.ObjectId().toString(),
Expand All @@ -13,6 +13,7 @@ const MOCK_SOURCE = {
tableName: 'test_table',
},
timestampValueExpression: 'timestamp',
defaultTableSelectExpression: 'body',
};

describe('sources router', () => {
Expand Down
15 changes: 11 additions & 4 deletions packages/api/src/routers/api/sources.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { SourceSchema } from '@hyperdx/common-utils/dist/types';
import {
SourceSchema,
sourceSchemaWithout,
} from '@hyperdx/common-utils/dist/types';
import express from 'express';
import { z } from 'zod';
import { validateRequest } from 'zod-express-middleware';
Expand Down Expand Up @@ -26,19 +29,22 @@ router.get('/', async (req, res, next) => {
}
});

const SourceSchemaNoId = sourceSchemaWithout({ id: true });

router.post(
'/',
validateRequest({
body: SourceSchema.omit({ id: true }),
body: SourceSchemaNoId,
}),
async (req, res, next) => {
try {
const { teamId } = getNonNullUserWithTeam(req);

// TODO: HDX-1768 Eliminate type assertion
const source = await createSource(teamId.toString(), {
...req.body,
team: teamId,
});
} as any);
Comment on lines +43 to +47
Copy link
Contributor Author

@knudtty knudtty Jun 10, 2025

Choose a reason for hiding this comment

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

Because TSource and the new TSourceUnion are both used in some areas, some type assertions are needed to make things compile right now. I tagged all type assertions with a TODO comment and the linear issue where they were added.

I plan to follow up with a Part 2 PR to switch all instances of TSource to TSourceUnion on the frontend, or just eliminate TSource entirely. This should eliminate nearly all type assertions


res.json(source);
} catch (e) {
Expand All @@ -59,10 +65,11 @@ router.put(
try {
const { teamId } = getNonNullUserWithTeam(req);

// TODO: HDX-1768 Eliminate type assertion
const source = await updateSource(teamId.toString(), req.params.id, {
...req.body,
team: teamId,
});
} as any);

if (!source) {
res.status(404).send('Source not found');
Expand Down
7 changes: 5 additions & 2 deletions packages/app/src/components/SQLInlineEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ type SQLInlineEditorProps = {
onLanguageChange?: (language: 'sql' | 'lucene') => void;
language?: 'sql' | 'lucene';
onSubmit?: () => void;
error?: React.ReactNode;
size?: string;
label?: React.ReactNode;
disableKeywordAutocomplete?: boolean;
Expand Down Expand Up @@ -134,6 +135,7 @@ export default function SQLInlineEditor({
onLanguageChange,
language,
onSubmit,
error,
value,
size,
label,
Expand Down Expand Up @@ -260,7 +262,7 @@ export default function SQLInlineEditor({
shadow="none"
bg="dark.6"
style={{
border: '1px solid var(--mantine-color-gray-7)',
border: `1px solid ${error ? 'var(--mantine-color-red-7)' : 'var(--mantine-color-gray-7)'}`,
display: 'flex',
alignItems: 'center',
minHeight: size === 'xs' ? 30 : 36,
Expand Down Expand Up @@ -357,7 +359,7 @@ export function SQLInlineEditorControlled({
queryHistoryType,
...props
}: Omit<SQLInlineEditorProps, 'value' | 'onChange'> & UseControllerProps<any>) {
const { field } = useController(props);
const { field, fieldState } = useController(props);

// Guard against wrongly typed values
const value = field.value || props.defaultValue;
Expand All @@ -375,6 +377,7 @@ export function SQLInlineEditorControlled({
onChange={field.onChange}
placeholder={placeholder}
value={stringValue}
error={fieldState.error?.message}
additionalSuggestions={additionalSuggestions}
queryHistoryType={queryHistoryType}
{...props}
Expand Down
Loading