Skip to content

tetherto/pear-apps-utils-validator

Repository files navigation

pear-apps-utils-validator

A lightweight and flexible JavaScript library for validating. It provides a simple API to define validation rules for various data types, including strings, numbers, arrays, and objects.

Table of Contents

Features

  • Validate strings, numbers, arrays, and objects.
  • Built-in validation rules like required, minLength, maxLength, min, max, email, and more.
  • Custom validation with refine.
  • Schema-based validation for objects and arrays.
  • Lightweight and easy to use.

Security Notice

  1. To ensure the security and integrity of your projects, please note that official PearPass packages are distributed exclusively through our GitHub organization.
  2. Any packages with similar names found on the npm registry or other third-party package managers are not affiliated with PearPass and should be strictly avoided. We recommend installing directly from this repository to ensure you are using the verified, open-source version.

Installation

Install the library using npm:

npm install git+https://github.com/tetherto/pear-apps-utils-validator.git

Usage Examples

String Validation

import { Validator } from '@tetherto/pear-apps-utils-validator';

const validator = Validator.string().required().minLength(3);
console.log(validator.validate('')); // Output: "This field is required"
console.log(validator.validate('ab')); // Output: "Minimum length is 3"
console.log(validator.validate('abc')); // Output: null

Object Validation

const schema = {
    name: Validator.string().required(),
    age: Validator.number().min(18),
};

const validator = Validator.object(schema);
console.log(validator.validate({ name: '', age: 16 })); 
// Output: { name: "This field is required", age: "Minimum value is 18" }

Array Validation

const itemValidator = Validator.string().minLength(3);
const arrayValidator = Validator.array().items(itemValidator);

console.log(arrayValidator.validate(['valid', 'a'])); 
// Output: [{ index: 1, error: "Minimum length is 3" }]

Dependencies

This package has no production dependencies.

Related Projects

License

This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.k

About

PearPass is an open-source, privacy-first password manager with peer-to-peer syncing and end-to-end encryption. This repository contains shared core components used across the PearPass apps.

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors