|
| 1 | +import { PhysicalForm } from "@prisma/client" |
| 2 | +import { Merge, Simplify } from "type-fest" |
| 3 | +import * as z from "zod" |
| 4 | + |
| 5 | +import { GetElementType, NullPartialDeep } from "types/util" |
| 6 | + |
| 7 | +import { refineNoDuplicates, transformStringToNumber } from "./utils" |
| 8 | + |
| 9 | +//==== Compound & Ingredient schemas ====// |
| 10 | + |
| 11 | +export const ingredientSchemaBase = z.object({ |
| 12 | + order: z.number().int(), |
| 13 | + chemicalId: z.number().int(), |
| 14 | + sdsId: z.number().int(), |
| 15 | + physicalForm: z.nativeEnum(PhysicalForm), |
| 16 | + isCommercialProduct: z.boolean(), |
| 17 | + commercialProduct: z.object({ |
| 18 | + name: z.string().trim().min(1), |
| 19 | + din: transformStringToNumber(z.number().int()).nullish(), |
| 20 | + hasNoDin: z.boolean(), |
| 21 | + hasProductMonographConcerns: z.boolean(), |
| 22 | + concernsDescription: z.string().trim().min(1).nullish(), |
| 23 | + }), |
| 24 | +}) |
| 25 | +const commercialIngredientSchema = ingredientSchemaBase.extend({ |
| 26 | + chemicalId: ingredientSchemaBase.shape.chemicalId.nullish(), |
| 27 | + sdsId: ingredientSchemaBase.shape.sdsId.nullish(), |
| 28 | + isCommercialProduct: z.literal(true), |
| 29 | +}) |
| 30 | +const nonCommercialIngredientSchema = ingredientSchemaBase.extend({ |
| 31 | + isCommercialProduct: z.literal(false), |
| 32 | + commercialProduct: z.object({ |
| 33 | + name: z.undefined(), |
| 34 | + din: z.undefined(), |
| 35 | + hasNoDin: z.undefined(), |
| 36 | + hasProductMonographConcerns: z.undefined(), |
| 37 | + concernsDescription: z.undefined(), |
| 38 | + }), |
| 39 | +}) |
| 40 | + |
| 41 | +export const ingredientSchema = z.discriminatedUnion("isCommercialProduct", [ |
| 42 | + commercialIngredientSchema, |
| 43 | + nonCommercialIngredientSchema, |
| 44 | +]) |
| 45 | + |
| 46 | +export type IngredientFields = Simplify<z.output<typeof ingredientSchema>> |
| 47 | +export type IngredientFieldsInput = Simplify<z.input<typeof ingredientSchema>> |
| 48 | + |
| 49 | +export type NullPartialIngredientFields = Simplify< |
| 50 | + NullPartialDeep<IngredientFieldsInput, { ignoreKeys: "order" }> |
| 51 | +> |
| 52 | +const shortcutSchema = z |
| 53 | + .discriminatedUnion("hasShortcut", [ |
| 54 | + z.object({ |
| 55 | + hasShortcut: z.literal(false), |
| 56 | + variations: z |
| 57 | + .object({ |
| 58 | + code: z.string().trim().min(1), |
| 59 | + name: z.string().trim().min(1), |
| 60 | + }) |
| 61 | + .array() |
| 62 | + .max(0) |
| 63 | + .default([]), |
| 64 | + suffix: z.undefined(), |
| 65 | + }), |
| 66 | + z.object({ |
| 67 | + hasShortcut: z.literal(true), |
| 68 | + //TODO: Simplify shortcut variations validation code |
| 69 | + variations: z |
| 70 | + .object({ |
| 71 | + code: z |
| 72 | + .string() |
| 73 | + .trim() |
| 74 | + .transform((arg) => (arg === "" ? null : arg.toUpperCase())) |
| 75 | + .nullable(), |
| 76 | + name: z |
| 77 | + .string() |
| 78 | + .trim() |
| 79 | + .transform((arg) => (arg === "" ? null : arg)) |
| 80 | + .nullable(), |
| 81 | + }) |
| 82 | + .array() |
| 83 | + .transform((arr) => |
| 84 | + arr.filter((v) => !(v.code === null && v.name === null)), |
| 85 | + ) |
| 86 | + .superRefine((arg, ctx) => { |
| 87 | + for (const i in arg) { |
| 88 | + const variation = arg[i] |
| 89 | + if (!variation.code) { |
| 90 | + ctx.addIssue({ |
| 91 | + code: "custom", |
| 92 | + message: "Required.", |
| 93 | + path: [i, "code"], |
| 94 | + }) |
| 95 | + } |
| 96 | + |
| 97 | + if (!variation.name) { |
| 98 | + ctx.addIssue({ |
| 99 | + code: "custom", |
| 100 | + message: "Required.", |
| 101 | + path: [i, "name"], |
| 102 | + }) |
| 103 | + } |
| 104 | + } |
| 105 | + }) |
| 106 | + .superRefine((arg, ctx) => |
| 107 | + refineNoDuplicates( |
| 108 | + arg, |
| 109 | + ctx, |
| 110 | + "code", |
| 111 | + (v: GetElementType<typeof arg>) => v.code, |
| 112 | + ), |
| 113 | + ) |
| 114 | + .pipe( |
| 115 | + z |
| 116 | + .object({ |
| 117 | + code: z.string().trim().min(1), |
| 118 | + name: z.string().trim().min(1), |
| 119 | + }) |
| 120 | + .array(), |
| 121 | + ), |
| 122 | + suffix: z |
| 123 | + .string() |
| 124 | + .trim() |
| 125 | + .transform((arg) => (arg === "" ? null : arg)) |
| 126 | + .nullable() |
| 127 | + .default(null), |
| 128 | + }), |
| 129 | + ]) |
| 130 | + .nullable() |
| 131 | + .default({ hasShortcut: false }) |
| 132 | + |
| 133 | +export const compoundSchema = z.object({ |
| 134 | + id: z.number().int().optional(), |
| 135 | + name: z.string().trim().min(1), |
| 136 | + ingredients: z.array(ingredientSchema).min(1), |
| 137 | + shortcut: shortcutSchema, |
| 138 | + notes: z |
| 139 | + .string() |
| 140 | + .trim() |
| 141 | + .transform((arg) => (arg === "" ? null : arg)) |
| 142 | + .nullable() |
| 143 | + .default(null), |
| 144 | +}) |
| 145 | + |
| 146 | +export type CompoundFields = z.output<typeof compoundSchema> |
| 147 | +export type CompoundFieldsInput = z.input<typeof compoundSchema> |
| 148 | + |
| 149 | +export type NullPartialCompoundFields = Simplify< |
| 150 | + Merge< |
| 151 | + Merge< |
| 152 | + NullPartialDeep<CompoundFieldsInput>, |
| 153 | + Pick<CompoundFieldsInput, "id"> |
| 154 | + >, |
| 155 | + { |
| 156 | + shortcut?: NullPartialDeep< |
| 157 | + z.input<typeof shortcutSchema>, |
| 158 | + { ignoreKeys: "hasShortcut" } |
| 159 | + > |
| 160 | + ingredients: NullPartialIngredientFields[] |
| 161 | + } |
| 162 | + > |
| 163 | +> |
0 commit comments