Skip to content
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
22 changes: 22 additions & 0 deletions src/protocol/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { z } from 'zod';

import type { ProtocolErrorCode } from './errors.js';

import { ERROR_CODES, makeCliError } from './errors.js';

export function parseValidatedResult<TSchema extends z.ZodType>(
schema: TSchema,
rawValue: unknown,
message: string,
errorCode: ProtocolErrorCode = ERROR_CODES.PROTOCOL_ERROR,
): z.infer<TSchema> {
const parsedResult = schema.safeParse(rawValue);
if (!parsedResult.success) {
throw makeCliError(errorCode, {
message,
details: { issues: parsedResult.error.issues },
});
}

return parsedResult.data;
}
12 changes: 2 additions & 10 deletions src/screenshot/capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { ulid } from 'ulid';
import type { ScreenshotResult } from '../protocol/messages.js';
import type { RendererBackend } from '../renderer/backend.js';

import { ERROR_CODES, makeCliError } from '../protocol/errors.js';
import { ScreenshotResultSchema } from '../protocol/schemas.js';
import { parseValidatedResult } from '../protocol/validation.js';
import {
appendArtifact,
createArtifactEntry,
Expand Down Expand Up @@ -46,15 +46,7 @@ export function parseScreenshotResult(
rawResult: unknown,
message = 'Unexpected response from host',
): ScreenshotResult {
const parsedResult = ScreenshotResultSchema.safeParse(rawResult);
if (!parsedResult.success) {
throw makeCliError(ERROR_CODES.PROTOCOL_ERROR, {
message,
details: { issues: parsedResult.error.issues },
});
}

return parsedResult.data;
return parseValidatedResult(ScreenshotResultSchema, rawResult, message);
}

export async function captureScreenshotResult(
Expand Down
11 changes: 2 additions & 9 deletions src/snapshot/capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { SemanticSnapshot } from '../renderer/types.js';

import { ERROR_CODES, makeCliError } from '../protocol/errors.js';
import { SnapshotResultSchema } from '../protocol/schemas.js';
import { parseValidatedResult } from '../protocol/validation.js';
import {
appendArtifact,
createArtifactEntry,
Expand All @@ -21,15 +22,7 @@ export function parseSnapshotResult(
rawResult: unknown,
message = 'Unexpected response from host',
): SnapshotResult {
const parsedResult = SnapshotResultSchema.safeParse(rawResult);
if (!parsedResult.success) {
throw makeCliError(ERROR_CODES.PROTOCOL_ERROR, {
message,
details: { issues: parsedResult.error.issues },
});
}

return parsedResult.data;
return parseValidatedResult(SnapshotResultSchema, rawResult, message);
}

export function createSnapshotResult(
Expand Down
49 changes: 49 additions & 0 deletions test/unit/protocol/validation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { z } from 'zod';

import { describe, expect, it } from 'vitest';

import { ERROR_CODES } from '../../../src/protocol/errors.js';
import { parseValidatedResult } from '../../../src/protocol/validation.js';

describe('parseValidatedResult', () => {
const ResultSchema = z
.object({
id: z.string().min(1),
count: z.number().int().positive(),
})
.strict();

it('returns parsed data on success', () => {
expect(
parseValidatedResult(ResultSchema, { id: 'result-01', count: 2 }, 'bad'),
).toEqual({ id: 'result-01', count: 2 });
});

it('throws PROTOCOL_ERROR with zod issues by default', () => {
expect(() =>
parseValidatedResult(ResultSchema, { id: '', count: 0 }, 'invalid'),
).toThrow(
expect.objectContaining({
code: ERROR_CODES.PROTOCOL_ERROR,
message: 'invalid',
details: { issues: expect.any(Array) as unknown },
}) as object,
);
});

it('uses a supplied error code', () => {
expect(() =>
parseValidatedResult(
ResultSchema,
{},
'invalid input',
ERROR_CODES.INVALID_INPUT,
),
).toThrow(
expect.objectContaining({
code: ERROR_CODES.INVALID_INPUT,
message: 'invalid input',
}) as object,
);
});
});
Loading