New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

jevalide

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jevalide

Jevalide is a user-friendly JavaScript library for data validation.

latest
Source
npmnpm
Version
0.0.4-dev.0
Version published
Maintainers
0
Created
Source

Jevalide

Tired of writing complex validation logic? Jevalide is your solution for clean, maintainable form validation that just works.

Why Choose Jevalide?

Write your validation rules once and use them everywhere. Jevalide is a lightweight validation library that works seamlessly in both browser and Node.js environments. Whether you're building a complex web application or a simple Node.js service, Jevalide has got you covered.

No complexity - just simple, powerful validation when you need it.

Installation

Using npm:

npm install jevalide

Quick Start

Jevalide simplifies validation with minimal setup. Here's how it works:

Simple Validation with validate

The Jevalide.validate method is perfect for quickly validating data without additional configuration:

// Import Jevalide
import { Jevalide } from 'jevalide';

const data = {
  email: 'test@example.com',
  password: '12345'
};

const rules = {
  email: ['required', 'email'],
  password: ['required', 'minlength:8']
};

const validator = Jevalide.validate(data, rules);

if (validator.isValid()) {
  console.log('Validation passed!');
} else {
  console.log(validator.getErrors());
  // Output: { password: 'Password must be at least 8 characters' }
}

Customize with init

The Jevalide.init method allows you to set up global configurations, such as custom rules, messages, and locales:

// Initialize with custom options
const validator = Jevalide.init({
  rules: {
    customRule: (value) => ({
      passes: /^[a-zA-Z]+$/.test(value),
      value
    })
  },
  messages: {
    required: 'This field is required',
    email: 'Please enter a valid email',
    customRule: 'Only letters are allowed'
  },
  local: 'en'
});

Validate Forms

Effortlessly validate entire forms:

const form = validator.form({
  email: ['required', 'email'],
  password: ['required', 'minlength:8']
}, {
  email: 'user@example.com',
  password: '12345'
});

if (form.passes()) {
  console.log('All good!');
} else {
  console.log(form.getErrors());
  // Output: { password: 'Password must be at least 8 characters' }
}

Input Validation

Handle single input validation:

const emailValidator = validator.input({
  name: 'email',
  rules: ['required', 'email']
});

emailValidator.setValue('invalid-email');
console.log(emailValidator.getError());
// Output: 'Please enter a valid email'

Custom Rules Made Easy

Add your own validation logic:

validator.rule('username', (value) => ({
  passes: /^[a-zA-Z0-9_]+$/.test(value),
  value
}), 'Username can only contain letters, numbers, and underscores');

Support for Multiple Languages

Customize messages for different locales:

validator.translate('fr', {
  required: 'Ce champ est requis',
  email: 'Veuillez entrer une adresse email valide'
});

validator.setLocale('fr');

Usage in Different Environments

Node.js

For CommonJS environments:

const { Jevalide } = require('jevalide');

Browser

For browser environments, simply include Jevalide in your HTML:

<script>
  const validator = Jevalide.init({
    messages: {
      required: 'This field is required',
      email: 'Please enter a valid email'
    }
  });

  document.querySelector('form').addEventListener('submit', (e) => {
    e.preventDefault();

    const form = validator.form({
      email: ['required', 'email'],
      password: ['required', 'minlength:8']
    }, {
      email: document.querySelector('input[name="email"]').value,
      password: document.querySelector('input[name="password"]').value
    });

    if (form.passes()) {
      // Submit the form
    } else {
      console.log(form.getErrors());
    }
  });
</script>

Some Built-In Rules

