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

@ededejr/validate

v0.5.0

Published

A utility for validating plain objects.

Downloads

4

Readme

Validate

validate is an experimental module for validating pojos.

Installation

npm install @ededejr/validate

Usage

Validating an object

The provided validate function can be used to test a given object against a set of rules.

import { validate } from '@ededejr/validate';

const isValid = validate(
  {
    name: 'Edede',
    handle: '@ededejr',
  },
  {
    name: (name) => name.length === 5,
    handle: (handle) => handle.startsWith('@'),
  }
); // true;

This has its uses, but can be somewhat cumbersome to do this for every object.

Reusing validation rules

Rather than inlining validation rules for an object, we can create a validator which expects a certain object type and reuse it.

import { createObjectValidator } from '@ededejr/validate';

interface Person {
  name: string;
  age: number;
}

const validatePerson = createObjectValidator<Person>({
  name: Validators.string,
  age: (number) => number > 0,
});

const isPerson = validatePerson({ name: 'Cole', age: 1 }); // true;

Creating functions with validated parameters

Extending things further, we can create special functions that validate their parameters before executing. This can be ideal for situations where remote execution is permitted, or inputs come over the wire.

import { createValidatedFunction, Validators } from '@ededejr/validate';

// Set up our scenario, in this case we'll work with simple coordinates
type Coordinates = { x: number; y: number };
type CoordinateOperator<Result = number> = (c: Coordinates) => Result;

// Write a function to print coordinates
const _print: CoordinateOperator<string> = ({ x, y }) => `(${x}, ${y})`;

// Create a validated copy of print
const print = createValidatedFunction<Coordinates, ReturnType<typeof _print>>(
  _print,
  {
    x: Validators.number,
    y: Validators.number,
  }
);

// Now we can use print
print({ x: 9, y: 10 }); // Success
print({ x: 9, y: '10' }); // Error

Validating deeply nested objects

It is also possible to specify how deep your validations need to go, this is especially useful for nested Objects.

import {
  createValidatedFunction,
  createObjectValidator,
  Validators,
} from '@ededejr/validate';

// Bringing back coordinates
type Coordinate = { x: number; y: number };
type CoordinateOperator<Result = number> = (c: Coordinate) => Result;

// Let's create a line type
type Line = { start: Coordinate; end: Coordinate };

// Create a coordinate validator that ensures a coordinate is always valid
const coordinateValidator = createObjectValidator<Coordinate>({
  x: Validators.number,
  y: Validators.number,
});

// Create a distance function that measures the distance between two coordinates
const _distance = ({ start, end }: Line) =>
  Math.sqrt(
    (start.x - end.x) *
      (start.x - end.x) *
      (start.y - end.y) *
      (start.y - end.y)
  );

// Created a validated version of distance
const distance = createValidatedFunction<Line, ReturnType<typeof _distance>>(
  _distance,
  {
    start: coordinateValidator,
    end: coordinateValidator,
  }
);

// Now we can use the validated distance
distance({ start: { x: 10, y: 25 }, end: { x: 50, y: 40 } }); // Success
distance({ start: { x: 10, y: 25 }, end: { x: '50', y: '40' } }); // Error