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 ensure from '@redux-beacon/ensure';
ensure(validator, eventDef)
validator
: Validator
The function used to validate the event.
eventDef
: EventDefinition
The event you want to validate.
type Validator = (event: any[]) => boolean;
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.
import joi from 'joi';import ensure from '@redux-beacon/ensure'const pageview = (action, prevState) => ({hitType: 'pageview',route: action.payload.location.pathname,referrer: prevState.currentRoute,});// Returns true if the event matches the schemaconst isValidPageView = event =>!joi.validate(event, joi.object().keys({hitType: joi.string().only('pageview').required(),page: joi.string().disallow('/404'),title: joi.string(),location: joi.string(),})).error;const eventsMap = {LOCATION_CHANGE: ensure(isValidPageView, pageview)};