
Security News
NVD Quietly Sweeps 100K+ CVEs Into a “Deferred” Black Hole
NVD now marks all pre-2018 CVEs as "Deferred," signaling it will no longer enrich older vulnerabilities, further eroding trust in its data.
react-action-validation
Advanced tools
Validates the payload of a dispatched action
You can use the validation using the validationGate middleware or by creating an instance of the validator if you want to use it out of the context of the redux.
npm install --save react-action-validation
Making validators is easy, you create a class which is extended from to base Validator class, and add the rules property and write your own rules. be sure you already installed @hapi/joi.
npm install --save @hapi/joi
// validations/auth/LoginRequest.js
import Joi from '@hapi/joi';
import { Validator, assign } from 'react-action-validation';
import authTypes from 'path/to/auth/types'; // path to the files that you wrote your action types
class LoginRequestValidation extends Validator {
rules = {
username: Joi.string(),
password: Joi.string(),
};
}
export default assign(authTypes.LOGIN)(LoginRequestValidation);
Note: the assign function accepts array to assign the validation to multiple types
export default assign([FOO_TYPE, BAR_TYPE])(CustomValidation);
Note: I recommend you to separate the validation directories from your component and other layers of your app. so in this example, the validators are in a directory called validations in the root of my app.
To register al the validators to the validationGate middleware, you should pack them all in an object and export it.
// configureValidation.js
import FooValidation from 'path/to/FooValidation';
import BarValidation from 'path/to/BarValidation';
export default {
...FooValidation,
...BarValidation
};
First of all you need to add the validationGate to your middlewares.
// ...
import { createStore, applyMiddleware } from 'redux';
import createSagaMiddleware from 'redux-saga';
// ...
import { validationGate } from 'react-action-validation';
// ...
import validations from 'path/to/configureValidation';
// ...
const validator = validationGate(validations);
// ...
const middlewares = [
validator,
// other middlewares
]
const store = createStore(
reducer,
applyMiddleware(middlewares)
)
// rest unchanged
You're also able to use it without any custom validation classes and middleware, you can simply create an instance from the Validator class and pass your rules.
import React, {useState} from 'react';
import Joi from '@hapi/joi';
import { Validator } from 'react-action-validation'
const MyFormComponent = props => {
const [name, setName] = useState('');
const [message, setMessage] = useState('');
const handleSubmit = () => {
const rules = {
name: Joi.string(),
}
const validator = new Validator(rules);
validator.validate({
name
})
.then(() => setMessage('name is valid'))
.catch(() => setMessage('name is invalid'));
}
return (
<form onSubmit={handleSubmit} {...props}>
<input
placeholder="enter your name"
value={name}
onChange={e => setName(e.target.value)}
/>
<button type="submit">submit</button>
{!!message && <p>{message}</p>}
</form>
);
};
export default MyFormComponent;
FAQs
validates the payload of the dispatched action
The npm package react-action-validation receives a total of 5 weekly downloads. As such, react-action-validation popularity was classified as not popular.
We found that react-action-validation demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NVD now marks all pre-2018 CVEs as "Deferred," signaling it will no longer enrich older vulnerabilities, further eroding trust in its data.
Research
Security News
Lazarus-linked threat actors expand their npm malware campaign with new RAT loaders, hex obfuscation, and over 5,600 downloads across 11 packages.
Security News
Safari 18.4 adds support for Iterator Helpers and two other TC39 JavaScript features, bringing full cross-browser coverage to key parts of the ECMAScript spec.