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

all-ok

v0.3.2

Published

Validate anything simply and type-safely

Downloads

251

Readme

Installation

$ npm install all-ok

Introduction

It's important to parse input data and narrow down its types more strictly using schema validation packages such as Zod, Valibot, and others.

A typical server-side implementation first parses external input into appropriate types before passing it to domain logic, then additional validations are executed in the domain logic.

When implementing validations in domain logic, you might have the following needs:

  • Function-based validation rather than additional schema validation
  • Easy validation with server-generated objects (a transaction object and other non-external objects)
  • Sharing domain validation logic with frontend schema validation in full-stack TypeScript applications

This package addresses all these needs. While designed to work with schema validation packages, it can also be used independently.

Usage

Here are some basic usage examples. The API Document is 👉 here.

import * as aok from 'all-ok';
import * as v from 'valibot';

import { db } from '~/db';

type User = {
  name: string;
};

const checkUser = aok.checkSync(
  (user: User) => user.name.length > 3,
  "name",
  "The name should be more than 3 characters."
);

const checkNameLength = aok.checkSync(
  (name: string) => name.length < 16,
  "name",
  "The name should be less than 16 characters."
);

const checkNameUniq = aok.checkAsync(
  async (name: string) => {
    const u = await db.findUser(name);
    return !u;
  },
  "name",
  "The name should be unique."
);

const userValidation = [
  checkUser,
  aok.mapAsync(
    (user: User) => user.name,
    [
      checkNameLength,
      checkNameUniq
    ]
  ),
];

const result = await aok.runAsync(userValidation, { name: "foo" });

if (!result.ok) {
  console.log(result.errors);
  // =>
  // [
  //   { label: "name", message: "The name should be more than 3 characters." },
  //   { label: "name", message: "The name should be unique." }
  // ]
}

// With valibot

const UserSchema = v.pipe(
  v.object({
    name: v.pipe(
      v.string(),
      v.check(checkNameLength.fn, checkNameLength.error.message),
    ),
  }),
  v.forward(
    v.check(checkUser.fn, checkUser.error.message),
    [ checkUser.error.label ]
  ),
);

Run validation with transaction. You can pass any object as context to the runner.

import * as aok from 'all-ok';

import { type Database, db } from '~/db';

type Seat = {
  userId: number;
  eventId: number;
};

const checkSeatReserved = aok.checkAsync(
  async (seat: Seat, tx: Database) => {
    const s = await tx.findSeat(seat.userId, seat.eventId);
    return !s;
  },
  "event",
  "You have reserved this event already."
);

const checkSeatAvailable = aok.checkAsync(
  async (seat: Seat, tx: Database) => {
    const e = await tx.findEventWithLock(seat.eventId);
    return e.isAvailable;
  },
  "event",
  "There are no seats available for this event."
);

const seatValidation = [
  checkSeatReserved,
  checkSeatAvailable,
];

await db.transaction(async (tx) => {
  const result = await aok.runAsyncWithContext(
    seatValidation,
    { userId: 1, eventId: 1 },
    tx
  );

  if (!result.ok) {
    console.log(result.errors);
  }
});