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

freeval

v2.28.17

Published

Freeval (for free validator) provides a flexible validation mechanism for objects based on predefined rules.

Downloads

55

Readme

Freeval

Freeval (for free validator) provides a flexible validation mechanism for objects based on predefined rules. It's a typescript implementation of freeval, a struct validation crate in Rust.

Installation

To install the freeval, you can use npm:

npm install freeval

or yarn

yarn add freeval

Usage

Here's how you can use the freeval in your TypeScript projects:

import { Validator, ValidatorRules } from 'freeval';

// Not necessary but you can define an interface for your data
interface UserData {
    username: string;
    email: string;
    password: string;
    confirmPassword: string
}

// Define your data object and validation rules
const data: UserData = {
    username: 'prodbyola',
    email: '[email protected]',
    password: 'securePassword123',
    confirmPassword: ''
};

const rules: ValidatorRules<UserData /* or typeof `data` */> = {
    username: [
        { condition: 'required', error: 'Username is required' },
        // minimum of 8 characters length
        { 
            condition: 'min_len', 
            error: 'Username must be 8 characters minimum', 
            size: 8 
        }, 
    ],
    email: [
        // Setting an error message is OPTIONAL.
        { condition: 'required' },
        { condition: 'email', error: 'Invalid email format' }
    ],
    password: [
        { condition: 'required', error: 'Password is required' },
        { condition: 'password', error: 'Weak password' }
    ]
    confirmPassword: [
        // You can also define your custom boolean condition
        { 
            condition: data.password === data.confirmPassword,
            error: 'Please confirm your password.'
        }
    ]
};

// Create a new Validator instance
const validator = new Validator(data, rules);

// Validate the data
const isValid = validator.validate();

if (!isValid) {
    console.log('Validation failed. Errors:');
    console.log(validator.errors);
} else {
    console.log('Validation successful.');
}

API

Validator<T>

The Validator<T> class provides methods for data validation.

constructor(data: T, rules?: ValidatorRules<T>)

  • data: The object to be validated.
  • rules: Optional. The validation rules for the object properties.

valid: boolean

Validates the object against the specified rules. Returns true if the object passes validation; otherwise, returns false.

Make sure you call either validate or validateField function before accessing this propery correctly:

    const validator = new Validator(data, rules)
    const isValid = validator.validate()
    validator.valid

invalid: boolean

Returns a negated value of valid.

Make sure you call either validate or validateField function before accessing this propery correctly:

    const validator = new Validator(data, rules)
    const isValid = validator.validate()
    validator.invalid

validate(): boolean

Validates all properties or fields in data based on speified rules.

setRules(rules: ValidatorRules<T>): void

Sets the validation rules for the object properties.

getErrors(field: keyof T): string[] | undefined

Gets the validation errors for a specific data field or property.

clearFieldErrors(field: keyof T): void

Clears the validation errors for a specific data field or property.

clearAllErrors(): void

Clears all validation errors.

getFieldRules(field: keyof T): ValidatorRule[] | undefined

Retrieves validation rules defined for a data field. Returns undefined if no rule is declared for field.

removeFieldRules(field: keyof T): void

Remove all rules added for a field or property.

removeFieldRule(field: keyof T, rule: ValidatorRule): void

Remove a specific rule added to a field or property.

Contributing

Contributions to the freeval package are welcome! Feel free to submit bug reports, feature requests, or pull requests through the GitHub repository.

License

This project is licensed under the GPL License - see the LICENSE file for details.