NameDescription
requiredEnsures the field is not empty. Works with strings, arrays, objects, numbers, booleans, etc.
Example: required
nullableAlways passes; effectively makes a field optional.
Example: nullable
in:x,yChecks if the input value is among the comma-separated list (x, y, etc.).
Example: required|in:admin,user,guest
size:xIf it’s a file, checks file size. Otherwise checks the exact length for strings.
Example: required|size:5
booleanChecks if the input is a boolean or a “boolean-like” string ("true","false","yes","no","1","0").
Example: required|boolean
between:x,yGeneral “range” check for numbers, strings, dates, or file size. Ensures the value is between x and y.
Example: required|between:18,99
regex:patternValidates that the input matches the given regular expression.
Example: regex:^[0-9]{3}-[0-9]{4}$
only:string / only:digitRestricts the input to either letters only (for "string") or strictly digits (for "digit").
Example: only:string or only:digit
digit:xChecks if the input is purely digits and exactly x digits long.
Example: digit:5
maxDigit:xChecks if the input is purely digits and at most x digits long.
Example: maxDigit:10
minDigit:xChecks if the input is purely digits and at least x digits long.
Example: minDigit:3
equal:valueEnsures the input is strictly equal (===) to the given value.
Example: equal:secret
same:fieldValueEnsures the input loosely equals (==) another field’s value (useful for confirmations).
Example: same:password
objectChecks if the input is a valid object (not null or an array). Optionally enforce required keys.
Example: required|object:name,age
jsonChecks if the input is valid JSON. Optionally ensure required keys or indexes.
Example: json:key1,key2
arrayChecks if the input is an array; optionally validate certain indexes.
Example: array:0,1
emailValidates if the input is a valid email address.
Example: required|email
minlength:xEnsures the string length is at least x.
Example: required|minlength:8
maxlength:xEnsures the string length does not exceed x.
Example: required|maxlength:10
stringChecks if the value is a string.
Example: required|string
urlChecks if the string is a valid URL.
Example: required|url
startWithUpperEnsures the string starts with an uppercase letter.
Example: startWithUpper
startWithLowerEnsures the string starts with a lowercase letter.
Example: startWithLower
startWith:x,yChecks if the string starts with any of the given prefixes (x, y, etc.).
Example: startWith:pre1,pre2
endWith:x,yChecks if the string ends with any of the given suffixes (x, y, etc.).
Example: endWith:suf1,suf2
contains:x,yChecks if the string contains all of the listed substrings (x, y, etc.).
Example: contains:foo,bar
length:xValidates that the string length is exactly x.
Example: length:9
passwordBasic password complexity: length ≥ 8, uppercase, lowercase, number, special char.
Example: required|password
startWithStringEnsures the input does not start with a digit.
Example: startWithString
endWithStringEnsures the input does not end with a digit.
Example: endWithString
hasLetterEnsures the string contains at least one letter.
Example: hasLetter
excludes:x,yEnsures none of the characters/strings listed (x, y) appear in the input.
Example: excludes:@,/,#
upperEnsures the string is entirely uppercase.
Example: upper
lowerEnsures the string is entirely lowercase.
Example: lower
stringBetween:min,maxEnsures the string length is between min and max.
Example: stringBetween:2,5
dateChecks if the input is a valid date using Day.js. Returns true if valid, false otherwise.
Example: required|date
before:xEnsures the input date is before the date x. You can use "now" for the current date/time.
Example: required|before:2020-01-01, required|before:now
after:xEnsures the input date is after the date x. You can use "now" for the current date/time.
Example: required|after:2020-01-01, required|after:now
dateBetween:x,yChecks if the input date lies between the two dates x and y. You can use "now" in place of either bound.
Example: required|dateBetween:2020-01-01,now
timeChecks if the input is a valid 24-hour time string (e.g., "HH:mm:ss"). If missing seconds, it automatically appends ":00" until the format is complete.
Example: required|time
min:xChecks the input value or character length against the minimum x. For numbers, ensures value >= x. For files, checks file size.
Example: required|min:2
max:xChecks the input value or character length against the maximum x. For numbers, ensures value <= x. For files, checks file size.
Example: required|max:20
integer / intEnsures the input is an integer.
Example: required|integer, required|int
number / numericValidates that the input can be parsed as a numeric value (not an object/boolean).
Example: required|number
modulo:x / mod:xChecks if the number is divisible by x.
Example: required|modulo:2, required|mod:2
lessThan:x / lthan:xEnsures the input value is strictly less than x.
Example: required|lessThan:10, required|lthan:10
greaterThan:x / gthan:xEnsures the input value is strictly greater than x.
Example: required|greaterThan:5, required|gthan:5
numberBetween:x,yChecks if the numeric input lies between x and y (inclusive).
Example: required|numberBetween:1,10
phoneValidates if the input is a phone number (for example, using a custom phone rule).
Example: required|phone
fileChecks if the input is a File, Blob, FileList, or an array of such items.
Example: required|file
maxFileSize:xEnsures the file size is at most x (e.g. 1MB).
Example: required|maxFileSize:2MB
minFileSize:xEnsures the file size is at least x (e.g. 1MB).
Example: required|minFileSize:500KB
fileBetween:x,yChecks that the file size is between x and y.
Example: required|fileBetween:1MB,5MB
mimes:x,yValidates the file’s MIME type/extension. Supports wildcards (*.jpg), type groups (image/*), or specific extensions.
Example: required|mimes:*.pdf,image/*

With Jevalide, validation is no longer a hassle. Customize once, reuse everywhere, and keep your validation logic clean and maintainable.

Keywords

Validation, Form validation, HTML form validator

FAQs

Package last updated on 24 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts