Skip to content

Instantly share code, notes, and snippets.

@ryodeushii
Created December 20, 2022 13:13
Show Gist options
  • Save ryodeushii/68e419d6ba228057754e8b1e61cb910e to your computer and use it in GitHub Desktop.
Save ryodeushii/68e419d6ba228057754e8b1e61cb910e to your computer and use it in GitHub Desktop.
banner schemas bazar
import { z } from 'express-zod-api';
enum BannerSize {
SMALL = 'small',
LARGE = 'large',
}
enum BannerType {
CATEGORY = 'category',
SUBCATEGORY = 'subcategory',
BRAND = 'brand',
MAIN_PAGE = 'main_page',
}
enum Action {
STORIES = 'stories',
MODAL = 'modal',
CATEGORY = 'category',
SUBCATEGORY = 'subcategory',
BRAND = 'brand',
}
enum MainPageBannerAction {
CATEGORY = 'category',
SUBCATEGORY = 'subcategory',
BRAND = 'brand',
}
const Screen = z.object({
title: z.string(),
text: z.string(),
appImage: z.string(),
webImage: z.string(),
actionType: z.nativeEnum(MainPageBannerAction),
entityId: z.string(),
button: z.string(), // FIXME: ????
duration: z.number(),
});
// TODO: optionals??
const Modal = z.object({
title: z.string(),
text: z.string(),
appImage: z.string(),
webImage: z.string(),
actionType: z.nativeEnum(MainPageBannerAction),
entityId: z.number(),
button: z.string(), // FIXME: ????
});
const BrandBanner = z.object({
id: z.number(),
partnerId: z.number(),
appImage: z.string().nullable(),
webImage: z.string().nullable(),
name: z.string().min(1),
type: z.literal(BannerType.BRAND),
brandId: z.number(),
});
const CategoryBanner = z.object({
id: z.number(),
partnerId: z.number(),
name: z.string().min(1),
type: z.literal(BannerType.CATEGORY),
categoryId: z.number(),
});
const SubCategoryBanner = CategoryBanner.omit({
type: true,
categoryId: true,
}).extend({
type: z.literal(BannerType.SUBCATEGORY),
subcategoryId: z.number(),
});
const BaseMainPageBanner = z.object({
id: z.number(),
partnerId: z.number(),
name: z.string().min(1),
size: z.nativeEnum(BannerSize),
type: z.literal(BannerType.MAIN_PAGE),
screens: z.array(Screen),
modal: Modal,
});
const BannerSchema = z.discriminatedUnion('type', [
BaseMainPageBanner.extend({
screens: z.array(Screen).min(1),
action: z.literal(Action.STORIES),
}),
BaseMainPageBanner.extend({
modal: Modal,
action: z.literal(Action.MODAL),
}),
BaseMainPageBanner.extend({
categoryId: z.number(),
action: z.literal(Action.CATEGORY),
}),
BaseMainPageBanner.extend({
subcategoryId: z.number(),
action: z.literal(Action.SUBCATEGORY),
}),
BaseMainPageBanner.extend({
brandId: z.number(),
action: z.literal(Action.BRAND),
}),
BrandBanner,
CategoryBanner,
SubCategoryBanner,
]);
/*
!(relation) partnerId
(relation) darkstores: []
!name
!size
!appImage
!webImage
type | main, category, subcategory, brand
actionType | stories, modal, category, subcategory, brand
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment