# optional

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

Creates an optional schema.

```ts
const Schema = v.optional<TWrapped, TDefault>(wrapped, default_);
```

## Generics

- `TWrapped` `extends BaseSchema<unknown, unknown, BaseIssue<unknown>>`
- `TDefault` `extends Default<TWrapped, undefined>`

## Parameters

- `wrapped` `TWrapped`
- `default_` `TDefault`

### Explanation

With `optional` the validation of your schema will pass `undefined` inputs, and if you specify a `default_` input value, the schema will use it if the input is `undefined`. For this reason, the output type may differ from the input type of the schema.

> **Important**: When used in object schemas, if a key is missing and no `default_` value is provided, the schema's pipe (including transformations) will not be executed. To ensure pipes run for missing keys, provide a `default_` value.

> Note that `optional` does not accept `null` as an input. If you want to accept `null` inputs, use [`nullable`](/api/nullable.md), and if you want to accept `null` and `undefined` inputs, use [`nullish`](/api/nullish.md) instead. Also, if you want to set a default output value for any invalid input, you should use [`fallback`](/api/fallback.md) instead.

## Returns

- `Schema` `OptionalSchema<TWrapped, TDefault>`

## Examples

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

### Optional string schema

Schema that accepts `string` and `undefined`.

```ts
const OptionalStringSchema = v.optional(v.string(), "I'm the default!");
```

### Optional date schema

Schema that accepts [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) and `undefined`.

> By using a function as the `default_` parameter, the schema will return a new [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) instance each time the input is `undefined`.

```ts
const OptionalDateSchema = v.optional(v.date(), () => new Date());
```

### Optional entry schema

Object schema with an optional entry.

```ts
const OptionalEntrySchema = v.object({
  key: v.optional(v.string()),
});
```

### Default to `undefined`

By default, when an object input is missing an optional entry, the corresponding key is omitted from the output. To always include the key in the output with an `undefined` value, pass a function that returns `undefined` as the `default_` parameter.

```ts
const OptionalEntrySchema = v.object({
  key: v.optional(v.string(), () => undefined),
});
```

### Unwrap optional schema

Use [`unwrap`](/api/unwrap.md) to undo the effect of `optional`.

```ts
const OptionalNumberSchema = v.optional(v.number());
const NumberSchema = v.unwrap(OptionalNumberSchema);
```

### Optional with pipes

When using `optional` in a [`pipe`](/api/pipe.md), the pipe actions only execute if a `default_` value is provided or the key is present. This applies to all pipe actions including [`transform`](/api/transform.md), [`check`](/api/check.md), and others.

```ts
const SchemaWithoutDefault = v.object({
  isActive: v.pipe(
    v.optional(v.string()),
    v.transform((value) => value === 'true') // Does not run for missing keys
  ),
}); // Output type: { isActive?: boolean }

const SchemaWithDefault = v.object({
  isActive: v.pipe(
    v.optional(v.string(), 'false'), // Default value provided
    v.transform((value) => value === 'true') // Runs for missing keys too
  ),
}); // Output type: { isActive: boolean }
```

## Related

The following APIs can be combined with `optional`.

### 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), [`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

[`assert`](/api/assert.md), [`config`](/api/config.md), [`fallback`](/api/fallback.md), [`getDefault`](/api/getDefault.md), [`getDefaults`](/api/getDefaults.md), [`getFallback`](/api/getFallback.md), [`getFallbacks`](/api/getFallbacks.md), [`is`](/api/is.md), [`message`](/api/message.md), [`parse`](/api/parse.md), [`parser`](/api/parser.md), [`pipe`](/api/pipe.md), [`safeParse`](/api/safeParse.md), [`safeParser`](/api/safeParser.md), [`unwrap`](/api/unwrap.md)

### Actions

[`check`](/api/check.md), [`brand`](/api/brand.md), [`description`](/api/description.md), [`flavor`](/api/flavor.md), [`guard`](/api/guard.md), [`metadata`](/api/metadata.md), [`partialCheck`](/api/partialCheck.md), [`rawCheck`](/api/rawCheck.md), [`rawTransform`](/api/rawTransform.md), [`readonly`](/api/readonly.md), [`title`](/api/title.md), [`transform`](/api/transform.md)

### Utils

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