# guard

> This document is the Markdown version of [valibot.dev/api/guard/](https://valibot.dev/api/guard/). For the complete documentation index, see [llms.txt](https://valibot.dev/llms.txt).

Creates a guard transformation action.

```ts
const Action = v.guard<TInput, TGuard, TMessage>(requirement, message);
```

## Generics

- `TInput` `extends any`
- `TGuard` `extends GuardFunction<TInput>`
- `TMessage` `extends ErrorMessage<GuardIssue<TInput, TGuard>> | undefined`

## Parameters

- `requirement` `TGuard`
- `message` `TMessage`

### Explanation

With `guard` you can freely validate the input and return `true` if it is valid or `false` otherwise. If the input does not match your `requirement`, you can use `message` to customize the error message.

This is especially useful if you have an existing type predicate (for example, from an external library).

> `guard` is useful for narrowing known types. For validating completely unknown values, consider [`custom`](/api/custom.md) instead.

## Returns

- `Action` `GuardAction<TInput, TGuard, TMessage>`

## Examples

The following examples show how `guard` can be used.

### Pixel string schema

Schema to validate a pixel string.

```ts
const PixelStringSchema = v.pipe(
  v.string(),
  v.guard((input): input is `${number}px` => /^\d+px$/.test(input))
);
```

### Axios Error schema

Schema to validate an object containing an Axios error.

```ts
import { isAxiosError } from 'axios';

const AxiosErrorSchema = v.object({
  error: v.pipe(
    v.instance(Error),
    v.guard(isAxiosError, 'The error is not an Axios error.')
  ),
});
```

## Related

The following APIs can be combined with `guard`.

### Schemas

[`any`](/api/any.md), [`array`](/api/array.md), [`bigint`](/api/bigint.md), [`blob`](/api/blob.md), [`boolean`](/api/boolean.md), [`custom`](/api/custom.md), [`date`](/api/date.md), [`enum`](/api/enum.md), [`exactOptional`](/api/exactOptional.md), [`file`](/api/file.md), [`function`](/api/function.md), [`instance`](/api/instance.md), [`intersect`](/api/intersect.md), [`lazy`](/api/lazy.md), [`literal`](/api/literal.md), [`looseObject`](/api/looseObject.md), [`looseTuple`](/api/looseTuple.md), [`map`](/api/map.md), [`nan`](/api/nan.md), [`never`](/api/never.md), [`nonNullable`](/api/nonNullable.md), [`nonNullish`](/api/nonNullish.md), [`nonOptional`](/api/nonOptional.md), [`null`](/api/null.md), [`nullable`](/api/nullable.md), [`nullish`](/api/nullish.md), [`number`](/api/number.md), [`object`](/api/object.md), [`objectWithRest`](/api/objectWithRest.md), [`optional`](/api/optional.md), [`picklist`](/api/picklist.md), [`promise`](/api/promise.md), [`record`](/api/record.md), [`set`](/api/set.md), [`strictObject`](/api/strictObject.md), [`strictTuple`](/api/strictTuple.md), [`string`](/api/string.md), [`symbol`](/api/symbol.md), [`tuple`](/api/tuple.md), [`tupleWithRest`](/api/tupleWithRest.md), [`undefined`](/api/undefined.md), [`undefinedable`](/api/undefinedable.md), [`union`](/api/union.md), [`unknown`](/api/unknown.md), [`variant`](/api/variant.md), [`void`](/api/void.md)

### Methods

[`forward`](/api/forward.md), [`pipe`](/api/pipe.md)

### Utils

[`isOfKind`](/api/isOfKind.md), [`isOfType`](/api/isOfType.md)
