Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
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';
class LoginRequestValidation extends Validator {
rules = {
username: Joi.string(),
password: Joi.string(),
};
accept(store, action){
console.log(`${action.type} accepted!`);
store.dispatch({
type: 'LOGIN_ACCEPTED',
payload: 'credentials are valid',
});
}
reject(store, action){
console.log(`${action.type} rejected!`);
store.dispatch({
type: 'LOGIN_REJECTED',
payload: 'credentials are invalid',
});
}
}
export default assign('LOGIN_REQUEST')(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 1 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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.