# @salmonui/core/validation API reference

> 12 exported symbols of @salmonui/core/validation: 1 function, 6 interfaces, 3 types and 2 constants.

Canonical: https://salmonui.dev/docs/reference/core/validation

```ts
import { … } from '@salmonui/core/validation';
```

## Functions

### compileValidationRules

Kind: function

Page: https://salmonui.dev/docs/reference/core/validation/compileValidationRules

```ts
function compileValidationRules<Row = unknown, Ctx = ValidatorContext<Row>>(rules: ColumnValidation<Row, Ctx> | undefined, baseMessages?: ValidationMessages, implicitRequired?: boolean): CompiledValidator;
```

Compiles `rules` into one validator. `implicitRequired` adds a required
check without a rule (primary keys of new rows).

## Interfaces

### ColumnValidation

Kind: interface

Page: https://salmonui.dev/docs/reference/core/validation/ColumnValidation

```ts
interface ColumnValidation<Row = unknown, Ctx = ValidatorContext<Row>> {
    readonly required?: RuleValue<boolean>;
    readonly min?: RuleValue<number | string>;
    readonly max?: RuleValue<number | string>;
    readonly range?: RuleValue<readonly [
        number | string,
        number | string
    ]>;
    readonly minLength?: RuleValue<number>;
    readonly maxLength?: RuleValue<number>;
    readonly rangeLength?: RuleValue<readonly [
        number,
        number
    ]>;
    readonly number?: RuleValue<boolean>;
    readonly digits?: RuleValue<boolean>;
    readonly email?: RuleValue<boolean>;
    readonly url?: RuleValue<boolean>;
    readonly date?: RuleValue<boolean>;
    readonly dateIso?: RuleValue<boolean>;
    readonly creditcard?: RuleValue<boolean>;
    readonly tel?: RuleValue<boolean>;
    readonly pattern?: RuleValue<string | RegExp>;
    readonly validate?: CustomValidator<Row, Ctx> | readonly [
        CustomValidator<Row, Ctx>,
        string
    ] | readonly CustomValidator<Row, Ctx>[];
    readonly messages?: Partial<ValidationMessages>;
}
```

Members (18):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `required` | `readonly required?: RuleValue<boolean>;` | optional |  |
| `min` | `readonly min?: RuleValue<number \| string>;` | optional |  |
| `max` | `readonly max?: RuleValue<number \| string>;` | optional |  |
| `range` | `readonly range?: RuleValue<readonly [     number \| string,     number \| string ]>;` | optional |  |
| `minLength` | `readonly minLength?: RuleValue<number>;` | optional |  |
| `maxLength` | `readonly maxLength?: RuleValue<number>;` | optional |  |
| `rangeLength` | `readonly rangeLength?: RuleValue<readonly [     number,     number ]>;` | optional |  |
| `number` | `readonly number?: RuleValue<boolean>;` | optional |  |
| `digits` | `readonly digits?: RuleValue<boolean>;` | optional |  |
| `email` | `readonly email?: RuleValue<boolean>;` | optional |  |
| `url` | `readonly url?: RuleValue<boolean>;` | optional |  |
| `date` | `readonly date?: RuleValue<boolean>;` | optional |  |
| `dateIso` | `readonly dateIso?: RuleValue<boolean>;` | optional |  |
| `creditcard` | `readonly creditcard?: RuleValue<boolean>;` | optional |  |
| `tel` | `readonly tel?: RuleValue<boolean>;` | optional |  |
| `pattern` | `readonly pattern?: RuleValue<string \| RegExp>;` | optional | A pattern string or a RegExp, tested as a search: a partial match passes, so anchor the pattern (`^[A-Z]{3}$`) to require the whole value. |
| `validate` | `readonly validate?: CustomValidator<Row, Ctx> \| readonly [     CustomValidator<Row, Ctx>,     string ] \| readonly CustomValidator<Row, Ctx>[];` | optional | One custom validator, `[validator, message]`, or several. |
| `messages` | `readonly messages?: Partial<ValidationMessages>;` | optional | Message overrides of this column, keyed by rule name; they win over `edit.messages`. |

