nonOptional
Creates a non optional schema.
This schema function can be used to override the behavior of
optional.
const Schema = v.nonOptional<TWrapped, TMessage>(wrapped, message);
Generics
TWrappedextends BaseSchema<unknown, unknown, BaseIssue<unknown>>TMessageextends ErrorMessage<NonOptionalIssue> | undefined
Parameters
wrappedTWrappedmessageTMessage
Explanation
With nonOptional the validation of your schema will not pass undefined inputs. If the input is undefined, you can use message to customize the error message.
Returns
SchemaNonOptionalSchema<TWrapped, TMessage>
Examples
The following examples show how nonOptional can be used.
Non optional string
Schema that does not accept undefined.
const NonOptionalStringSchema = v.nonOptional(v.optional(v.string()));
Unwrap non optional
Use unwrap to undo the effect of nonOptional.
const NonOptionalNumberSchema = v.nonOptional(v.optional(v.number()));
const OptionalNumberSchema = v.unwrap(NonOptionalNumberSchema);
Related
The following APIs can be combined with nonOptional.
Schemas
any, array, bigint, blob, boolean, custom, date, enum, exactOptional, file, function, instance, intersect, lazy, literal, looseObject, looseTuple, map, nan, never, nonNullable, 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, getDefault, getDefaults, getFallback, getFallbacks, is, message, parse, parser, pipe, safeParse, safeParser, unwrap
Actions
check, brand, description, flavor, guard, metadata, partialCheck, rawCheck, rawTransform, readonly, title, transform
Utils
entriesFromList, isOfKind, isOfType
© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/nonOptional/