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-extended-validators

v2.2.17

Published

Extended validators for zod.

Downloads

81

Readme

Zod Extended Validators

Zod Extended Validators is a TypeScript library enhancing Zod, a popular schema validation library, with additional validation functionalities. For more information, check the npm package page and the GitHub repository.

Installation

To integrate Zod Extended Validators into your project, use npm or yarn:

Using npm:

npm install zod-extended-validators

Using yarn:

yarn add zod-extended-validators

Quick Example

Usage:

import { z } from "zod";
import { textField, requiredNumberField } from "zod-extended-validators";

const schema = z.object({
  name: textField(),
  age: requiredNumberField(),
});

// Example data
const data = { name: "John Doe", age: 30 };

// Validation
const validationResult = schema.safeParse(data);

if (validationResult.success) {
  console.log("Validation passed:", validationResult.data);
} else {
  console.log("Validation errors:", validationResult.error.issues);
}

Result:

Validation passed: { "name": "John Doe", "age": 30 }

Available Validators

Here are some of the validators provided by Zod Extended Validators:

Basic Field Validators

  • textField(): Returns an optional string schema, suitable for text inputs.
  • numberField(): Returns an optional number schema, used for numeric inputs.
  • booleanField(): Returns an optional boolean schema, typically for checkboxes or toggle inputs.

Required Field Validators

  • requiredTextField(customError?): Ensures a string field is not empty, with a custom error option.
  • requiredNumberField(customError?): Validates a number field, ensuring it's provided, with custom error handling.
  • requiredIntegerField(customError?): Similar to requiredNumberField but ensures the number is an integer.
  • requiredBooleanField(customError?): Validates a boolean field, ensuring it's true (useful for agreements or confirmations).
  • requiredOnlyNumberPostalCodeField(customError?): Ensures that the postal code field is not empty and contains only numeric characters.

Specialized Field Validators

  • requiredPhoneNumberField(customError?): Ensures a minimum length for phone numbers.
  • requiredPositiveNumberField(customError?): Checks for positive numbers, either integers or decimals.
  • requiredPercentageField(customError?): Validates percentages, ensuring values are within a valid range.
  • emailField(customError?): Validates email addresses.
  • passwordField(customError?): Validates passwords with specific complexity requirements.
  • requiredPostalCodeField(customError?): Validates postal codes with a specific length requirement that are not required.

Regex-Based Field Validators

  • onlyNumbersLettersCommasPointsField(customError?): Validates input against a regex allowing only specific characters.
  • onlyNumbersLettersHyphenField(customError?): Similar to the above but includes hyphens.
  • IBANField(customError?): Validates International Bank Account Numbers (IBAN) using a comprehensive regex.
  • BICField(customError?): Validates Bank Identifier Codes (BIC).
  • salesTaxIdField(customError?): Validates various formats of sales tax IDs.
  • onlyNumberPostalCodeField(customError?): Validates postal codes to ensure they contain only numeric characters.

Additional Utility Validators

  • withMaxWords(zodField, maxWords, customError?): Extends a Zod string field to limit the number of words.

Each of these validators can be imported and used to create robust, fine-tuned validation schemas for your TypeScript applications, ensuring data integrity and user input validation according to specific rules and formats.

Contributing

Contributors should adhere to guidelines such as clean, documented, and tested code, following the existing style, and updating the README as needed.

License

The library is available under the MIT License.