# required

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

Creates a modified copy of an object schema that marks all or only the selected entries as required.

```ts
const AllKeysSchema = v.required<TSchema, TMessage>(schema, message);
const SelectedKeysSchema = v.required<TSchema, TKeys, TMessage>(
  schema,
  keys,
  message
);
```

## Generics

- `TSchema` `extends SchemaWithoutPipe<LooseObjectSchema<ObjectEntries, ErrorMessage<LooseObjectIssue> | undefined> | ObjectSchema<ObjectEntries, ErrorMessage<ObjectIssue> | undefined> | ObjectWithRestSchema<ObjectEntries, BaseSchema<unknown, unknown, BaseIssue<unknown>>, ErrorMessage<ObjectWithRestIssue> | undefined> | StrictObjectSchema<ObjectEntries, ErrorMessage<StrictObjectIssue> | undefined>>`
- `TKeys` `extends ObjectKeys<TSchema>`
- `TMessage` `extends ErrorMessage<NonOptionalIssue> | undefined`

## Parameters

- `schema` `TSchema`
- `keys` `TKey`
- `message` `TMessage`

### Explanation

`required` creates a modified copy of the given object `schema` where all or only the selected `keys` are required. It is similar to TypeScript's [`Required`](https://www.typescriptlang.org/docs/handbook/utility-types.html#requiredtype) utility type.

> Because `required` changes the data type of the input and output, it is not allowed to pass a schema that has been modified by the [`pipe`](/api/pipe.md) method, as this may cause runtime errors. Please use the [`pipe`](/api/pipe.md) method after you have modified the schema with `required`.

## Returns

- `AllKeysSchema` `SchemaWithRequired<TSchema, undefined, TMessage>`
- `SelectedKeysSchema` `SchemaWithRequired<TSchema, Tkeys, TMessage>`

## Examples

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

### Required object schema

Schema to validate an object with required entries.

```ts
const RequiredSchema = v.required(
  v.object({
    key1: v.optional(v.string()),
    key2: v.optional(v.number()),
  })
); // { key1: string; key2: number }
```

### With only specific keys

Schema to validate an object with only specific entries marked as required.

```ts
const RequiredSchema = v.required(
  v.object({
    key1: v.optional(v.string()),
    key2: v.optional(v.number()),
    key3: v.optional(v.boolean()),
  }),
  ['key1', 'key3']
); // { key1: string; key2?: number; key3: boolean }
```

## Related

The following APIs can be combined with `required`.

### Schemas

[`array`](/api/array.md), [`exactOptional`](/api/exactOptional.md), [`intersect`](/api/intersect.md), [`lazy`](/api/lazy.md), [`looseObject`](/api/looseObject.md), [`looseTuple`](/api/looseTuple.md), [`map`](/api/map.md), [`nonNullable`](/api/nonNullable.md), [`nonNullish`](/api/nonNullish.md), [`nonOptional`](/api/nonOptional.md), [`nullable`](/api/nullable.md), [`nullish`](/api/nullish.md), [`object`](/api/object.md), [`objectWithRest`](/api/objectWithRest.md), [`optional`](/api/optional.md), [`record`](/api/record.md), [`set`](/api/set.md), [`strictObject`](/api/strictObject.md), [`strictTuple`](/api/strictTuple.md), [`tuple`](/api/tuple.md), [`tupleWithRest`](/api/tupleWithRest.md), [`undefinedable`](/api/undefinedable.md), [`union`](/api/union.md)

### Methods

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

### Actions

[`brand`](/api/brand.md), [`check`](/api/check.md), [`description`](/api/description.md), [`entries`](/api/entries.md), [`flavor`](/api/flavor.md), [`guard`](/api/guard.md), [`maxEntries`](/api/maxEntries.md), [`metadata`](/api/metadata.md), [`minEntries`](/api/minEntries.md), [`notEntries`](/api/notEntries.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

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