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

@minimajs/schema

v0.1.1

Published

Schema, built on top of Yup, provides a comprehensive set of validation tools and exposes everything from `@minimajs/schema` to facilitate seamless validation in your applications.

Downloads

31

Readme

Schema, built on top of Yup, provides a comprehensive set of validation tools and exposes everything from @minimajs/schema to facilitate seamless validation in your applications.

Installation

You can install @minimajs/schema via npm or yarn:

npm install @minimajs/schema

Validating Request Body

To validate request bodies, you can use the createBody function along with Yup schema definitions. Here's an example:

import { createBody, string } from "@minimajs/schema";

const getUserPayload = createBody({
  name: string().required(),
});

function createUser() {
  const payload = getUserPayload(); // { name: string } type will be inferred.
  // Save data
  // payload = { name: string }
  return payload;
}

app.post("/users", createUser);

In this example, we define a schema for validating user payloads with a required name field.

Custom Validation Type

You can also create custom validation types using Yup's test function. Here's an example:

const jamesSchema = string().test(
  "is-james",
  (d) => `${d.path} is not James`,
  (value) => value == null || value === "James"
);

This schema ensures that the value is either null, undefined, or equals "James".

Async Validation

In some cases, you may need to perform asynchronous validation, such as checking if a username is unique. You can achieve this by defining a custom validator with an asynchronous test function. Here's how you can create a custom username validator:

// validation/rules.ts
const username = string().test(
  "username",
  (d) => `${d.path} is taken`,
  async (value) => User.findOne({ username: value })
);

This validator checks if the username already exists in the database asynchronously.

const getUserPayload = createBodyAsync({
  name: string().required(),
  username: username().required(),
});

async function createUser() {
  // highlight-next-line
  const payload = await getUserPayload();
  // Save data
  return "saved";
}

app.post("/", createUser);

In this example, we use createBodyAsync to validate the request body asynchronously, ensuring that both the name and username fields are present and satisfy the custom username validation rule.

The following functions are exposed from @minimajs/schema for your convenience:

  1. createBody
  2. createHeaders
  3. createSearchParams
  4. createBodyAsync
  5. createHeadersAsync
  6. createSearchParamsAsync

These functions enable you to easily define and validate request bodies, headers, and searchParams, both synchronously and asynchronously, ensuring the integrity and security of your application's data.