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

@slicr97/simple-rules

v1.1.0

Published

Simple input validation

Downloads

2

Readme

simple-rules

Simple input validation

Installation

Using npm:

npm install simple-rules

Using yarn:

yarn add simple-rules

Usage Example

To give you an example of how this library works, look at the following code snippet:

import { rules, Rules } from 'simple-rules';

type RegistrationInput = {
  email: string;
  password: string;
  passwordConfirmation: string;
};

const registrationInputRules = rules<RegistrationInput>(
  ['email', Rules.notEmpty],
  ['password', Rules.notEmpty],
  ['password', Rules.minLength({ threshold: 8 })],
  ['password', (password, input) => password === input.passwordConfirmation, 'PASSWORDS_DONT_MATCH'],
  ['passwordConfirmation', Rules.notEmpty],
  ['passwordConfirmation', Rules.minLength({ threshold: 8 })]
);

const input: RegistrationInput = {
  email: '[email protected]',
  password: 'superSecretPassword',
  passwordConfirmation: 'anotherSecretPassword'
};

const ruleViolations = registrationInputRules(input);

/**
 * RuleViolations:
 * {
 *   password: {
 *     errorCodes: [ 'PASSWORDS_DONT_MATCH' ],
 *     propertyValue: 'superSecretPassword'
 *   }
 * }
 */

As we see, there are 4 steps to perform a validation with simple-rules:

  1. Define the type that should be validated
  2. Define your validations by using the rules function and passing it the tuples / triples you need for the validation
  3. Get an actual input object (this could for example be the body of a POST request)
  4. Apply the input object to your validation function and evaluate the results

Types of rules

As for now, there are three different types of rules you can use to validate your objects:

Normal rules

Normal rules are the simplest ones. They simply take the property you want to validate and return a RuleViolation if the validation fails or undefined if it succeeds. This library provides many predefined normal rules out of the box.

Nested rules

Nested rules are used when the validated property is in itself a complex object. That way, you can validate objects that are nested as deeply as you need them to be.

Codependent rules

Codependent rules are used when the validity of a property depends on the state of another. For example, in the code snippet above, we needed to check if the two passwords entered by the user actually match.

Combining the rule types

As a general rule of thumb, you should always be able to apply as many rules as needed to a given property. The same goes for codependent rules. Nested rules are the exception here, because you cannot apply nested rules to a property that is being validated with normal or codependent rules, nor can you apply two sets of nested rules to the same property.

Further information

For further information, I have to direct you to the unit tests for now. I will continue this documentation once I find the time necessary to do so. You can also check out this repository and play around with the code in the sample directory.