looseObject

Creates a loose object schema.

const Schema = v.looseObject<TEntries, TMessage>(entries, message);

Generics

Parameters

  • entries TEntries
  • message TMessage

Explanation

With looseObject you can validate the data type of the input and whether the content matches entries. 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 any unknown entries in the output. In addition, this schema filters certain entries from the unknown entries for security reasons.

This schema does not distinguish between missing and undefined entries. The reason for this decision is that it reduces the bundle size, and we also expect that most users will expect this behavior.

Returns

Examples

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

Simple object schema

Schema to validate a loose object with two specific keys.

const SimpleObjectSchema = v.looseObject({
  key1: v.string(),
  key2: v.number(),
});

Merge several objects

Schema that merges the entries of two object schemas.

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

Mark keys as optional

Schema to validate an object with partial entries.

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

Object with selected entries

Schema to validate only selected entries of a loose object.

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

The following APIs can be combined with looseObject.

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

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 fabulousgk
  • GitHub profile picture of jdgamble555
  • GitHub profile picture of isoden
  • GitHub profile picture of nickytonline
  • GitHub profile picture of caegdeveloper
  • GitHub profile picture of luckasnix
  • GitHub profile picture of andrew-3kb
  • GitHub profile picture of dslatkin