Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Validator
✓ import Validator from 'redux-validator';
✗ const Validator = require('redux-validator');
The library primarily exports a default function for creating the middleware. While CommonJS `require` might work in some transpiled environments, `import` is the idiomatic way for modern JavaScript/TypeScript.
This quickstart demonstrates how to set up `redux-validator` middleware and define validation rules within an action's `meta` field. It shows both successful and aborted dispatches based on validation outcomes.
import { createStore, applyMiddleware } from 'redux';
import Validator from 'redux-validator';
// A dummy reducer for the example
const reducer = (state = { todos: [] }, action) => {
switch (action.type) {
case 'ADD_TODO':
return { ...state, todos: [...state.todos, action.payload] };
default:
return state;
}
};
// Initialize the validator middleware
const validator = Validator();
// Apply middleware, ensure validator is applied first for correctness
const createStoreWithMiddleware = applyMiddleware(validator)(createStore);
const store = createStoreWithMiddleware(reducer);
// Example valid action
const validAction = {
type: 'ADD_TODO',
payload: {
text: 'Learn redux-validator',
complete: false
},
meta: {
validator: {
text: {
func: (text, state, payload) => typeof text === 'string' && text.length > 0,
msg: 'Todo text cannot be empty'
},
complete: {
func: (complete, state, payload) => typeof complete === 'boolean',
msg: 'Complete status must be a boolean'
}
}
}
};
const result1 = store.dispatch(validAction);
console.log('Dispatch result (valid):', result1);
console.log('Current state (after valid):', store.getState());
// Example invalid action
const invalidAction = {
type: 'ADD_TODO',
payload: {
text: '', // Invalid: empty string
complete: 'no' // Invalid: not a boolean
},
meta: {
validator: {
text: {
func: (text, state, payload) => typeof text === 'string' && text.length > 0,
msg: 'Todo text cannot be empty'
},
complete: {
func: (complete, state, payload) => typeof complete === 'boolean',
msg: 'Complete status must be a boolean'
}
}
}
};
const result2 = store.dispatch(invalidAction);
console.log('Dispatch result (invalid):', result2);
console.log('Current state (after invalid - should be same as before):', store.getState());
Errors
Common errors & fixes
{ err: 'validator', msg: 'Cannot add an empty todo', param: 'text', id: 0 }
An action was dispatched where one or more validation functions defined in `action.meta.validator` returned `false` for a corresponding `action.payload` property.
fixExamine the `msg` and `param` fields in the returned error object to identify the specific validation failure. Adjust the action's `payload` data to meet the defined validation criteria before dispatching.
Error: Actions may not have an undefined 'type' property.
The `redux-validator` middleware might be incorrectly ordered, leading to an action being dispatched or modified by another middleware before validation, resulting in an unexpected action shape or an aborted dispatch that doesn't return the expected error object.
fixEnsure that `redux-validator` is the very first middleware in your `applyMiddleware` call. This allows it to inspect and potentially abort the dispatch before other middlewares process or expect a fully valid action.
Audit
Dependencies
reduxrequiredRedux is required as the core state management library for which this package provides middleware. It's a peer dependency for functionality.