hadeeb / ts-rest-yup

Use Yup schemas in ts-rest

Repository from Github https://github.comhadeeb/ts-rest-yupRepository from Github https://github.comhadeeb/ts-rest-yup

Use Yup schemas in ts-rest

// package.json
{
  "dependencies": {
    "@hadeeb/ts-rest-yup": "hadeeb/ts-rest-yup"
  }
}

Step 1: Lie to TypeScript that you have zod installed, and that it re-exports yup types.

// tsconfig.json
{
  "include": ["@hadeeb/ts-rest-yup/types"]
}

Step 2: Use Yup2Zod to add zod-like methods to yup.

import * as Yup from "yup";
import { Yup2Zod } from "@hadeeb/ts-rest-yup";
// Add zod-like methods to Yup
Yup2Zod(Yup);

// Now you can use Yup schemas in ts-rest
import { initContract } from "@ts-rest/core";

const c = initContract();

const appRouter = c.router({
  getUser: {
    method: "GET",
    path: "/user",
    responses: {
      200: Yup.object({
        id: Yup.string().required(),
        full_name: Yup.string().required(),
      }),
    },
  },
});

About

Use Yup schemas in ts-rest

License:MIT License


Languages

Language:TypeScript 80.7%Language:JavaScript 19.3%