recordAsync
Creates a record schema.
const Schema = v.recordAsync<TKey, TValue, TMessage>(key, value, message);
Generics
TKeyextends BaseSchema<string, string | number | symbol, BaseIssue<unknown>> | BaseSchemaAsync<string, string | number | symbol, BaseIssue<unknown>>TValueextends BaseSchema<unknown, unknown, BaseIssue<unknown>> | BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>TMessageextends ErrorMessage<RecordIssue> | undefined
Parameters
keyTKeyvalueTValuemessageTMessage
Explanation
With recordAsync you can validate the data type of the input and whether the entries match key and value. If the input is not an object, you can use message to customize the error message.
This schema filters certain entries from the record for security reasons.
This schema marks an entry as optional if it detects that its key is a literal type. The reason for this is that it is not technically possible to detect missing literal keys without restricting the
keyschema tostring,enumandpicklist. However, ifenumandpicklistare used, it is better to useobjectAsyncwithentriesFromListbecause it already covers the needed functionality. This decision also reduces the bundle size ofrecordAsync, because it only needs to check the entries of the input and not any missing keys.
Returns
SchemaRecordSchemaAsync<TKey, TValue, TMessage>
Examples
The following examples show how recordAsync can be used.
ID to email schema
Schema to validate a record that maps an ID to a public user email.
import { isEmailPublic } from '~/api';
const IdToEmailSchema = v.recordAsync(
v.pipe(v.string(), v.uuid()),
v.pipeAsync(
v.string(),
v.email(),
v.checkAsync(isEmailPublic, 'The email address is private.')
)
);
Related
The following APIs can be combined with recordAsync.
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
config, getDefault, getFallback
Actions
brand, check, description, entries, flavor, guard, maxEntries, metadata, minEntries, notEntries, partialCheck, rawCheck, rawTransform, readonly, title, transform
Utils
entriesFromList, isOfKind, isOfType
Async
arrayAsync, checkAsync, customAsync, exactOptionalAsync, fallbackAsync, getDefaultsAsync, getFallbacksAsync, intersectAsync, lazyAsync, looseObjectAsync, looseTupleAsync, mapAsync, nonNullableAsync, nonNullishAsync, nonOptionalAsync, nullableAsync, nullishAsync, objectAsync, objectWithRestAsync, optionalAsync, parseAsync, parserAsync, partialCheckAsync, pipeAsync, rawCheckAsync, rawTransformAsync, safeParseAsync, safeParserAsync, setAsync, strictObjectAsync, strictTupleAsync, transformAsync, tupleAsync, tupleWithRestAsync, undefinedableAsync, unionAsync, variantAsync
© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/recordAsync/