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

    Class VineString

    VineString represents a string value in the validation schema. It provides comprehensive string validation with built-in rules for common patterns like email, URL, UUID, and more.

    const schema = vine.string()
    .email()
    .minLength(5)
    .maxLength(100)

    const result = await vine.validate({
    schema,
    data: 'user@example.com'
    })

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    "[ITYPE]": string

    Define the input type of the schema for TypeScript inference

    "[OTYPE]": string

    The output value type of the field after validation. The property points to a type only and not the real value.

    "[COTYPE]": string

    Type marker for camelCase output type inference

    dataTypeValidator?: Validation<any>

    The validation to use for validating the schema data type. Using a data type validator guards custom rules to only run when the data type validation passes.

    class StringSchema extends BaseLiteralType {
    dataTypeValidator = stringDataTypeRule
    }
    options: FieldOptions

    Configuration options for this field including bail mode, nullability, and parsing

    rules: {
        in: (
            ...options: [
                options: { choices: string[]
                | ((field: FieldContext) => string[]) },
            ],
        ) => Validation<
            { choices: string[]
            | ((field: FieldContext) => string[]) },
        >;
        jwt: (...options: [options?: undefined]) => Validation<undefined>;
        url: (
            ...options: [options?: IsURLOptions],
        ) => Validation<IsURLOptions | undefined>;
        iban: (...options: [options?: undefined]) => Validation<undefined>;
        uuid: (
            ...options: [options?: { version?: (1 | 2 | 3 | 4 | 5 | 6 | 7 | 8)[] }],
        ) => Validation<
            { version?: (1 | 2 | 3 | 4 | 5 | 6 | 7 | 8)[] }
            | undefined,
        >;
        ulid: (...options: [options?: undefined]) => Validation<undefined>;
        trim: (...options: [options?: undefined]) => Validation<undefined>;
        email: (
            ...options: [options?: IsEmailOptions],
        ) => Validation<IsEmailOptions | undefined>;
        alpha: (
            ...options: [options?: AlphaOptions],
        ) => Validation<AlphaOptions | undefined>;
        ascii: (...options: [options?: undefined]) => Validation<undefined>;
        notIn: (
            ...options: [
                options: { list: string[]
                | ((field: FieldContext) => string[]) },
            ],
        ) => Validation<{ list: string[] | ((field: FieldContext) => string[]) }>;
        regex: (...options: [options: RegExp]) => Validation<RegExp>;
        escape: (...options: [options?: undefined]) => Validation<undefined>;
        sameAs: (
            ...options: [options: { otherField: string }],
        ) => Validation<{ otherField: string }>;
        mobile: (
            ...options: [
                options?: | MobileOptions
                | ((field: FieldContext) => MobileOptions | undefined),
            ],
        ) => Validation<
            | MobileOptions
            | ((field: FieldContext) => MobileOptions | undefined)
            | undefined,
        >;
        string: (...options: [options?: undefined]) => Validation<undefined>;
        hexCode: (...options: [options?: undefined]) => Validation<undefined>;
        passport: (
            ...options: [
                options: PassportOptions
                | ((field: FieldContext) => PassportOptions),
            ],
        ) => Validation<
            PassportOptions
            | ((field: FieldContext) => PassportOptions),
        >;
        endsWith: (
            ...options: [options: { substring: string }],
        ) => Validation<{ substring: string }>;
        confirmed: (
            ...options: [
                options?: { confirmationField?: string }
                | { as?: string },
            ],
        ) => Validation<
            { confirmationField?: string }
            | { as?: string }
            | undefined,
        >;
        activeUrl: (...options: [options?: undefined]) => Validation<undefined>;
        minLength: (
            ...options: [options: { min: number }],
        ) => Validation<{ min: number }>;
        notSameAs: (
            ...options: [options: { otherField: string }],
        ) => Validation<{ otherField: string }>;
        maxLength: (
            ...options: [options: { max: number }],
        ) => Validation<{ max: number }>;
        vat: (
            ...options: [
                options: VATOptions
                | ((field: FieldContext) => VATOptions),
            ],
        ) => Validation<VATOptions | ((field: FieldContext) => VATOptions)>;
        ipAddress: (
            ...options: [options?: { version: 4 | 6 }],
        ) => Validation<{ version: 4 | 6 } | undefined>;
        creditCard: (
            ...options: [
                options?: | CreditCardOptions
                | ((field: FieldContext) => void | CreditCardOptions | undefined),
            ],
        ) => Validation<
            | CreditCardOptions
            | ((field: FieldContext) => void | CreditCardOptions | undefined)
            | undefined,
        >;
        postalCode: (
            ...options: [
                options?: | PostalCodeOptions
                | ((field: FieldContext) => void | PostalCodeOptions | undefined),
            ],
        ) => Validation<
            | PostalCodeOptions
            | ((field: FieldContext) => void | PostalCodeOptions | undefined)
            | undefined,
        >;
        startsWith: (
            ...options: [options: { substring: string }],
        ) => Validation<{ substring: string }>;
        toUpperCase: (
            ...options: [options?: string | string[]],
        ) => Validation<string | string[] | undefined>;
        toLowerCase: (
            ...options: [options?: string | string[]],
        ) => Validation<string | string[] | undefined>;
        toCamelCase: (...options: [options?: undefined]) => Validation<undefined>;
        fixedLength: (
            ...options: [options: { size: number }],
        ) => Validation<{ size: number }>;
        coordinates: (...options: [options?: undefined]) => Validation<undefined>;
        normalizeUrl: (
            ...options: [options?: Options],
        ) => Validation<Options | undefined>;
        alphaNumeric: (
            ...options: [options?: AlphaOptions],
        ) => Validation<AlphaOptions | undefined>;
        normalizeEmail: (
            ...options: [options?: NormalizeEmailOptions],
        ) => Validation<NormalizeEmailOptions | undefined>;
    } = ...

    Static collection of all available validation rules for strings

    "[SUBTYPE]": string = 'string'

    The subtype identifier for the literal schema field

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

    Unique name identifier for union type resolution

    Methods

    • 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 it.

      Parameters

      • callback: ParseFn

      Returns this

    • Adds a validation rule to the schema's validation chain. Rules are executed in the order they are added.

      Parameters

      Returns this

      This schema instance for method chaining

      vine.string().use(minLength({ length: 3 }))
      vine.number().use(customRule({ strict: true }))
    • Enable/disable bail mode for this field. In bail mode, field validations stop after the first error.

      Parameters

      • state: boolean

        Whether to enable bail mode

      Returns VineString

      This schema instance for method chaining

      vine.string().bail(false) // Continue validation after first error
      vine.number().bail(true) // Stop after first error (default)
    • Compiles the literal schema type into a compiler node. This method transforms the schema definition into a format that the validation compiler can process.

      Parameters

      • propertyName: string

        Name of the property being compiled

      • refs: RefsStore

        Reference store for the compiler

      • options: ParserOptions

        Parser options including camelCase conversion

      Returns FieldNode & {} & { subtype: string }

      Compiled literal node with subtype information

    • Type checker function to determine if a value is a string. Required for "unionOfTypes" functionality.

      Parameters

      • value: unknown

        The value to check

      Returns value is string

      True if the value is a string

    • Validates the value to be a valid URL.

      Parameters

      • ...args: [options?: IsURLOptions]

        Optional URL validation options

      Returns VineString

      This string schema instance for method chaining

    • Validates the value to be a valid email address.

      Parameters

      • ...args: [options?: IsEmailOptions]

        Optional email validation options

      Returns VineString

      This string schema instance for method chaining

    • Validates the value to be a valid mobile phone number for specified locales.

      Parameters

      Returns VineString

      This string schema instance for method chaining

      vine.string().mobile({ locale: ['en-US', 'en-GB'] })
      
    • Validates the value to be a valid IP address (IPv4 or IPv6).

      Parameters

      • Optionalversion: 4 | 6

        Optional IP version (4 for IPv4, 6 for IPv6). Omit to allow both.

      Returns VineString

      This string schema instance for method chaining

      vine.string().ipAddress()     // Allows IPv4 and IPv6
      vine.string().ipAddress(4) // Only IPv4
      vine.string().ipAddress(6) // Only IPv6
    • Validates the value to be a valid hexadecimal color code.

      Returns VineString

      This string schema instance for method chaining

      vine.string().hexCode()  // Accepts #FFF, #FFFFFF, etc.
      
    • Validates the value against a custom regular expression pattern.

      Parameters

      • expression: RegExp

        The regular expression to match against

      Returns VineString

      This string schema instance for method chaining

      vine.string().regex(/^[A-Z]{3}\d{3}$/)  // Matches ABC123 pattern
      
    • Validates the value to contain only alphabetic characters.

      Parameters

      • Optionaloptions: AlphaOptions

        Options to allow spaces, underscores, or dashes

      Returns VineString

      This string schema instance for method chaining

      vine.string().alpha()
      vine.string().alpha({ allowSpaces: true })
      vine.string().alpha({ allowSpaces: true, allowDashes: true })
    • Validates the value to contain only alphanumeric characters (letters and numbers).

      Parameters

      • Optionaloptions: AlphaOptions

        Options to allow spaces, underscores, or dashes

      Returns VineString

      This string schema instance for method chaining

      vine.string().alphaNumeric()
      vine.string().alphaNumeric({ allowSpaces: true, allowUnderscores: true })
    • Enforce a minimum length on a string field.

      Parameters

      • expectedLength: number

        The minimum required length

      Returns VineString

      This string schema instance for method chaining

    • Enforce a maximum length on a string field.

      Parameters

      • expectedLength: number

        The maximum allowed length

      Returns VineString

      This string schema instance for method chaining

    • Enforce a fixed length on a string field.

      Parameters

      • expectedLength: number

        The exact required length

      Returns VineString

      This string schema instance for method chaining

    • Ensures the field is confirmed by having another field with "_confirmation" suffix (or a custom suffix). Useful for password confirmation fields.

      Parameters

      • Optionaloptions: { confirmationField?: string } | { as?: string }

        Optional configuration for the confirmation field name

        • { confirmationField?: string }
          • OptionalconfirmationField?: string

            Use "as" field instead

        • { as?: string }

      Returns VineString

      This string schema instance for method chaining

      // Validates that "password_confirmation" field matches "password"
      vine.string().confirmed()
      // Custom confirmation field name
      vine.string().confirmed({ as: 'passwordConfirm' })
    • Trims leading and trailing whitespace from the string value.

      Returns VineString

      This string schema instance for method chaining

      vine.string().trim()  // "  hello  " becomes "hello"
      
    • Normalizes the email address by applying transformations like lowercasing and removing dots from Gmail addresses.

      Parameters

      • Optionaloptions: NormalizeEmailOptions

        Email normalization options

      Returns VineString

      This string schema instance for method chaining

      vine.string().email().normalizeEmail({ gmail_remove_dots: true })
      
    • Converts the field value to camelCase.

      Returns VineString

      This string schema instance for method chaining

      vine.string().toCamelCase()  // "hello_world" becomes "helloWorld"
      
    • Escapes HTML entities in the string to prevent XSS attacks.

      Returns VineString

      This string schema instance for method chaining

      vine.string().escape()  // "<script>" becomes "&lt;script&gt;"
      
    • Normalizes a URL by applying standardization rules like removing trailing slashes, sorting query parameters, and stripping default ports.

      Parameters

      • ...args: [options?: Options]

      Returns VineString

      This string schema instance for method chaining

      vine.string().url().normalizeUrl({ stripWWW: true })
      
    • Ensures the value starts with a specified substring.

      Parameters

      • substring: string

        The required starting substring

      Returns VineString

      This string schema instance for method chaining

      vine.string().startsWith('https://')
      
    • Ensures the value ends with a specified substring.

      Parameters

      • substring: string

        The required ending substring

      Returns VineString

      This string schema instance for method chaining

      vine.string().endsWith('.com')
      
    • Ensures the value matches the value of another field in the data.

      Parameters

      • otherField: string

        The name of the field to compare with

      Returns VineString

      This string schema instance for method chaining

      vine.string().sameAs('password')
      
    • Ensures the value does not match the value of another field in the data.

      Parameters

      • otherField: string

        The name of the field to compare with

      Returns VineString

      This string schema instance for method chaining

      vine.string().notSameAs('oldPassword')
      
    • Ensures the field's value is one of the predefined choices.

      Parameters

      • choices: string[] | ((field: FieldContext) => string[])

        Array of allowed values or function returning allowed values

      Returns VineString

      This string schema instance for method chaining

      vine.string().in(['red', 'green', 'blue'])
      
      // Dynamic choices based on metadata
      vine.string().in((field) => getUserRoles(field.meta.userId))
    • Ensures the field's value is not in the predefined list.

      Parameters

      • list: string[] | ((field: FieldContext) => string[])

        Array of disallowed values or function returning disallowed values

      Returns VineString

      This string schema instance for method chaining

      vine.string().notIn(['admin', 'root', 'system'])
      
    • Validates the value to be a valid UUID (Universally Unique Identifier).

      Parameters

      • ...args: [options?: { version?: (1 | 2 | 3 | 4 | 5 | 6 | 7 | 8)[] }]

      Returns VineString

      This string schema instance for method chaining

      vine.string().uuid()
      vine.string().uuid({ version: 4 })
    • Validates the value to be a valid ULID (Universally Unique Lexicographically Sortable Identifier).

      Returns VineString

      This string schema instance for method chaining

      vine.string().ulid()
      
    • Validates the value to be a string containing latitude and longitude coordinates.

      Returns VineString

      This string schema instance for method chaining

      vine.string().coordinates()  // Accepts "40.7128,-74.0060"