Class ArrayPredicate<T>

Type Parameters

  • T = unknown

Hierarchy

  • Predicate<T[]>
    • ArrayPredicate

Accessors

Methods

  • Test if the elements in the array exactly matches the elements placed at the same indices in the predicates array.

    Example

    ow(['1', 2], ow.array.exactShape([ow.string, ow.number]));
    

    Parameters

    • predicates: Predicate<unknown>[]

      Predicates to test the array against. Describes what the tested array should look like.

    Returns ArrayPredicate<T>

  • Test an array to include all the provided elements. The values are tested by identity, not structure.

    Parameters

    • Rest ...searchElements: readonly T[]

      The values that should be included in the array.

    Returns ArrayPredicate<T>

  • Test an array to include any of the provided elements. The values are tested by identity, not structure.

    Parameters

    • Rest ...searchElements: readonly T[]

      The values that should be included in the array.

    Returns ArrayPredicate<T>

  • Test if the value matches a custom validation function. The validation function should return true if the value passes the function. If the function either returns false or a string, the function fails and the string will be used as error message.

    Parameters

    • validator: ((value: T[]) => string | boolean)

      Validation function.

        • (value: T[]): string | boolean
        • Parameters

          • value: T[]

          Returns string | boolean

    Returns ArrayPredicate<T>

  • Provide a new error message to be thrown when the validation fails.

    Example

    ow('🌈', 'unicorn', ow.string.equals('🦄').message('Expected unicorn, got rainbow'));
    //=> ArgumentError: Expected unicorn, got rainbow

    Example

    ow('🌈', ow.string.minLength(5).message((value, label) => `Expected ${label}, to have a minimum length of 5, got \`${value}\``));
    //=> ArgumentError: Expected string, to be have a minimum length of 5, got `🌈`

    Parameters

    • newMessage: string | ValidatorMessageBuilder<T[]>

      Either a string containing the new message or a function returning the new message.

    Returns ArrayPredicate<T>

  • Test all elements in the array to match to provided predicate.

    Example

    ow(['a', 1], ow.array.ofType(ow.any(ow.string, ow.number)));
    

    Type Parameters

    • U

    Parameters

    • predicate: BasePredicate<U>

      The predicate that should be applied against every individual item.

    Returns ArrayPredicate<U>

  • Test an array to start with a specific value. The value is tested by identity, not structure.

    Parameters

    • searchElement: T

      The value that should be the start of the array.

    Returns ArrayPredicate<T>

  • Test if the value matches a custom validation function. The validation function should return an object containing a validator and message. If the validator is false, the validation fails and the message will be used as error message. If the message is a function, the function is invoked with the label as argument to let you further customize the error message.

    Parameters

    • customValidator: CustomValidator<T[]>

      Custom validation function.

    Returns ArrayPredicate<T>