getDefaultsAsync

Returns the default values of the schema.

The difference to getDefault is that for object and tuple schemas this function recursively returns the default values of the subschemas instead of undefined.

const values = v.getDefaultsAsync<TSchema>(schema);

Generics

Parameters

  • schema TSchema

Returns

Examples

The following examples show how getDefaultsAsync can be used.

Donation schema defaults

Get the default values of a donation schema.

import { getRandomOrgId } from '~/api';

const DonationSchema = v.objectAsync({
  timestamp: v.optional(v.date(), () => new Date()),
  sponsor: v.optional(v.pipe(v.string(), v.nonEmpty()), 'anonymous'),
  organizationId: v.optionalAsync(v.pipe(v.string(), v.uuid()), getRandomOrgId),
  message: v.optional(v.pipe(v.string(), v.minLength(1))),
});

const defaultValues = await v.getDefaultsAsync(DonationSchema);

/*
  {
    timestamp: new Date(),
    sponsor: "anonymous",
    organizationId: "43775869-95f3-4e00-9f37-161ec8f9f7cd",
    message: undefined
  }
*/

The following APIs can be combined with getDefaultsAsync.

Schemas

Methods

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