npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-changeset

v1.0.1

Published

A react hook based on validated changeset library for form handling

Downloads

18

Readme

React Changeset

It provides a react hook on top of validated-changeset library (which is base library for ember-changeset)

It makes forms easy to handle

Quick Start

For full react form codesandbox example, view

Features

  1. Check if form isValid, isDirty(changed), changes(what fieds are changed), errors(error messages) without any extra logic
  2. Define validations for each form field in declarative way (based on ember-changeset-validations)

Steps to use

  1. Define initial values of your form with all fields
const user = {
  name: '',
  email: '',
  password: '',
  confirmPassword: '',
};
  1. Pass it to changeset hook to get changeset
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

  1. Bind changeset to react form fields
<input
  type="text"
  value={userChangeset.get('name')}
  onChange={({ target }) => {
    userChangeset.set('name', target.value);
  }}
/>
  1. Enjoy changeset features, like
  • Get only changed fields to send for PUT/PATCH request
userChangeset.changes;
  • Enable/Disable submit button
<input
  type="submit"
  disabled={!(userChangeset.isDirty && userChangeset.isValid)}
/>
  • Show errors for each field
userChangeset.error.username && userChangeset.error.username.validation[0];

For complete changeset API, visit validated-changeset

For full react form example, view

Validation Map

// 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;

Available Validation Functions

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
}
  • All validators accepts message property for custom error message

length

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
}

Custom Validators

For writing your own validators, refer 'Writing your own validators' section of ember-changeset-validations