# exactOptional

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

Creates an exact optional schema.

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

## Generics

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

## Parameters

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

### Explanation

With `exactOptional` the validation of your schema will pass missing object entries, and if you specify a `default_` input value, the schema will use it if the object entry is missing. 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.

> The difference to [`optional`](/api/optional.md) is that this schema function follows the implementation of TypeScript's [`exactOptionalPropertyTypes` configuration](https://www.typescriptlang.org/tsconfig/#exactOptionalPropertyTypes) and only allows missing but not undefined object entries.

## Returns

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

## Examples

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

### Exact optional object entries

Object schema with exact optional entries.

> 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 OptionalEntrySchema = v.object({
  key1: v.exactOptional(v.string()),
  key2: v.exactOptional(v.string(), "I'm the default!"),
  key3: v.exactOptional(v.date(), () => new Date()),
});
```

### Unwrap exact optional schema

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

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

### Exact optional with pipes

When using `exactOptional` 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({
  isEnabled: v.pipe(
    v.exactOptional(v.string()),
    v.transform((value) => value === '1') // Does not run for missing keys
  ),
}); // Output type: { isEnabled?: boolean }

const SchemaWithDefault = v.object({
  isEnabled: v.pipe(
    v.exactOptional(v.string(), '0'), // Default value provided
    v.transform((value) => value === '1') // Runs for missing keys too
  ),
}); // Output type: { isEnabled: boolean }
```

## Related

The following APIs can be combined with `exactOptional`.

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

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