mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-08 22:53:25 +00:00
refacto: duplicated validation schema
This commit is contained in:
14
src/lib/category/categoryValidationSchema.ts
Normal file
14
src/lib/category/categoryValidationSchema.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { number, object, string } from "yup";
|
||||
|
||||
const CategorieBodySchema = object({
|
||||
name: string()
|
||||
.trim()
|
||||
.required("Category name's required")
|
||||
.max(128, "Category name's too long"),
|
||||
}).typeError("Missing request Body");
|
||||
|
||||
const CategorieQuerySchema = object({
|
||||
cid: number().required(),
|
||||
});
|
||||
|
||||
export { CategorieBodySchema, CategorieQuerySchema };
|
||||
21
src/lib/link/linkValidationSchema.ts
Normal file
21
src/lib/link/linkValidationSchema.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { VALID_URL_REGEX } from "constants/url";
|
||||
import { boolean, number, object, string } from "yup";
|
||||
|
||||
const LinkBodySchema = object({
|
||||
name: string()
|
||||
.trim()
|
||||
.required("Link name is required")
|
||||
.max(128, "Link name is too long"),
|
||||
url: string()
|
||||
.trim()
|
||||
.required("URl is required")
|
||||
.matches(VALID_URL_REGEX, "Invalid URL format"),
|
||||
categoryId: number().required("CategoryId must be a number"),
|
||||
favorite: boolean().default(() => false),
|
||||
}).typeError("Missing request Body");
|
||||
|
||||
const LinkQuerySchema = object({
|
||||
lid: number().required(),
|
||||
});
|
||||
|
||||
export { LinkBodySchema, LinkQuerySchema };
|
||||
@@ -1,29 +1,21 @@
|
||||
import { number, object, string } from "yup";
|
||||
|
||||
import { apiHandler } from "lib/api/handler";
|
||||
import {
|
||||
CategorieBodySchema,
|
||||
CategorieQuerySchema,
|
||||
} from "lib/category/categoryValidationSchema";
|
||||
import getUserCategory from "lib/category/getUserCategory";
|
||||
import prisma from "utils/prisma";
|
||||
import getUserCategoryByName from "lib/category/getUserCategoryByName";
|
||||
|
||||
import prisma from "utils/prisma";
|
||||
|
||||
export default apiHandler({
|
||||
put: editCategory,
|
||||
delete: deleteCategory,
|
||||
});
|
||||
|
||||
const querySchema = object({
|
||||
cid: number().required(),
|
||||
});
|
||||
|
||||
const bodySchema = object({
|
||||
name: string()
|
||||
.trim()
|
||||
.required("name is required")
|
||||
.max(32, "name is too long"),
|
||||
}).typeError("Missing request Body");
|
||||
|
||||
async function editCategory({ req, res, user }) {
|
||||
const { cid } = await querySchema.validate(req.query);
|
||||
const { name } = await bodySchema.validate(req.body);
|
||||
const { cid } = await CategorieQuerySchema.validate(req.query);
|
||||
const { name } = await CategorieBodySchema.validate(req.body);
|
||||
|
||||
const category = await getUserCategory(user, cid);
|
||||
if (!category) {
|
||||
@@ -50,7 +42,7 @@ async function editCategory({ req, res, user }) {
|
||||
}
|
||||
|
||||
async function deleteCategory({ req, res, user }) {
|
||||
const { cid } = await querySchema.validate(req.query);
|
||||
const { cid } = await CategorieQuerySchema.validate(req.query);
|
||||
|
||||
const category = await getUserCategory(user, cid);
|
||||
if (!category) {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { apiHandler } from "lib/api/handler";
|
||||
import { CategorieBodySchema } from "lib/category/categoryValidationSchema";
|
||||
import getUserCategories from "lib/category/getUserCategories";
|
||||
import getUserCategoryByName from "lib/category/getUserCategoryByName";
|
||||
|
||||
import prisma from "utils/prisma";
|
||||
import { object, string } from "yup";
|
||||
|
||||
export default apiHandler({
|
||||
get: getCatgories,
|
||||
@@ -16,15 +17,8 @@ async function getCatgories({ res, user }) {
|
||||
});
|
||||
}
|
||||
|
||||
const bodySchema = object({
|
||||
name: string()
|
||||
.trim()
|
||||
.required("name is required")
|
||||
.max(32, "name is too long"),
|
||||
}).typeError("Missing request Body");
|
||||
|
||||
async function createCategory({ req, res, user }) {
|
||||
const { name } = await bodySchema.validate(req.body);
|
||||
const { name } = await CategorieBodySchema.validate(req.body);
|
||||
|
||||
const category = await getUserCategoryByName(user, name);
|
||||
if (category) {
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { boolean, number, object, string } from "yup";
|
||||
|
||||
import { VALID_URL_REGEX } from "constants/url";
|
||||
import { apiHandler } from "lib/api/handler";
|
||||
import getUserLink from "lib/link/getUserLink";
|
||||
import { LinkBodySchema, LinkQuerySchema } from "lib/link/linkValidationSchema";
|
||||
|
||||
import prisma from "utils/prisma";
|
||||
|
||||
export default apiHandler({
|
||||
@@ -10,27 +9,9 @@ export default apiHandler({
|
||||
delete: deleteLink,
|
||||
});
|
||||
|
||||
const querySchema = object({
|
||||
lid: number().required(),
|
||||
});
|
||||
|
||||
// FIXME: code duplicated from api/link/create
|
||||
const bodySchema = object({
|
||||
name: string()
|
||||
.trim()
|
||||
.required("name is required")
|
||||
.max(32, "name is too long"),
|
||||
url: string()
|
||||
.trim()
|
||||
.required("url is required")
|
||||
.matches(VALID_URL_REGEX, "invalid url format"),
|
||||
categoryId: number().required("categoryId must be a number"),
|
||||
favorite: boolean().default(() => false),
|
||||
}).typeError("Missing request Body");
|
||||
|
||||
async function editLink({ req, res, user }) {
|
||||
const { lid } = await querySchema.validate(req.query);
|
||||
const { name, url, favorite, categoryId } = await bodySchema.validate(
|
||||
const { lid } = await LinkQuerySchema.validate(req.query);
|
||||
const { name, url, favorite, categoryId } = await LinkBodySchema.validate(
|
||||
req.body
|
||||
);
|
||||
|
||||
@@ -64,7 +45,7 @@ async function editLink({ req, res, user }) {
|
||||
}
|
||||
|
||||
async function deleteLink({ req, res, user }) {
|
||||
const { lid } = await querySchema.validate(req.query);
|
||||
const { lid } = await LinkQuerySchema.validate(req.query);
|
||||
|
||||
const link = await getUserLink(user, lid);
|
||||
if (!link) {
|
||||
|
||||
@@ -1,31 +1,16 @@
|
||||
import { boolean, number, object, string } from "yup";
|
||||
|
||||
import { apiHandler } from "lib/api/handler";
|
||||
import getUserCategory from "lib/category/getUserCategory";
|
||||
import getUserLinkByName from "lib/link/getLinkFromCategoryByName";
|
||||
import { LinkBodySchema } from "lib/link/linkValidationSchema";
|
||||
|
||||
import { VALID_URL_REGEX } from "constants/url";
|
||||
import prisma from "utils/prisma";
|
||||
|
||||
export default apiHandler({
|
||||
post: createLink,
|
||||
});
|
||||
|
||||
const bodySchema = object({
|
||||
name: string()
|
||||
.trim()
|
||||
.required("name is required")
|
||||
.max(32, "name is too long"),
|
||||
url: string()
|
||||
.trim()
|
||||
.required("url is required")
|
||||
.matches(VALID_URL_REGEX, "invalid url format"),
|
||||
categoryId: number().required("categoryId must be a number"),
|
||||
favorite: boolean().default(() => false),
|
||||
}).typeError("Missing request Body");
|
||||
|
||||
async function createLink({ req, res, user }) {
|
||||
const { name, url, favorite, categoryId } = await bodySchema.validate(
|
||||
const { name, url, favorite, categoryId } = await LinkBodySchema.validate(
|
||||
req.body
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user