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-fields-body

v1.1.11

Published

Validate-Fields-Body is a Javascript library for dealing with validation.

Downloads

73

Readme

Validate-Fields-Body

This is a JavaScript function that validates a given form data object against a set of validation keys. The function returns an array with three elements:

Installation

npm i validate-fields-body

The first element is a boolean value that indicates whether there are any validation errors or not. The second element is an array of strings that contains the error messages for each validation failure, or null if there are no errors. The third element is an object that contains only the valid fields from the form data, or null if there are any errors. The function performs three types of validation:

Required fields: The validation keys that have a “!” prefix indicate that the corresponding field in the form data must be present and not empty. For example, “!name” means that the name field is required. Required types: The validation keys that have a “:” suffix indicate that the corresponding field in the form data must have a specific type. For example, “age:number” means that the age field must be a number. Matching keys: The validation keys that do not have any prefix or suffix indicate that the corresponding field in the form data must exist. For example, “email” means that the email field must be present. The function loops through each validation key and checks if it matches with the form data. If not, it adds an error message to the logs array and skips to the next key. If all keys are validated successfully, it returns false for errors and copies only the valid fields to the result object.

Here is an example of how to use this function:

Example

const { useValidate } = require("validate-fields-body");

const register = (req, res) => {
  // Set your fields to validate
  const fieldsValidate = ["username", "email", "password", "age", "skills"];

  const [InValid, logs, result] = useValidate(fieldsValidate, req.body);

  if (InValid) return res.status(403).json({ success: false, message: logs });

  return res.status(201).send({ success: true, data: result });
};

Require Value

const fieldsValidate = ["!username", "!email", "!password", "!age", "skills"];

Check Types

const fieldsValidate = [
  "!username:string",
  "!email:string",
  "!password:string",
  "!age:number",
  "skills:object",
];

Check Length

const fieldsValidate = ["password*3-15"];

Check Email

const fieldsValidate = ["email@"];

Options

| Option | Keyword | | ----------- | --------------- | | ! | Required Value | | :[TYPE] | Check Type | | @ | Validate Emai | | *[min-max] | Validate Length |

Contributing

Thanks you!.

Donation

Buy me a coffee :)

USDT Address

0x92072439f5cf2f35defa28a4915004971de543b0

Author by: Nero Sengtianthr.

License

MIT