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

power-guard

v1.1.0

Published

The 0 dependency recipe for type strong validations: from `unknown` to anything you want. [中文](README-cn.md)

Downloads

375

Readme

Power Guard

The 0 dependency recipe for type strong validations: from unknown to anything you want. 中文

CI version size

🤔 A Cozy Way to Validate Unknown Variables

Instead of having:

const validate = (x: unknown): SomeType => {
  if (!(typeof x === 'object' && x !== null)) {
    throw new Error('x should be an object');
  }
  if (!('foo' in x && typeof x.foo === 'string')) {
    throw new Error('x.foo should be a string');
  }
  if (!('bar' in x && typeof x.bar === 'boolean')) {
    throw new Error('x.bar should be a boolean');
  }
  // ... a lot of validations
  return x;
};

Why not:

const guarded = guard({
  foo: string.required,
  bar: boolean.required,
  // ... other rules
})(x);

Installation

yarn add power-guard
# npm
npm i power-guard

Play with Your Favorite Framework

// Express.js
const { id, name } = guard({ id: number.required, name: string.required })(req.body);

// Koa
const { id, name } = guard({ id: number.required, name: string.required })(ctx.request.body);

Examples

Primitive Validation

import { number, string, boolean } from 'power-guard';

number.required(10); // 10
number.strict.required('10'); // error: string is not allowed in strict mode
number.gt(10).required('10'); // error: value should be greater than 10
string.with(10).required('a really long string'); // error: string has max length 10
string.optional(undefined); // undefined
boolean.required.array([true, false]); // [true, false]
boolean.required.array(true); // error: value should be an array
boolean.loose.required('1'); // true

Object Validation

import guard, { number, string, boolean } from 'power-guard';

const guardObject = guard({
  foo: number.required,
  bar: string.optional,
  baz: boolean.required,
});

const { foo, bar, baz } = guardObject({ foo: 10, baz: true }); // 10, undefined, true
const { foo, bar, baz } = guardObject({ foo: '10', baz: 'true' }); // 10, undefined, true
const { foo, bar, baz } = guardObject({ foo: 'abc', baz: 'true' }); // error: foo should be a number

Validating Complex Objects

import guard, { number, string, enumGuard, boolean, array } from 'power-guard';

enum Foo {
  key1 = 'key1',
  key2 = 2,
}

const guardObject = guard({
  foo: number.gte(10).required,
  bar: string.escaped.optional,
  baz: enumGuard(Foo).optional.array,
  bla: boolean.loose.optional,
  bnd: array(guard({ foo: number.strict.required })),
});

const guarded = guardObject({
  foo: '10',
  bar: '!abc ABC!',
  baz: [2, 'key1'],
  bla: 1,
  bnd: [{ foo: 123 }, { foo: 456 }],
});

/*
{
  foo: 10,
  bar: 'abc ABC',
  baz: [2, 'key1'],
  bla: true,
  bnd: [{ foo: 123 }, { foo: 456 }],
}
*/

Docs

The guard function

This function receives a DataConfig object as parameter, and returns a guard function which accepts an unknown variable and returns a typed object based on the config if validation succeeded, otherwise it will throw a PowerGuardError.

In short, this is the function for validating objects. Say we want an object to have the shape like:

{ foo: 123, bar: 'abc' }

Then we just need to tell power-guard:

const guardObject = guard({
  foo: number.required,
  bar: string.required,
});

You may notice there are various built-in guards, meanwhile deep customizations are also allowed:

const validate = (x: unknown): SomeType => {
  // your code goes here
};

const guardObject = guard({
  foo: validate,
});

const { foo } = guardObject(x); // foo will be `SomeType` if succeeded

Built-in Guards

Each of the guards below should follow with a .required or .optional, while the latter will allow undefined. Simply append .array in the end when needed e.g. boolean.required.array.

boolean

| Boolean Guard | Description | | -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | boolean | A boolean value must be presented. 'true' and 'false' are acceptable. | | boolean.strict | A boolean value must be presented. No implicit conversions allowed. | | boolean.loose | A boolean value must be presented. 'true' and 'false' are acceptable. If the value can be converted to a number, then return false iff the number equals 0, otherwise true. |

enum

| Enum Guard | Description | | ------------------- | -------------------------------------- | | enumGuard(EnumType) | A value of EnumType must be presented. |

number

| Number Guard | Description | | ------------------------------- | ------------------------------------------------------------------------- | | number | A number value must be presented. Valid number string is also acceptable. | | number.strict | A number value must be presented. No implicit conversions allowed. | | number.with([lower, upper]) | A number value within [lower, upper] must be presented. | | number.with(range: NumberRange) | A number value within the range must be presented. | | number.gt(x) | A number value within (x, +∞) must be presented. | | number.gte(x) | A number value within [x, +∞) must be presented. | | number.lt(x) | A number value within (-∞, x) must be presented. | | number.lte(x) | A number value within (-∞, x] must be presented. |

type NumberRange = Partial<{
  lower: number;
  equalsLower: boolean;
  upper: number;
  equalsUpper: boolean;
}>;

Note you can chain these restrictions like: number.gt(10).lte(20).strict.required

string

| String Guard | Description | | -------------- | -------------------------------------------------------------------------------------------------------------------------------------- | | string | A string value must be presented. | | string.escaped | A string value must be presented. Punctuations and unknown chars will be removed and fullwidth chars will be converted into halfwidth. | | string.with(x) | A string value with a max length of x must be presented. |

Note you can chain them as well, e.g.: string.with(10).escaped.optional

array

There's a helper function for customized array guards with optional override:

function array(elemGuard: (x: unknown) => T): (x: unknown) => Array<T>;
function array(elemGuard: (x: unknown) => T, optional: true): (x: unknown) => Array<T> | undefined;

Traditional Type Guards

power-guard will return a variable with the type you want instead of asserting types like traditional type guards. Browse types.ts if you would like to infer types.