1- import { z } from "zod" ;
1+ import { z } from "zod" ;
22
3- import { beanconqueror } from "@/lib/beanconqueror/proto/generated/beanconqueror" ;
3+ import { beanconqueror } from "@/lib/beanconqueror/proto/generated/beanconqueror" ;
44
55import BeanRoastingType = beanconqueror . BeanRoastingType ;
66import Roast = beanconqueror . Roast ;
77import BeanMix = beanconqueror . BeanMix ;
88
9- function zodEnumFromObjKeys < K extends string > ( obj : Record < K , unknown > ) : z . ZodEnum < [ K , ...K [ ] ] > {
10- const [ firstKey , ...otherKeys ] = Object . keys ( obj ) as K [ ] ;
11- return z . enum ( [ firstKey , ...otherKeys ] ) ;
9+ function zodEnumFromObjKeys < K extends string > ( obj : Record < K , unknown > ) : z . ZodEnum < [ K , ...K [ ] ] > {
10+ const [ firstKey , ...otherKeys ] = Object . keys ( obj ) as K [ ] ;
11+ return z . enum ( [ firstKey , ...otherKeys ] ) ;
1212}
1313
1414export const varietyInformationShape = z . object ( {
15- country : z . optional ( z . string ( ) ) ,
16- region : z . optional ( z . string ( ) ) ,
17- farm : z . optional ( z . string ( ) ) ,
18- farmer : z . optional ( z . string ( ) ) ,
19- elevation : z . optional ( z . string ( ) ) ,
20- variety : z . optional ( z . string ( ) ) ,
21- processing : z . optional ( z . string ( ) ) ,
22- harvested : z . optional ( z . string ( ) ) ,
23- percentage : z . optional ( z . string ( ) ) ,
24- certification : z . optional ( z . string ( ) ) ,
25- purchasePrice : z . optional ( z . string ( ) ) ,
26- fobPrice : z . optional ( z . string ( ) ) ,
15+ country : z . optional ( z . string ( ) ) ,
16+ region : z . optional ( z . string ( ) ) ,
17+ farm : z . optional ( z . string ( ) ) ,
18+ farmer : z . optional ( z . string ( ) ) ,
19+ elevation : z . optional ( z . string ( ) ) ,
20+ variety : z . optional ( z . string ( ) ) ,
21+ processing : z . optional ( z . string ( ) ) ,
22+ harvested : z . optional ( z . string ( ) ) ,
23+ percentage : z . optional ( z . string ( ) ) ,
24+ certification : z . optional ( z . string ( ) ) ,
25+ purchasePrice : z . optional ( z . string ( ) ) ,
26+ fobPrice : z . optional ( z . string ( ) ) ,
2727} ) ;
2828
2929export type varietyInformationType = z . infer < typeof varietyInformationShape >
3030
3131export const beanInformationFormSchema = z . object ( {
32- coffeeName : z . string ( ) ,
33- roaster : z . optional ( z . string ( ) ) ,
34- roastingDate : z . optional ( z . date ( ) ) ,
35- beanRoastingType : z . optional ( zodEnumFromObjKeys ( BeanRoastingType ) ) ,
36- degreeOfRoast : z . optional ( z . array ( z . number ( ) . min ( 0 ) . max ( 5 ) ) ) ,
37- roast : z . optional ( zodEnumFromObjKeys ( Roast ) ) ,
38- beanMix : z . optional ( zodEnumFromObjKeys ( BeanMix ) ) ,
39- weight : z . optional ( z . string ( ) ) , // Note: string instead of decimal because of proto
40- cost : z . optional ( z . string ( ) . min ( 0 ) ) , // Note: string instead of decimal because of proto
41- flavourProfile : z . optional ( z . string ( ) ) ,
42- cuppingPoints : z . optional ( z . string ( ) ) ,
43- decaffeinated : z . optional ( z . boolean ( ) ) ,
44- website : z . optional ( z . string ( ) ) ,
45- eanArticle : z . optional ( z . string ( ) ) ,
46- notes : z . optional ( z . string ( ) ) ,
47- varietyInformation : z . optional ( z . array ( varietyInformationShape ) . min ( 0 ) )
32+ coffeeName : z . string ( ) ,
33+ roaster : z . optional ( z . string ( ) ) ,
34+ roastingDate : z . optional (
35+ z . preprocess ( ( val ) => ( typeof val === "string" ? new Date ( val ) : val ) , z . date ( ) )
36+ ) ,
37+ beanRoastingType : z . optional ( zodEnumFromObjKeys ( BeanRoastingType ) ) ,
38+ degreeOfRoast : z . optional ( z . array ( z . number ( ) . min ( 0 ) . max ( 5 ) ) ) ,
39+ roast : z . optional ( zodEnumFromObjKeys ( Roast ) ) ,
40+ beanMix : z . optional ( zodEnumFromObjKeys ( BeanMix ) ) ,
41+ weight : z . optional ( z . string ( ) ) , // Note: string instead of decimal because of proto
42+ cost : z . optional ( z . string ( ) . min ( 0 ) ) , // Note: string instead of decimal because of proto
43+ flavourProfile : z . optional ( z . string ( ) ) ,
44+ cuppingPoints : z . optional ( z . string ( ) ) ,
45+ decaffeinated : z . optional ( z . boolean ( ) ) ,
46+ website : z . optional ( z . string ( ) ) ,
47+ eanArticle : z . optional ( z . string ( ) ) ,
48+ notes : z . optional ( z . string ( ) ) ,
49+ varietyInformation : z . optional ( z . array ( varietyInformationShape ) . min ( 0 ) )
4850} ) ;
4951
5052export type beanInformationFormSchema = z . infer < typeof beanInformationFormSchema > ;
5153
5254export const defaultVarietyInformation = {
53- country : "" ,
54- region : "" ,
55- farm : "" ,
56- farmer : "" ,
57- elevation : "" ,
58- variety : "" ,
59- processing : "" ,
60- harvested : "" ,
61- percentage : "" ,
62- certification : "" ,
63- purchasePrice : "" ,
64- fobPrice : "" ,
65- } ;
55+ country : "" ,
56+ region : "" ,
57+ farm : "" ,
58+ farmer : "" ,
59+ elevation : "" ,
60+ variety : "" ,
61+ processing : "" ,
62+ harvested : "" ,
63+ percentage : "" ,
64+ certification : "" ,
65+ purchasePrice : "" ,
66+ fobPrice : "" ,
67+ } ;
0 commit comments