deguapp/api/src/validators/beerValidator.ts

71 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-05-12 21:25:52 +02:00
import * as yup from "yup";
import YupPassword from "yup-password";
YupPassword(yup);
2024-05-12 21:25:52 +02:00
import { Schema } from "mongoose";
interface mongooseAddition {
_id?: Schema.Types.ObjectId;
createdAt?: Schema.Types.Date;
updatedAt?: Schema.Types.Date;
}
// Create
export const add = yup.object({
brand: yup.string().required(),
name: yup.string().required(),
degree: yup.number().required(),
2024-05-12 21:25:52 +02:00
packaging: yup.string().required(),
});
export interface IBeer extends yup.InferType<typeof add>, mongooseAddition {}
export const addExam: IBeer = {
2024-05-12 21:25:52 +02:00
brand: "Pilsner Urqell",
name: "Kozel",
degree: 11,
2024-05-12 21:25:52 +02:00
packaging: "can",
};
// Remove
export const del = yup.object({
2024-05-12 21:25:52 +02:00
_id: yup.string().required(),
});
export interface IDel extends yup.InferType<typeof del> {}
export const delExam: IDel = {
2024-05-12 21:25:52 +02:00
_id: "6352b303b71cb62222f39895",
};
// Update
export const edit = yup.object({
_id: yup.string().required(),
brand: yup.string(),
name: yup.string(),
degree: yup.number(),
packaging: yup.string(),
2024-05-12 21:25:52 +02:00
//imgs: yup.mixed().when('$imgs', (imgs, schema) =>
// Array.isArray(imgs) ? schema.array() : schema.string()
//)
2024-05-12 21:25:52 +02:00
imgs: yup
.mixed()
.test(
"is-array-or-string",
"imgs must be either an array or a string",
(value) => Array.isArray(value) || typeof value === "string",
),
//imgs: yup.mixed().when('isArray', {
// is: Array.isArray,
// then: yup.array(),
// otherwise: yup.string()
//})
});
export interface IEdit extends yup.InferType<typeof edit> {}
export const editExam: IEdit = {
2024-05-12 21:25:52 +02:00
_id: "6355b95dc03fad77bc380146",
brand: "Pilsner Urqell",
name: "Radegast",
degree: 12,
2024-05-12 21:25:52 +02:00
packaging: "bottle",
imgs: [],
};