{"id":17916,"library":"redux-validator","title":"Redux Action Validator Middleware","description":"redux-validator is a middleware for Redux that enables validation of action parameters before they reach the reducers. It works by inspecting the `meta.validator` field of a dispatched action, which defines validation functions and messages for properties within the `action.payload`. If any validation fails, the dispatch is aborted, and an error object containing the `err`, `msg`, `param`, and `id` of the failed validation is returned. The library is compatible with Flux Standard Actions (FSA). As of the provided information, the current version is 0.2.3, suggesting it may be an early-stage or unmaintained project with no active release cadence. Its primary differentiator is the direct integration of validation rules within the action's `meta` field, offering a localized approach to action-specific data integrity checks.","status":"abandoned","version":"0.2.3","language":"javascript","source_language":"en","source_url":"https://github.com/MaxLi1994/redux-validator","tags":["javascript","validator","redux","middleware"],"install":[{"cmd":"npm install redux-validator","lang":"bash","label":"npm"},{"cmd":"yarn add redux-validator","lang":"bash","label":"yarn"},{"cmd":"pnpm add redux-validator","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Redux is required as the core state management library for which this package provides middleware. It's a peer dependency for functionality.","package":"redux","optional":false}],"imports":[{"note":"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.","wrong":"const Validator = require('redux-validator');","symbol":"Validator","correct":"import Validator from 'redux-validator';"}],"quickstart":{"code":"import { createStore, applyMiddleware } from 'redux';\nimport Validator from 'redux-validator';\n\n// A dummy reducer for the example\nconst reducer = (state = { todos: [] }, action) => {\n  switch (action.type) {\n    case 'ADD_TODO':\n      return { ...state, todos: [...state.todos, action.payload] };\n    default:\n      return state;\n  }\n};\n\n// Initialize the validator middleware\nconst validator = Validator();\n\n// Apply middleware, ensure validator is applied first for correctness\nconst createStoreWithMiddleware = applyMiddleware(validator)(createStore);\nconst store = createStoreWithMiddleware(reducer);\n\n// Example valid action\nconst validAction = {\n  type: 'ADD_TODO',\n  payload: {\n    text: 'Learn redux-validator',\n    complete: false\n  },\n  meta: {\n    validator: {\n      text: {\n        func: (text, state, payload) => typeof text === 'string' && text.length > 0,\n        msg: 'Todo text cannot be empty'\n      },\n      complete: {\n        func: (complete, state, payload) => typeof complete === 'boolean',\n        msg: 'Complete status must be a boolean'\n      }\n    }\n  }\n};\n\nconst result1 = store.dispatch(validAction);\nconsole.log('Dispatch result (valid):', result1);\nconsole.log('Current state (after valid):', store.getState());\n\n// Example invalid action\nconst invalidAction = {\n  type: 'ADD_TODO',\n  payload: {\n    text: '', // Invalid: empty string\n    complete: 'no' // Invalid: not a boolean\n  },\n  meta: {\n    validator: {\n      text: {\n        func: (text, state, payload) => typeof text === 'string' && text.length > 0,\n        msg: 'Todo text cannot be empty'\n      },\n      complete: {\n        func: (complete, state, payload) => typeof complete === 'boolean',\n        msg: 'Complete status must be a boolean'\n      }\n    }\n  }\n};\n\nconst result2 = store.dispatch(invalidAction);\nconsole.log('Dispatch result (invalid):', result2);\nconsole.log('Current state (after invalid - should be same as before):', store.getState());","lang":"javascript","description":"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."},"warnings":[{"fix":"Ensure `validator` is the first argument passed to `applyMiddleware`, e.g., `applyMiddleware(validator, otherMiddleware)`.","message":"It is crucial to apply `redux-validator` as the *first* middleware in your `applyMiddleware` chain. Placing it after other middleware that might modify the action could lead to unexpected validation behavior or errors, as the validator expects the original action shape.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Review the GitHub repository for recent activity (commits, issues) to assess its current maintenance status. For critical applications, consider more actively maintained alternatives or be prepared to contribute to its development.","message":"The library is in a very early development state (version 0.2.3). This might imply a lack of active maintenance, potential for unpatched bugs, or incomplete features. Consider its stability and long-term support before relying on it for production applications.","severity":"gotcha","affected_versions":"<=0.2.3"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Examine 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.","cause":"An action was dispatched where one or more validation functions defined in `action.meta.validator` returned `false` for a corresponding `action.payload` property.","error":"{ err: 'validator', msg: 'Cannot add an empty todo', param: 'text', id: 0 }"},{"fix":"Ensure 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.","cause":"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.","error":"Error: Actions may not have an undefined 'type' property."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}