getDefaultsAsync
Returns the default values of the schema.
The difference to
getDefaultis that for object and tuple schemas this function recursively returns the default values of the subschemas instead ofundefined.
const values = v.getDefaultsAsync<TSchema>(schema);
Generics
TSchemaextends BaseSchema<unknown, unknown, BaseIssue<unknown>> | BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>
Parameters
schemaTSchema
Returns
valuesPromise<InferDefaults<TSchema>>
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
}
*/
Related
The following APIs can be combined with getDefaultsAsync.
Schemas
any, array, bigint, blob, boolean, custom, date, enum, exactOptional, file, function, instance, intersect, lazy, literal, looseObject, looseTuple, map, nan, never, nonNullable, nonNullish, nonOptional, null, nullable, nullish, number, object, objectWithRest, optional, picklist, promise, record, set, strictObject, strictTuple, string, symbol, tuple, tupleWithRest, undefined, undefinedable, union, unknown, variant, void
Methods
assert, config, fallback, keyof, message, omit, partial, pick, pipe, required, unwrap
Async
arrayAsync, customAsync, exactOptionalAsync, fallbackAsync, intersectAsync, lazyAsync, looseObjectAsync, looseTupleAsync, mapAsync, nonNullableAsync, nonNullishAsync, nonOptionalAsync, nullableAsync, nullishAsync, objectAsync, objectWithRestAsync, optionalAsync, partialAsync, pipeAsync, recordAsync, requiredAsync, setAsync, strictObjectAsync, strictTupleAsync, tupleAsync, tupleWithRestAsync, unionAsync, variantAsync
© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/getDefaultsAsync/