New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@jaris/validator

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jaris/validator

> TODO: description

  • 0.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

@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')],
  },
);

FAQs

Package last updated on 19 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc