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

@jaris/validator

v0.0.7

Published

> TODO: description

Downloads

3

Readme

@jaris/validator

Easily validate any data using an easily testable and extendable approach. AKA plain functions.

Usage

import validate from '@jaris/validator';
import { optional, required, string, object } from '@jaris/validator/rules';

const userController = {
  async store(ctx) {
    const { data, errors } = await validate(ctx.request.body, {
      firstName: [required(), string()],
      lastName: [optional(), string()],
      team: [
        optional(),
        object({
          name: [string()],
          sport: [string()],
        }),
      ],
    });

    if (errors) {
      return errorResponse(errors);
    }

    const user = createSomeUser(data);

    return successResponse(user);
  },
};

export default userController;

Available Rules

  • all
  • allowed
  • array
  • boolean
  • exists
  • hexColor
  • lambda
  • length
  • max
  • min
  • notAllowed
  • notExists
  • number
  • object
  • optional
  • regex
  • required
  • requiredWith
  • string
  • type

all

When you want to check that all items in an array meet a certain condition.

await validate(
  { evenNumbers: [2, 4, 6] },
  {
    evenNumbers: [array(), all(item => item % 2 === 0)],
  },
);

allowed

Makes sure that the item being validated is in the provided array

await validate(
  { day: 'Saturday' },
  {
    day: [string(), allowed(['Saturday', 'Sunday'])],
  },
);

array

Makes sure that the value being validated is an array

await validate(
  { days: ['Monday', 'Wednesday'] },
  {
    day: [array()],
  },
);

boolean

Makes sure that the value being validated is either true or false

await validate(
  { termsOfService: true },
  {
    termsOfService: [boolean()],
  },
);

exists

Alias of allowed

hexColor

Checks that the value being validated is a hex color

await validate(
  { color: '#AFAFAF' },
  {
    color: [hexColor()],
  },
);

lambda

Takes a function that gets the value being validated passed as the first argument and either returns true or false.

await validate(
  { username: 'nehero' },
  {
    username: [lambda(username => usernameDoesntExist(username))],
  },
);

length

Verifies that the value being validated is of a certain length.

await validate(
  { username: 'nehero' },
  {
    username: [length(0, 64)],
  },
);

max

Verifies that the value being validated is not greater than the provided value

await validate(
  { username: 'nehero' },
  {
    username: [max(64)],
  },
);

min

Verifies that the value being validated is not less than the provided value

await validate(
  { username: 'nehero' },
  {
    username: [min(0)],
  },
);

notAllowed

Verifies that the value being validated is not in the provided array

await validate(
  { day: 'Tuesday' },
  {
    day: [notAllowed(['Saturday', 'Sunday'])],
  },
);

notExists

alias of notAllowed

number

Verifies that the value being validated is of type number

await validate(
  { day: 16 },
  {
    day: [number()],
  },
);

object

Verifies that the value being validated is of type object and allows you to test any keys.

await validate(
  { team: { name: 'Test Team', userIds: [4, 5, 6] } },
  {
    team: [
      required(),
      object({
        name: [required(), string()],
        userIds: [required(), array(), all(userId => userIdExists(id))],
      }),
    ],
  },
);

optional

Allows the value to not be present in the body. Note that null and other falsy values still count as being present. Does not continue on with other validators in the same array if value isn't present.

await validate(
  { firstName: 'Ozzie' },
  {
    firstName: [required(), string()],
    lastName: [optional(), string()],
  },
);

regex

Allows the value to be tested against a regex pattern.

await validate(
  { username: 'nehero' },
  {
    username: [regex(/[A-Za-z0-9]+/g)],
  },
);

required

Ensures that the value is present in the body

await validate(
  { firstName: 'Ozzie' },
  {
    firstName: [required(), string()],
    lastName: [optional(), string()],
  },
);

requiredWith

Only marks the value as required if the other key specified is present on the body. Works with nested values using dot syntax.

await validate(
  { firstName: 'Ozzie' },
  {
    firstName: [required(), string()],
    lastName: [requiredWith('team.name'), string()],
    team: [
      optional(),
      object({
        name: [string()],
      }),
    ],
  },
);

string

Ensures the value is a string.

await validate(
  { firstName: 'Ozzie' },
  {
    firstName: [required(), string()],
  },
);

type

Ensures the value is of the provided type. Evaluated using typeof. For array either use array() helper or pass in Array.

await validate(
  { firstName: 'Ozzie' },
  {
    firstName: [required(), type('string')],
  },
);