> For the complete documentation index, see [llms.txt](https://rangle.gitbook.io/redux-beacon/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rangle.gitbook.io/redux-beacon/index-3/ensure.md).

# 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

```javascript
import ensure from '@redux-beacon/ensure';
```

## Syntax

```javascript
ensure(validator, eventDef)
```

### Parameters

* `validator`: [`Validator`](/redux-beacon/index-3/ensure.md#validator)
  * The function used to validate the event.
* `eventDef`: [`EventDefinition`](/redux-beacon/index-1/event-definition.md)
  * The event you want to validate.

### Validator

```typescript
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.

## Example

```javascript
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 schema
const 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)
};
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://rangle.gitbook.io/redux-beacon/index-3/ensure.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