### CompiledValidator

Kind: interface

Page: https://salmonui.dev/docs/reference/core/validation/CompiledValidator

```ts
interface CompiledValidator {
    readonly required: boolean;
    validate(value: unknown, row: unknown): readonly string[];
    readonly custom: AsyncCheck | null;
}
```

One column's compiled rules.

Members (3):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `required` | `readonly required: boolean;` |  | True when a `required` rule (or an implicit primary-key requirement) applies. |
| `validate` | `validate(value: unknown, row: unknown): readonly string[];` |  | Synchronous built-in rules: the first failing message, as a one-element array, or `NO_ERRORS`. |
| `custom` | `readonly custom: AsyncCheck \| null;` |  | Custom validators: a message, null, or a promise of either. Null when the column has no custom rule. |

### FieldValidatorContext

Kind: interface

Page: https://salmonui.dev/docs/reference/core/validation/FieldValidatorContext

```ts
interface FieldValidatorContext {
    readonly name: string;
    readonly formData: FormDataLike | null;
    readonly isNew: false;
}
```

Context of a form field's custom validators (`@salmonui/inputs/form` `Field`). It is passed as both the `row` and the
`ctx` argument, so `validate(value, { formData })` reads other fields.

Members (3):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `name` | `readonly name: string;` |  | The field's `name` (`''` without one). |
| `formData` | `readonly formData: FormDataLike \| null;` |  | The owning form's data at validation time; `null` outside a form. |
| `isNew` | `readonly isNew: false;` |  |  |

### FormDataLike

Kind: interface

Page: https://salmonui.dev/docs/reference/core/validation/FormDataLike

```ts
interface FormDataLike {
    get(name: string): unknown;
    getAll(name: string): unknown[];
    has(name: string): boolean;
}
```

The read side of `FormData` (core has no DOM types).

Members (3):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `get` | `get(name: string): unknown;` |  |  |
| `getAll` | `getAll(name: string): unknown[];` |  |  |
| `has` | `has(name: string): boolean;` |  |  |

### ValidationMessages

Kind: interface

Page: https://salmonui.dev/docs/reference/core/validation/ValidationMessages

```ts
interface ValidationMessages {
    readonly required: string;
    readonly min: string;
    readonly max: string;
    readonly range: string;
    readonly minLength: string;
    readonly maxLength: string;
    readonly rangeLength: string;
    readonly number: string;
    readonly digits: string;
    readonly email: string;
    readonly url: string;
    readonly date: string;
    readonly dateIso: string;
    readonly creditcard: string;
    readonly tel: string;
    readonly pattern: string;
    readonly validate: string;
}
```

Message templates; `{0}` and `{1}` are the rule arguments.

Members (17):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `required` | `readonly required: string;` |  |  |
| `min` | `readonly min: string;` |  |  |
| `max` | `readonly max: string;` |  |  |
| `range` | `readonly range: string;` |  |  |
| `minLength` | `readonly minLength: string;` |  |  |
| `maxLength` | `readonly maxLength: string;` |  |  |
| `rangeLength` | `readonly rangeLength: string;` |  |  |
| `number` | `readonly number: string;` |  |  |
| `digits` | `readonly digits: string;` |  |  |
| `email` | `readonly email: string;` |  |  |
| `url` | `readonly url: string;` |  |  |
| `date` | `readonly date: string;` |  |  |
| `dateIso` | `readonly dateIso: string;` |  |  |
| `creditcard` | `readonly creditcard: string;` |  |  |
| `tel` | `readonly tel: string;` |  |  |
| `pattern` | `readonly pattern: string;` |  |  |
| `validate` | `readonly validate: string;` |  |  |

### ValidatorContext

Kind: interface

Page: https://salmonui.dev/docs/reference/core/validation/ValidatorContext

```ts
interface ValidatorContext<Row, Column = unknown> {
    readonly rowId: RowId;
    readonly columnId: string;
    readonly column: Column;
    readonly isNew: boolean;
}
```

