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

validum

v1.2.1

Published

☑️ A simple fluent validation library for TypeScript

Downloads

5

Readme

☑️ validum

validum is a simple fluent validation library that allows you to validate anything by creating a chain of validations like this:

const user = {
    name: 'Test McTestFace',
    username: 'test',
    age: 17,
}

const result = validation
    .of(user)
    .property('name')
        .alphanumeric()
        .maxLength(10)
        .withMessage('Who has a name that long?')
        .withCode('ERR-100')
    .andProperty('age')
        .greaterThanOrEqual(18)
        .withMessage('Only adults allowed!')
    .result()

This will return a result object that contains different methods to retrieve the validation errors, the original input and some utility methods to combine different results. For example, calling errors() on the previous result will return this array:

[
    {
        message: 'name must contain only alphanumeric characters',
        code: undefined,
        property: 'name',
    },
    {
        message: 'Who has a name that long?',
        code: 'ERR-100',
        property: 'name',
    },
    {
        message: 'Only adults allowed!',
        code: undefined,
        property: 'age',
    },
]

Thanks to TypeScript every property name, even though they're strings, are type safe. For example, the following will yield an error:

const result = validation
    .of(user)
    .property('namee') // Error: Argument of type '"namee"' is not assignable to...
    ...

You can also combine different results to get one array with all the different validation errors instead of creating one big validation. For example:

const address = {
    street: 'Fake Street',
    number: -1,
}

const user = {
    name: 'Test McTestFace',
    username: 'test',
    age: 17,
}

const result = validation
    .of(user)
    // ...(same as before)
    .result()

const addressResult = validation
    .of(address)
    .property('street')
        .minLength(5)
        .andProperty('number')
        .positive()
    .result()

const combinedResult = result.combineWith(addressResult)

If we call errors() on combinedResult now it will return:

[
    // ...(same as before)
    {
        message: 'number must be positive',
        code: undefined,
        property: 'number',
    },
]

🧰 Installation

npm install validum

# or if you use yarn
yarn add validum

📖 Documentation

Head to the wiki page.

🛠 Building

validum uses yarn as the default package manager, so for building the project it's recommended that you use the same; using npm is still an option, though, but yarn is recommended for a more predictable build. That being said:

git clone https://github.com/sleepyfran/validum.git
cd validum
yarn install

🧪 Testing

After building you can execute the tests by simply running yarn test.

❓ FAQ

Can you add XYZ validator/feature?

This library was created specifically for Duets and it may not cover every use-case. If you want a feature to be added, feel free to open an issue and we'll discuss the possibility there.

How can I contribute?

Take a look at the current issues or open your own so we can discuss how the implementation would go.