strictObjectAsync

Creates a strict object schema.

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

Generics

Parameters

  • entries TEntries
  • message TMessage

Explanation

With strictObjectAsync you can validate the data type of the input and whether the content matches entries. If the input is not an object or does include unknown entries, you can use message to customize the error message.

The difference to objectAsync is that this schema returns an issue for unknown entries. It intentionally returns only one issue. Otherwise, attackers could send large objects to exhaust device resources. If you want an issue for every unknown key, use the objectWithRestAsync schema with never for the rest argument.

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 strictObjectAsync can be used. Please see the object guide for more examples and explanations.

New user schema

Schema to validate a strict object containing only specific new user details.

import { isEmailPresent } from '~/api';

const NewUserSchema = v.strictObjectAsync({
  firstName: v.pipe(v.string(), v.minLength(2), v.maxLength(45)),
  lastName: v.pipe(v.string(), v.minLength(2), v.maxLength(45)),
  email: v.pipeAsync(
    v.string(),
    v.email(),
    v.checkAsync(isEmailPresent, 'The email is already in use by another user.')
  ),
  password: v.pipe(v.string(), v.minLength(8)),
  avatar: v.optional(v.pipe(v.string(), v.url())),
});

The following APIs can be combined with strictObjectAsync.

Schemas

Methods

Actions

Utils

Async

Contributors

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

  • GitHub profile picture of fabian-hiller
  • GitHub profile picture of EltonLobo07

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