ensure
Returns a new event definition whose events will first go through the validator before being passed to their target. Events that don't pass the validator won't reach their targets.

Import

1
import ensure from '@redux-beacon/ensure';
Copied!

Syntax

1
ensure(validator, eventDef)
Copied!

Parameters

    validator: Validator
      The function used to validate the event.
    eventDef: EventDefinition
      The event you want to validate.

Validator

1
type Validator = (event: any[]) => boolean;
Copied!
    Accepts an event or array of events as its sole argument.
    Returns true if the event is valid.
    Returns false if the event is not valid.

Example

1
import joi from 'joi';
2
import ensure from '@redux-beacon/ensure'
3
4
const pageview = (action, prevState) => ({
5
hitType: 'pageview',
6
route: action.payload.location.pathname,
7
referrer: prevState.currentRoute,
8
});
9
10
// Returns true if the event matches the schema
11
const isValidPageView = event =>
12
!joi.validate(event, joi.object().keys({
13
hitType: joi.string().only('pageview').required(),
14
page: joi.string().disallow('/404'),
15
title: joi.string(),
16
location: joi.string(),
17
})).error;
18
19
const eventsMap = {
20
LOCATION_CHANGE: ensure(isValidPageView, pageview)
21
};
Copied!