ow
Options
All
  • Public
  • Public/Protected
  • All
Menu

Hierarchy

  • Predicate<object>
    • ObjectPredicate

Implements

  • BasePredicate<object>

Index

Accessors

empty

  • get empty(): this

nonEmpty

  • get nonEmpty(): this

not

  • get not(): this

plain

  • get plain(): this

Methods

Protected addValidator

  • addValidator(validator: Validator<object>): this
  • Register a new validator.

    Parameters

    • validator: Validator<object>

      Validator to register.

    Returns this

deepEqual

  • deepEqual(expected: object): this
  • Test an object to be deeply equal to the provided object.

    Parameters

    • expected: object

      Expected object to match.

    Returns this

deepValuesOfType

  • deepValuesOfType<T>(predicate: Predicate<T>): this
  • Test all the values in the object deeply to match the provided predicate.

    Type parameters

    • T

    Parameters

    • predicate: Predicate<T>

      The predicate that should be applied against every value in the object.

    Returns this

exactShape

  • exactShape(shape: Shape): this
  • Test an object to match the shape exactly. This means that will fail if it comes across unexpected properties. The shape comparison is deep.

    The shape is an object which describes how the tested object should look like. The keys are the same as the source object and the values are predicates.

    example
    import ow from 'ow';
    
    ow({unicorn: '🦄'}, ow.object.exactShape({
    unicorn: ow.string
    }));

    Parameters

    • shape: Shape

      Shape to test the object against.

    Returns this

hasAnyKeys

  • hasAnyKeys(...keys: readonly string[]): this
  • Test an object to include any of the provided keys. You can use dot-notation in a key to access nested properties.

    Parameters

    • Rest ...keys: readonly string[]

      The keys that could be a key in the object.

    Returns this

hasKeys

  • hasKeys(...keys: readonly string[]): this
  • Test an object to include all the provided keys. You can use dot-notation in a key to access nested properties.

    Parameters

    • Rest ...keys: readonly string[]

      The keys that should be present in the object.

    Returns this

instanceOf

  • instanceOf(instance: Function): this
  • Test an object to be of a specific instance type.

    Parameters

    • instance: Function

      The expected instance type of the object.

    Returns this

is

  • is(validator: (value: object) => boolean | string): this
  • 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: object) => boolean | string

      Validation function.

        • (value: object): boolean | string
        • Parameters

          • value: object

          Returns boolean | string

    Returns this

message

  • 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<object>

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

    Returns this

partialShape

  • partialShape(shape: Shape): this
  • Test an object to match the shape partially. This means that it ignores unexpected properties. The shape comparison is deep.

    The shape is an object which describes how the tested object should look like. The keys are the same as the source object and the values are predicates.

    example
    import ow from 'ow';
    
    const object = {
    unicorn: '🦄',
    rainbow: '🌈'
    };
    
    ow(object, ow.object.partialShape({
    unicorn: ow.string
    }));

    Parameters

    • shape: Shape

      Shape to test the object against.

    Returns this

validate

  • 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

    Returns this

valuesOfType

  • valuesOfType<T>(predicate: BasePredicate<T>): this
  • Test all the values in the object to match the provided predicate.

    Type parameters

    • T

    Parameters

    • predicate: BasePredicate<T>

      The predicate that should be applied against every value in the object.

    Returns this