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.
You can install @minimajs/schema
via npm or yarn:
npm install @minimajs/schema
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.
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".
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:
createBody
createHeaders
createSearchParams
createBodyAsync
createHeadersAsync
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.