SchemaValidationError.ts
This file
Path
source/errors/SchemaValidationError.ts
33 lines · 998 bytes · TypeScript
Last changed by
Add Standard Schema validation for `.json()` (#830)Sindre Sorhusbefore adoption94741a9
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334import type {StandardSchemaV1Issue} from '../types/standard-schema.js';
/**Thrown when a response body fails validation against a user-provided Standard Schema.
This error intentionally does not extend `KyError` because it does not represent a failure in Ky's HTTP lifecycle. The request succeeded; the user's schema rejected the data. As such, it is not matched by `isKyError()`.
@example```import ky, {SchemaValidationError} from 'ky';import {z} from 'zod';
const userSchema = z.object({name: z.string()});
try { const user = await ky('/api/user').json(userSchema); console.log(user.name);} catch (error) { if (error instanceof SchemaValidationError) { console.error(error.issues); }}```*/export class SchemaValidationError extends Error { override name = 'SchemaValidationError' as const; readonly issues: readonly StandardSchemaV1Issue[];
constructor(issues: readonly StandardSchemaV1Issue[]) { super('Response schema validation failed'); this.issues = issues; }}