@minimajs/schema

0.1.1 • Public • 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.

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.

Readme

Keywords

none

Package Sidebar

Install

npm i @minimajs/schema

Weekly Downloads

5

Version

0.1.1

License

none

Unpacked Size

16.2 kB

Total Files

11

Last publish

Collaborators

  • adil.sudo