
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
react-changeset
Advanced tools
A react hook based on validated changeset library for form handling
It provides a react hook on top of validated-changeset library (which is base library for ember-changeset
)
It makes forms easy to handle
For full react form codesandbox example, view
isValid
, isDirty
(changed), changes
(what fieds are changed), errors
(error messages) without any extra logicconst user = {
name: '',
email: '',
password: '',
confirmPassword: '',
};
import { useChangeset, lookupValidator } from 'react-changeset';
// without validations
const userChangeset = useChangeset(user);
// with validations
const userChangeset = useChangeset(
user,
lookupValidator(validationMap),
validationMap
);
Example validation map here
<input
type="text"
value={userChangeset.get('name')}
onChange={({ target }) => {
userChangeset.set('name', target.value);
}}
/>
userChangeset.changes;
<input
type="submit"
disabled={!(userChangeset.isDirty && userChangeset.isValid)}
/>
userChangeset.error.username && userChangeset.error.username.validation[0];
For complete changeset API, visit validated-changeset
For full react form example, view
// validations/user-form.js
import {
validateLength,
validateFormat,
validatePresence,
validateConfirmation,
} from 'react-changeset';
const validationMap = {
name: [validateLength({ min: 8 })],
email: [validateFormat({ type: 'email' })],
password: [validateLength({ min: 5 }), validatePresence(true)],
confirmPassword: [validateConfirmation({ on: 'password' })],
};
export default validationMap;
presence
Validates presence/absence of a value.
{
propertyName: validatePresence(true), // must be present
propertyName: validatePresence(false) // must be blank
propertyName: validatePresence({ presence: true }) // alternative option syntax
propertyName: validatePresence({ presence: true, ignoreBlank: true }) // If ignoreBlank true, treats an empty or whitespace string as not present.
propertyName: validatePresence({ presence: true, message: "Property not present" }) // custom error message
}
message
property for custom error messagelength
Validates the length of a String
{
propertyName: validateLength({ min: 1 }), // 1 or more
propertyName: validateLength({ max: 8 }), // up to 8
propertyName: validateLength({ min: 1, max: 8 }), // between 1 and 8 (inclusive)
propertyName: validateLength({ is: 16 }), // exactly 16
}
number
Validates various properties of a number.
{
propertyName: validateNumber({ is: 16 }), // exactly 16
propertyName: validateNumber({ allowBlank: true }), // can be blank
propertyName: validateNumber({ integer: true }), // must be an integer
propertyName: validateNumber({ lt: 10 }), // less than 10
propertyName: validateNumber({ lte: 10 }), // less than or equal to 10
propertyName: validateNumber({ gt: 5 }), // greater than 5
propertyName: validateNumber({ gte: 10 }), // greater than or equal to 10
propertyName: validateNumber({ positive: true }), // must be a positive number
propertyName: validateNumber({ odd: true }), // must be an odd number
propertyName: validateNumber({ even: true }), // must be an even number
propertyName: validateNumber({ multipleOf: 7 }) // must be a multiple of 7
}
format
Validates a String
based on a regular expression.
{
propertyName: validateFormat({ allowBlank: true }), // can be blank
propertyName: validateFormat({ type: 'email' }), // built-in email format
propertyName: validateFormat({ type: 'url' }), // built-in URL format
propertyName: validateFormat({ regex: /\w{6,30}/ }) // custom regular expression
propertyName: validateFormat({ type: 'email', inverse: true }) // passes if the value doesn't match the given format
}
confirmation
Validates that a field has the same value as another.
{
propertyName: validateConfirmation({ on: 'password' }), // must match 'password'
propertyName: validateConfirmation({ allowBlank: true }), // can be blank
}
For writing your own validators, refer 'Writing your own validators' section of ember-changeset-validations
FAQs
A react hook based on validated changeset library for form handling
The npm package react-changeset receives a total of 40 weekly downloads. As such, react-changeset popularity was classified as not popular.
We found that react-changeset 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
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.