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

@euriklis/validator

v4.2.1

Published

A javascript package for testing and managing of type coonditions.

Downloads

244

Readme

@euriklis/validator

A versatile JavaScript library for conditional analysis, testing, and validation of JavaScript types.

Installation

You can install the @euriklis/validator package using npm:

npm install @euriklis/validator

Alternatively, you can install a specific version using the --save-exact flag:

npm install @euriklis/validator@<version> --save-exact

This command will add the package to your node_modules folder.

Usage

The @euriklis/validator library is designed for conditional testing, allowing you to create complex compositions of checking criteria and obtain the logical results of these tests. For example, if you have a user, a registration protocol, and a database, you can use the validator to test if the registration criteria are fulfilled:

const user = {
    username: 'Harris',
    password: 'k12d87dc3A!43d',
    email: '[email protected]',
    age: 22
};

const users = DB.getUsers();
const has_payment = true;

new validator(user)
    .interface({
        username: usr => usr.isString.and.hasLengthEqualsOrGreaterThan(6),
        password: psw => psw.isString.and.hasLengthGreaterThan(6),
        email: mail => mail.isString.and.isEmail,
        age: age => age.isEqualOrGreaterThan(18)
    })
    .and.bind(new validator(user.not.isSameWithAny(users)))
    .and.bind(new validator(has_payment).isSame(true))
    .on(true, () => makeRegistration(user))
    .on(false, () => requireInfo(users, user, has_payment));

Starting from version 2.0.0, we've introduced getter methods for methods that don't require arguments (excluding copy() and absoluteCopy). These getter methods provide a more intuitive way to access various validation checks and conditions.

For example, you can use methods like isInteger, isArray, isNumber, isString, isObject, isUndefined, isEmpty, and more without parentheses. Additionally, the operators and, or, and not should now be written with lowercase letters (from version 4.0.0), while all methods follow the camel case convention. For example:

import validator from '@euriklis/validator';

const array = [1, 2, 3, 4, 5, 8, 12];

new validator(array)
    .isIntegerArray
    .or.isStringArray
    .on(true, () => console.log('The array contains only integers or strings.'));

The library has been updated since version 3.0.0 with private methods for operands and the not property. We use again the @euriklis/message package (from version 4.0.0) for the test method, and new methods have been introduced, including isIntegerArray, isObjectArray, hasLengthGreater/LessThan(), hasLengthEqualsOrGreater/LessThan(), hasLengthInRange(), hasLengthInClosedRange(), isPositiveInteger, isNegativeInteger, isPositive, isNegative, isConvertibleToNumber (instead of the old version isNumberLike) and many others. From version 4.0.0 the contains and interface methods was deleted. The interface2 method is now used as interface method.

Because of the fact that the package uses private methods, please note that private methods require Node.js version 12.0.0 or higher, Chrome version 74 or higher, and Firefox version 90 or higher.

Methods

The most of the methods of the @euriklis/validator return a validator instance, allowing you to chain methods. The result of the comparison (the answer) and the condition fulfillment are recorded in the "answer" property. Here's an example:

import validator from '@euriklis/validator';

let a = 5, b = 12, c = [11, 13];

let result = new validator(a)
    .isInteger
    .and.isLessThan(6).or.isInClosedRange(5, 6)
    .and.bind(
        new validator(b).isInteger.and
            .isGreaterThan(10)
    )
    .and.bind(
        new validator(c).isNumberArray
            .and.not.forAny(number => {
                return number.isFloat();
            })
    )
    .answer;

console.log(result); // true

Bugs and Tips

If you encounter any issues or have tips to share, please feel free to contact us. You can reach us via email at [email protected] or [email protected] or to publish your suggestion in the issues section of the github repo of the package.

License

The @euriklis/validator package is released under the MIT License. It is provided for free for personal and non-commercial use. The author of the package is not liable for errors in third-party software, libraries, packages, or source code used in conjunction with this library.