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

oop-validator

v0.1.1

Published

A versatile and robust validation library for any UI framework.

Downloads

76

Readme

oop-validator

oop-validator is a versatile and robust validation library designed to seamlessly integrate with any UI framework or library. Whether you're building applications with Vue.js, React, Angular, or any other front-end technology, oop-validator provides a comprehensive and flexible solution for all your validation needs.

Key Features

  • Framework-Agnostic: Designed to work with any UI framework, ensuring maximum flexibility for your projects.
  • Extensible: Easily extend the library with custom validation rules to meet specific application requirements.
  • Comprehensive Rule Set: Includes built-in validation rules such as required, minimum and maximum length, email format, domain validation, and more.
  • Customizable Error Messages: Configure error messages for each validation rule to provide clear and user-friendly feedback.
  • Easy Integration: Simple setup and intuitive API make it easy to integrate into existing projects.
  • Lightweight and Performant: Optimized for performance, ensuring minimal impact on application load times and responsiveness.

Installation

You can install oop-validator via npm:

npm install oop-validator

USAGE

import { ValidationEngine, RequiredValidationRule, MinValidationRule, MaxValidationRule, EmailValidationRule } from 'oop-validator';

// Define the validation rules
const rules = [
    'required',
    { rule: 'min', params: { length: 3 }, message: "Minimum length is 3 characters." },
    { rule: 'max', params: { length: 15 }, message: "Maximum length is 15 characters." },
    'email'
];

// Initialize the validation engine with desired rules
const validationEngine = new ValidationEngine(rules);

// Validate a single value
const emailResult = validationEngine.validateValue('[email protected]');
if (!emailResult.isValid) {
    console.log('Email validation errors:', emailResult.errors);
}

const usernameResult = validationEngine.validateValue('us');
if (!usernameResult.isValid) {
    console.log('Username validation errors:', usernameResult.errors);
}

Custom Validation Rule

import { IValidationRule } from 'oop-validator';

export class PhoneNumberValidationRule extends IValidationRule {
    private errorMessage: string = "This field must be a valid phone number.";

    isValid(param: string): [boolean, string] {
        // A simple regex for validating phone numbers
        const phonePattern = /^\+?[1-9]\d{1,14}$/;
        const isValid = phonePattern.test(param);
        return [isValid, isValid ? "" : this.errorMessage];
    }

    isMatch(type: string): boolean {
        return type.toLowerCase() === 'phone';
    }

    setParams(params: any): void {
        // No parameters needed for phone number rule
    }

    setErrorMessage(message: string): void {
        this.errorMessage = message;
    }
}


// Add the custom rule to the validation engine
validationEngine.addRule(new PhoneNumberValidationRule());

const phoneResult = validationEngine.validateValue('+1234567890');
if (!phoneResult.isValid) {
    console.log('Phone validation errors:', phoneResult.errors);
}