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

validate-hook

v0.6.0

Published

React hook

Downloads

16

Readme

npm GitHub repo size NPM Version

Features

Install

npm install validate-hook

Quickstart


import { useValidate } from 'validate-hook';
import { useState } from 'react';

function App() {
  const { validateSingleSync } = useValidate();

  // CREATE AN INPUT WITH VALIDATIONS, ATTRIBUTES AND ERROR ARRAY
  const [input, setInput] = useState({
    validationsSync: (attributes) => [
      {
        conditional: attributes.value.length < 6,
        message: 'At least 6 characters'
      }
    ],
    attributes: { value: ''},
    errors: []
  });


  return (
    <div className="App">
      <input onChange={(e)=> {

        // UPDATE THE ATTRIBUTE
        setInput((prev) => ({...prev, attributes: { ...prev.attributes, value: e.target.value}}));

        // VALIDATE THE INPUT
        setInput((prev) => validateSingleSync(prev));

      }} type="text" value={input.attributes.value}/>
      <div>
        {input.errors.length > 0 ? input.errors[0] : null}
      </div>
    </div>
  );
}

Many Inputs


import { useValidate } from 'validate-hook';

function App() {
  const { validateManySync } = useValidate();

  // CREATE MORE THAN ONE INPUT WITH VALIDATIONS, ATTRIBUTES AND ERROR ARRAY
  const [inputs, setInputs] = useState({
    password: { 
      validationsSync: (attributes) => [
        {
          conditional: attributes.value.length < 6,
          message: 'At least 6 characters'
        }
      ],
      attributes: { value: ''},
      errors: [] 
    },
    username: {
        validationsSync: (attributes) => [
          {
            conditional: attributes.value.length < 6,
            message: 'At least 6 characters'
          }
      ],
      attributes: { value: ''},
      errors: []
    }
  });

  // UPDATE THEM
  function onChange(e, key){
    setInputs((prev) => {
      const newAtt = { ...prev[key].attributes, value: e.target.value };
      const newInput = {...prev, [key]: { ...prev[key], attributes: newAtt }}
      return newInput;
    })
  }

  return (
    <div className="App">
      <input id="username" onChange={(e) => onChange(e, 'username')} type="text" value={inputs.username.attributes.value}/>
      <input id="password" onChange={(e) => onChange(e, 'password')} type="text" value={inputs.password.attributes.value}/>
      <div>
        {/* CHECK THEM AT THE SAME TIME */}
        {!validateManySync(inputs) ? 'invalid form' : null}
      </div>
    </div>
  );
}

Async Validations


import { useValidate } from 'validate-hook';
import { useState } from 'react';

function App() {
  const { validateSingle } = useValidate();

  // CREATE AN INPUT WITH VALIDATIONS, ATTRIBUTES AND ERROR ARRAY
  const [input, setInput] = useState({
    validations: async (attributes) => [
      {
        // VALIDATE THE REQUIRED ATTRIBUTE
        conditional: !(await onCheckInputOnServer(attributes.value)),
        message: 'Value is invalid'
      }
    ],
    attributes: { value: ''},
    errors: []
  });

  // CREATE YOUR ASYNC VALIDATION METHOD
  async function onCheckInputOnServer(value){
    const response = await fetch('url', { method: 'POST', body: value })
    const parsedResponse = await response.json();
    if(parsedResponse.ok) return true;
    return false;
  }

  return (
    <div className="App">
      <input onChange={async (e)=> {

        // UPDATE THE INPUT
        const updatedInput = {...input, attributes: { ...input.attributes, value: e.target.value }}
        setInput(updatedInput);

        // VALIDATE AND UPDATE THE INPUT
        const validatedInput = await validateSingle(updatedInput)
        setInput(validatedInput);
      }} type="text" value={input.attributes.value}/>
      <div>
        {input.errors.length > 0 ? input.errors[0] : null}
      </div>
    </div>
  );
}