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

zod-password-validation-schema

v0.0.2

Published

An opinionated, reusable, and adaptable Zod schema that can be used to validate password form fields in the most popular form libraries. See usage notes below for implementation specifics for React Hook Form, Mantine Form, Formik, and React Final Form.

Downloads

4

Readme

Zod Password Validation Schema

An opinionated, reusable, and adaptable Zod schema that can be used to validate password form fields in the most popular form libraries. See usage notes below for implementation specifics for React Hook Form, Mantine Form, Formik, and React Final Form.

NPM Install:

npm install zod-password-validation-schema

Yarn Install:

yarn add zod-password-validation-schema

Schema Details

  • [x] Requires two form fields named password and confirmPassword
  • [x] Password has a min length of 6 characters
  • [x] Password has at least 1 uppercase character
  • [x] Password has at least 1 lowercase character
  • [x] Password has at least 1 number
  • [x] Password has at least 1 special character (!@#$%^&*()_-+={[}]|:;"'<,>.)

Usage

Universal Requirement

Once installed via npm or yarn, import dependency into your component

import { customZodSchema } from "zod-password-validation-schema"

React Hook Form

Install react-hook-form (if not already) along side the react-hook-form zod resolver

yarn add react-hook-form @hookform/resolvers/zod

Import these dependencies in the same component you imported the schema

import { useForm } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"

Supply the schema to react-hook-form's useForm hook, including default values

const { control, handleSubmit } = useForm({
  defaultValues: {
    password: "",
    confirmPassword: "",
  },
  resolver: zodResolver(customZodSchema),
})

React-hook-form requires a control={control} prop to be added to your form fields

<PasswordInput
  name="confirmPassword"
  label="Confirm password"
  control={control}
/>

Mantine

Install Mantine form (if not already) along side the Mantine form zod resolver

yarn add @mantine/form mantine-form-zod-resolver

Import these dependencies in the same component you imported the schema

import { useForm } from "@mantine/form"
import { zodResolver } from "mantine-form-zod-resolver"

Supply the schema to Mantine's useForm hook, including initial values

const form =
  useForm <
  z.infer <
  typeof customZodSchema >>
    {
      initialValues: {
        password: "",
        confirmPassword: "",
      },
      validate: zodResolver(customZodSchema),
    }

Mantine form requires a the field's input props to be spread on the input

<PasswordInput label="Password" {...form.getInputProps("password")} />

Formik

Install Formik (if not already) along side the Formik form zod adapter

yarn add formik zod-formik-adapter

Import these dependencies in the same component you imported the schema

import { Formik, Form } from "formik"
import { toFormikValidate } from "zod-formik-adapter"

Supply the schema to Formik's validate prop. including initial values prop as well.

<Formik
  initialValues={{
    password: "",
    confirmPassword: "",
  }}
  validateOnChange={true}
  validate={toFormikValidate(customZodSchema)}
  onSubmit={submitForm}
>
  {({
    values,
    errors,
    touched,
    handleChange,
    handleBlur,
    handleSubmit,
  }) => (
    [...form fields]
  )}
</Formik>

Formik passes down data to form elements

<PasswordInput
  label="Password"
  onChange={handleChange("password")}
  onBlur={handleBlur}
  value={values.password}
  name="password"
/>

React Final Form

Install React Final Form (if not already)

yarn add react-final-form

Import these dependencies in the same component you imported the schema

import { Form, Field } from "react-final-form"

Create a generic handler

const formValidator = <T extends z.ZodType<any, any>>(schema: T) =>
  (values: any) => {
    try {
      schema.parse(values)
      return {}
    } catch (err) {
      return (err as z.ZodError).formErrors.fieldErrors
    }
  }

Supply the schema to our generic handler within the validate prop. including initial values prop as well.

<Form
  onSubmit={submitForm}
  validate={formValidator(customZodSchema)}
  render={() => (
  )}>
  [...form fields]
</Form>

Within the render prop of React Final Form's <Form> element, add your inputs inside their <Field> component.

<Field name="password">
  {({ input, meta }) => (
    <PasswordInput
      label="Password"
      error={meta.touched && meta.error ? meta.error[0] : null}
      {...input}
    />
  )}
</Field>