required

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

const Schema = required<TSchema>(schema, message);
const Schema = required<TSchema, TKeys>(schema, keys, message);

Generics

Parameters

  • schema TSchema
  • keys TKey
  • message ErrorMessage<NonOptionalIssue> | undefined

Explanation

required creates a modified copy of the given object schema where all entries or only the selected keys are required. It is similar to TypeScript's Required 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 method, as this may cause runtime errors. Please use the pipe method after you have modified the schema with required.

Returns

  • Schema SchemaWithRequired<TSchema, TKeys>

Examples

The following examples show how required can be used.

Required object schema

Schema to validate an object with required entries.

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.

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 }

The following APIs can be combined with required.

Schemas

Methods

Actions

Contributors

Thanks to all the contributors who helped make this page better!

  • GitHub profile picture of fabian-hiller

Partners

Thanks to our partners who support the project ideally and financially.

Sponsors

Thanks to our GitHub sponsors who support the project financially.

  • GitHub profile picture of dailydotdev
  • GitHub profile picture of KATT
  • GitHub profile picture of osdiab
  • GitHub profile picture of Thanaen
  • GitHub profile picture of ruiaraujo012
  • GitHub profile picture of hyunbinseo
  • GitHub profile picture of caegdeveloper
  • GitHub profile picture of seahindeniz