Skip to main content
⚠️ AppKit is in an early development phase and is subject to change without notice. DO NOT use in production environments.

# Class: ValidationError

Error thrown when input validation fails. Use for invalid parameters, missing required fields, or type mismatches.

## Example​

    throw new ValidationError("Statement is required", { context: { field: "statement" } });
    throw new ValidationError("maxPoolSize must be at least 1", { context: { value: config.maxPoolSize } });

## Extends​

- `AppKitError`

## Constructors​

### Constructor​

    new ValidationError(message: string, options?: {
      cause?: Error;
      context?: Record<string, unknown>;
    }): ValidationError;

#### Parameters​

Parameter | Type
--- | ---
`message` | `string`
`options?` | { `cause?`: `Error`; `context?`: `Record`<`string`, `unknown`>; }
`options.cause?` | `Error`
`options.context?` | `Record`<`string`, `unknown`>

#### Returns​

`ValidationError`

#### Inherited from​

`AppKitError`.`constructor`

## Properties​

### cause?​

    readonly optional cause: Error;

Optional cause of the error

#### Inherited from​

`AppKitError`.`cause`

### code​

    readonly code: "VALIDATION_ERROR" = "VALIDATION_ERROR";

Error code for programmatic error handling

#### Overrides​

`AppKitError`.`code`

### context?​

    readonly optional context: Record<string, unknown>;

Additional context for the error

#### Inherited from​

`AppKitError`.`context`

### isRetryable​

    readonly isRetryable: false = false;

Whether this error type is generally safe to retry

#### Overrides​

`AppKitError`.`isRetryable`

### statusCode​

    readonly statusCode: 400 = 400;

HTTP status code suggestion (can be overridden)

#### Overrides​

`AppKitError`.`statusCode`

## Methods​

### toJSON()​

    toJSON(): Record<string, unknown>;

Convert error to JSON for logging/serialization. Sensitive values in context are automatically redacted.

#### Returns​

`Record`<`string`, `unknown`>

#### Inherited from​

`AppKitError`.`toJSON`

### toString()​

    toString(): string;

Create a human-readable string representation

#### Returns​

`string`

#### Inherited from​

`AppKitError`.`toString`

### invalidValue()​

    static invalidValue(
       fieldName: string,
       value: unknown,
       expected?: string): ValidationError;

Create a validation error for an invalid field value. Note: The actual value is not stored in context for security reasons. Only the value's type is recorded.

#### Parameters​

Parameter | Type
--- | ---
`fieldName` | `string`
`value` | `unknown`
`expected?` | `string`

#### Returns​

`ValidationError`

### missingEnvVars()​

    static missingEnvVars(vars: string[]): ValidationError;

Create a validation error for missing environment variables

#### Parameters​

Parameter | Type
--- | ---
`vars` | `string`[]

#### Returns​

`ValidationError`

### missingField()​

    static missingField(fieldName: string): ValidationError;

Create a validation error for a missing required field

#### Parameters​

Parameter | Type
--- | ---
`fieldName` | `string`

#### Returns​

`ValidationError`