strictObjectAsync
Creates a strict object schema.
const Schema = v.strictObjectAsync<TEntries, TMessage>(entries, message);
Generics
TEntriesextends ObjectEntriesAsyncTMessageextends ErrorMessage<StrictObjectIssue> | undefined
Parameters
entriesTEntriesmessageTMessage
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
objectAsyncis 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 theobjectWithRestAsyncschema withneverfor therestargument.
Returns
SchemaStrictObjectSchemaAsync<TEntries, TMessage>
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())),
});
Related
The following APIs can be combined with strictObjectAsync.
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, keyof, omit, pick
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, forwardAsync, getDefaultsAsync, getFallbacksAsync, intersectAsync, lazyAsync, looseObjectAsync, looseTupleAsync, mapAsync, nonNullableAsync, nonNullishAsync, nonOptionalAsync, nullableAsync, nullishAsync, objectAsync, objectWithRestAsync, optionalAsync, parseAsync, parserAsync, partialAsync, partialCheckAsync, pipeAsync, rawCheckAsync, rawTransformAsync, recordAsync, requiredAsync, safeParseAsync, safeParserAsync, setAsync, strictTupleAsync, transformAsync, tupleAsync, tupleWithRestAsync, unionAsync, variantAsync
© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/strictObjectAsync/