objectWithRest

Creates an object with rest schema.

const Schema = v.objectWithRest<TEntries, TRest, TMessage>(
  entries,
  rest,
  message
);

Generics

Parameters

  • entries TEntries
  • rest TRest
  • message TMessage

Explanation

With objectWithRest you can validate the data type of the input and whether the content matches entries and rest. If the input is not an object, you can use message to customize the error message.

The difference to object is that this schema includes unknown entries in the output. In addition, this schema filters certain entries from the unknown entries for security reasons.

Returns

Examples

The following examples show how objectWithRest can be used. Please see the object guide for more examples and explanations.

Object schema with rest

Schema to validate an object with generic rest entries.

const ObjectSchemaWithRest = v.objectWithRest(
  {
    key1: v.string(),
    key2: v.number(),
  },
  v.boolean()
);

Merge several objects

Schema that merges the entries of two object schemas.

const MergedObjectSchema = v.objectWithRest(
  {
    ...ObjectSchema1.entries,
    ...ObjectSchema2.entries,
  },
  v.null()
);

Mark keys as optional

Schema to validate an object with partial entries.

const PartialObjectSchema = partial(
  objectWithRest(
    {
      key1: string(),
      key2: number(),
    },
    v.undefined()
  )
);

Object with selected entries

Schema to validate only selected entries of an object.

const PickObjectSchema = v.pick(
  v.objectWithRest(
    {
      key1: v.string(),
      key2: v.number(),
      key3: v.boolean(),
    },
    v.null()
  ),
  ['key1', 'key3']
);

The following APIs can be combined with objectWithRest.

Schemas

Methods

Actions

Utils

Contributors

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

  • GitHub profile picture of @fabian-hiller
  • GitHub profile picture of @kazizi55
  • GitHub profile picture of @muningis

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 @antfu
  • GitHub profile picture of @Thanaen
  • GitHub profile picture of @osdiab
  • GitHub profile picture of @ruiaraujo012
  • GitHub profile picture of @hyunbinseo
  • GitHub profile picture of @F0rce
  • GitHub profile picture of @UniquePixels
  • GitHub profile picture of @jdgamble555
  • GitHub profile picture of @nickytonline
  • GitHub profile picture of @KubaJastrz
  • GitHub profile picture of @andrewmd5
  • GitHub profile picture of @caegdeveloper
  • GitHub profile picture of @dslatkin
  • GitHub profile picture of @BrianCurliss