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

    Function union

    • Create a new union schema type. A union is a collection of conditionals and schema associated with it.

      Unions evaluate conditionals in order and apply the first matching schema. Use union.if() to define conditional branches and union.else() for the default fallback.

      Type Parameters

      Parameters

      • conditionals: Conditional[]

        Array of conditional branches to evaluate

      Returns VineUnion<Conditional>

      const schema = vine.union([
      vine.union.if(
      (value) => typeof value === 'string',
      vine.string()
      ),
      vine.union.if(
      (value) => typeof value === 'number',
      vine.number()
      )
      ])
    Index

    Methods

    Methods

    • Wrap a schema inside a conditional branch. The condition is evaluated at runtime, and if it returns a truthy value, the associated schema is used for validation.

      Type Parameters

      • Schema extends SchemaTypes

        The schema type to apply when condition matches

      Parameters

      • conditon: (value: Record<string, unknown>, field: FieldContext) => any

        Function that evaluates whether this branch should be used

      • schema: Schema

        The schema to apply if the condition is true

      Returns UnionConditional<Schema>

      vine.union.if(
      (value) => value.type === 'email',
      vine.object({ email: vine.string().email() })
      )