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

light-validation

v1.0.20

Published

A lightweight validation package that helps validate fields from requests.

Downloads

346

Readme

Light Validation

This npm package has just been created. In the future, we plan to add more validation rules and features to enhance its functionality. Stay tuned for updates!

Light Validation is a lightweight JavaScript validation library inspired by Zod, providing flexible validation rules for strings, integers, email, password and object schemas. Use string(), integer(), email(), file(), password() and object() to perform custom validations on your data, with easy-to-read error messages.

Installation

To install this package, use npm:

npm install light-validation

API

1. StringValidator

Use string() to apply rules such as minimum/maximum length. Errors are optional, so if you don't want an error message, you can skip providing one.

Rules:

  • min(length, options): Validates that the string length is at least length characters.
  • max(length, options): Validates that the string length is at most length characters.
  • nullable(): Allows the string to be null or an empty string, making it a valid input when set.
  • validate(value, options): Runs all applied rules on the string and returns either the validated data if all rules pass, or an error if any rule fails. If you are not using a schema, you can pass the fieldName as an option to attach the fieldName to error message.
import v from 'light-validation';

const result = v.validate.min(7)
.("Almant",{fieldName:'Name'});

console.log(result); //  {valid: false, errors: [ 'Name must be at least 5 characters long.' ]}

2. IntegerValidator

Use integer() to validate integer values based on minimum, maximum, and positive value constraints.

Rules:

  • min(minValue, options): Ensures the integer is greater than or equal to minValue.
  • max(maxValue, options): Ensures the integer is less than or equal to maxValue.
  • nullable(): Allows the string to be null or an empty string, making it a valid input when set.
  • positive(error): Ensures the integer is greater than 0.
  • validate(value, options): Runs all applied rules on the integer and returns either the validated data if all rules pass, or an error if any rule fails. If you are not using a schema, you can pass the fieldName as an option to attach the fieldName to error message.
import v from 'light-validation';

const result = v.integer().min(18)
.validate(17,{fieldName:'Age'})

console.log(result); // { valid: false, errors: [ 'Age must be greater than or equal to 18' ] }

//Or you can define your custom error message

const result1 = v.integer().min(18,{message:'You must be at least 18 years old'})
.validate(17,{fieldName:'Age'})

console.log(result1); // { valid: false, errors: [ 'You must be at least 18 years old' ] }

3. EmailValidator

Use email() to validate email addresses with built-in checks for proper email format.

Rules:

  • nullable(): Allows the string to be null or an empty string, making it a valid input when set.
  • validate(value, options): Runs all applied rules on the integer and returns either the validated data if all rules pass, or an error if any rule fails. If you are not using a schema, you can pass the fieldName as an option to attach the fieldName to error message.
import v from 'light-validation';

const result = v.email().validate('[email protected]')

console.log(result); // { valid: true, data: '[email protected]' }

4. PasswordValidation

Use password() to validate passwords with requirements like minimum length, inclusion of numbers, symbols, etc.

Rules:

  • containsNumber(): Ensures the password contains at least one numeric digit.
  • containsSpecialChar(): Ensures the password contains at least one special character.
  • containsUppercase(): Ensures the password contains at least one uppercase letter.
  • min(): Ensures the password has a minimum length.
  • nullable(): Allows the string to be null or an empty string, making it a valid input when set.
  • validate(value, options): Runs all applied rules on the integer and returns either the validated data if all rules pass, or an error if any rule fails. If you are not using a schema, you can pass the fieldName as an option to attach the fieldName to error message.
import v from 'light-validation';

const password = v.password().min(8)
.containsNumber()
.containsSpecialChar()
.validate('1234567',{fieldName:"Password"});

console.log(result);  //{
//   valid: false,
//   errors: [
//     'Password must be at least 8 characters long.',
//     'Password must contain at least one special character.'
//   ]
// }

5. FileValidation

Use file() to validate file uploads based on type, size, and other properties.

Rules:

  • type(mimeType, error): Ensures the file is of the specified MIME type (e.g., 'jpeg, jpg, png, ect...').
  • size(maxSize, error): Ensures the file is not larger than maxSize.
  • nullable(): Allows the string to be null or an empty string, making it a valid input when set.
  • validate(value, options): Runs all applied rules on the integer and returns either the validated data if all rules pass, or an error if any rule fails. If you are not using a schema, you can pass the fieldName as an option to attach the fieldName to error message.
import v from 'light-validation';

const file = {
  type: 'image/jpg',
  size: 50000000, // 5MB
};

// Validate the file with specific rules:
const image = v
  .file()  // Start the validation chain for file
  .type(['jpg'])  // Validate that the file is of type 'jpg'
  .maxSize(5)  // Validate that the file pass it as MB
  .validate(file, { fieldName: 'avatar' });  // Perform validation on the 'file' object with custom field name 'avatar'

console.log(image); 
// This will log the result of the validation. If everything is fine, it will return:
// { valid: true, data: file }
// Otherwise, it will return errors based on the validation rules.

6. ObjectSchema

Use object() to validate objects against a predefined schema. Each field in the schema is associated with a validator that provides specific validation rules and methods.

  • parseData(data): Validates the object against the schema and returns either an object data with success if all rules pass or an error if any rule fails.
import v from 'light-validation';

 const userSchema = v.object({
    username: v.string().min(3),
    email: v.email(),
    password: v.password().min(8).containsSpecialChar(),
    age: v.integer().min(18),
  });
  
  const result = userSchema.parseData({
    username: "john",
    email: "[email protected]",
    password: "password1",
    age: 20
  });

console.log(result); 
// Expected output:
/*
{
  valid: false,
  errors: { password: 'password must contain at least one special character.' }
}
*/