@vinejs/vine - v4.2.0
    Preparing search index...

    Class VineOptional<Output>

    VineOptional represents an optional value inside a union or schema. It allows both null and undefined values to pass validation.

    This type is typically used with unions to make certain branches optional, or to explicitly mark a field as allowing undefined/null values.

    const schema = vine.object({
    name: vine.string().optional()
    })
    const schema = vine.unionOfTypes([
    vine.string(),
    vine.optional()
    ])

    Type Parameters

    • Output

      The output type when the value is defined

    Hierarchy (View Summary)

    Implements

    Index

    Constructors

    Properties

    "[ITYPE]": null | undefined

    The input type of the schema (null or undefined)

    "[OTYPE]": Output

    The output type of the schema when value is defined

    "[COTYPE]": Output

    The camelCase output type of the schema

    "[SUBTYPE]": string = 'optional'

    The subtype identifier for the literal schema field

    "[UNIQUE_NAME]": string = 'vine.optional'

    Unique name identifier for union type resolution

    Methods

    • Marks the field as required when a condition is met. Can be used with a callback or with comparison operators to compare another field's value.

      Type Parameters

      Parameters

      • otherField: string

        Field name to compare or callback function

      • operator: Operator

        Comparison operator (=, !=, in, notIn, >, <, >=, <=)

      • expectedValue: Operator extends ArrayComparisonOperators
            ? (string | number | boolean)[]
            : Operator extends NumericComparisonOperators
                ? number
                : string
                | number
                | boolean

        Expected value to compare against

      Returns this

      vine.string().optional().requiredWhen('role', '=', 'admin')
      
      vine.string().optional().requiredWhen('age', '>', 18)
      
      vine.string().optional().requiredWhen((field) => field.data.isAdmin === true)
      
    • Marks the field as required when a condition is met. Can be used with a callback or with comparison operators to compare another field's value.

      Parameters

      • callback: (field: FieldContext) => boolean

      Returns this

      vine.string().optional().requiredWhen('role', '=', 'admin')
      
      vine.string().optional().requiredWhen('age', '>', 18)
      
      vine.string().optional().requiredWhen((field) => field.data.isAdmin === true)
      
    • Type checker function to determine if a value is optional (null or undefined). Required for "unionOfTypes" functionality.

      Parameters

      • value: unknown

        The value to check

      Returns value is null | undefined

    • Define a method to parse the input value. The method is invoked before any validation and hence you must perform type-checking to know the value you are working with.

      Parameters

      • callback: ParseFn

        Parser function to transform the input value

      Returns this

    • Mark the field under validation to be nullable. The null value will be written to the output as well. When combined with optional, both null and undefined values are allowed.

      Returns VineOptional<null | undefined>

    • Compiles the schema type to a compiler node.

      Parameters

      • propertyName: string

        The name of the property being validated

      • refs: RefsStore

        Reference store for tracking validators and parsers

      • options: ParserOptions

        Parser options including camelCase transformation

      Returns FieldNode & {} & { subtype: string }