Context passed to custom validators; `Column` is the host's column (the Data Grid passes its compiled column).

Members (4):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `rowId` | `readonly rowId: RowId;` |  |  |
| `columnId` | `readonly columnId: string;` |  |  |
| `column` | `readonly column: Column;` |  |  |
| `isNew` | `readonly isNew: boolean;` |  | True for a row added in this edit session (not yet saved). |

## Types

### CustomValidator

Kind: type

Page: https://salmonui.dev/docs/reference/core/validation/CustomValidator

```ts
type CustomValidator<Row = unknown, Ctx = ValidatorContext<Row>> = (value: unknown, row: Row, ctx: Ctx) => string | boolean | null | undefined | PromiseLike<string | boolean | null | undefined>;
```

Custom validator: returns an error message, or null / undefined / true when
the value is valid (false uses the rule message). May return a promise
(async validation): commit waits for it.

### FieldValidation

Kind: type

Page: https://salmonui.dev/docs/reference/core/validation/FieldValidation

```ts
type FieldValidation = ColumnValidation<FieldValidatorContext, FieldValidatorContext>;
```

Rules of a form field: the column vocabulary with the field context.

Members (18):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `required` | `required?: RuleValue<boolean>;` | optional, inherited |  |
| `min` | `min?: RuleValue<string \| number>;` | optional, inherited |  |
| `max` | `max?: RuleValue<string \| number>;` | optional, inherited |  |
| `range` | `range?: RuleValue<readonly [string \| number, string \| number]>;` | optional, inherited |  |
| `minLength` | `minLength?: RuleValue<number>;` | optional, inherited |  |
| `maxLength` | `maxLength?: RuleValue<number>;` | optional, inherited |  |
| `rangeLength` | `rangeLength?: RuleValue<readonly [number, number]>;` | optional, inherited |  |
| `number` | `number?: RuleValue<boolean>;` | optional, inherited |  |
| `digits` | `digits?: RuleValue<boolean>;` | optional, inherited |  |
| `email` | `email?: RuleValue<boolean>;` | optional, inherited |  |
| `url` | `url?: RuleValue<boolean>;` | optional, inherited |  |
| `date` | `date?: RuleValue<boolean>;` | optional, inherited |  |
| `dateIso` | `dateIso?: RuleValue<boolean>;` | optional, inherited |  |
| `creditcard` | `creditcard?: RuleValue<boolean>;` | optional, inherited |  |
| `tel` | `tel?: RuleValue<boolean>;` | optional, inherited |  |
| `pattern` | `pattern?: RuleValue<string \| RegExp>;` | optional, inherited | A pattern string or a RegExp, tested as a search: a partial match passes, so anchor the pattern (`^[A-Z]{3}$`) to require the whole value. |
| `validate` | `validate?: CustomValidator<FieldValidatorContext, FieldValidatorContext> \| readonly CustomValidator<FieldValidatorContext, FieldValidatorContext>[] \| readonly [...];` | optional, inherited | One custom validator, `[validator, message]`, or several. |
| `messages` | `messages?: Partial<ValidationMessages>;` | optional, inherited | Message overrides of this column, keyed by rule name; they win over `edit.messages`. |

### RuleValue

Kind: type

Page: https://salmonui.dev/docs/reference/core/validation/RuleValue

```ts
type RuleValue<T> = T | readonly [
    T,
    string
];
```

A rule argument, or `[argument, message]` to override the message.

## Constants

### DEFAULT_VALIDATION_MESSAGES

Kind: const

Page: https://salmonui.dev/docs/reference/core/validation/DEFAULT_VALIDATION_MESSAGES

```ts
const DEFAULT_VALIDATION_MESSAGES: ValidationMessages;
```

English defaults. Override with `edit.messages` / `editing.messages`.

### NO_ERRORS

Kind: const

Page: https://salmonui.dev/docs/reference/core/validation/NO_ERRORS

```ts
const NO_ERRORS: readonly string[];
```
