@@ -38,7 +38,7 @@ export const slugSchema = z
3838 'The slug has to follow the pattern [0-9a-z] followed by multiple optional groups of -[0-9a-z]. e.g. my-slug' ,
3939 } )
4040 . max ( MAX_SLUG_LENGTH , {
41- message : `slug can be max ${ MAX_SLUG_LENGTH } characters long` ,
41+ message : `The slug can be max ${ MAX_SLUG_LENGTH } characters long` ,
4242 } ) ;
4343
4444/** Schema for a general description property */
@@ -105,7 +105,7 @@ export function metaSchema(options?: {
105105export const filePathSchema = z
106106 . string ( )
107107 . trim ( )
108- . min ( 1 , { message : 'path is invalid' } ) ;
108+ . min ( 1 , { message : 'The path is invalid' } ) ;
109109
110110/** Schema for a fileNameSchema */
111111export const fileNameSchema = z
@@ -114,7 +114,7 @@ export const fileNameSchema = z
114114 . regex ( filenameRegex , {
115115 message : `The filename has to be valid` ,
116116 } )
117- . min ( 1 , { message : 'file name is invalid' } ) ;
117+ . min ( 1 , { message : 'The file name is invalid' } ) ;
118118
119119/** Schema for a positiveInt */
120120export const positiveIntSchema = z . number ( ) . int ( ) . positive ( ) ;
@@ -167,26 +167,43 @@ export function scorableSchema<T extends ReturnType<typeof weightedRefSchema>>(
167167 duplicateCheckFn : ( metrics : z . infer < T > [ ] ) => false | string [ ] ,
168168 duplicateMessageFn : ( metrics : z . infer < T > [ ] ) => string ,
169169) {
170- return z . object (
171- {
172- slug : slugSchema . describe ( 'Human-readable unique ID, e.g. "performance"' ) ,
173- refs : z
174- . array ( refSchema )
175- . min ( 1 )
176- // refs are unique
177- . refine (
178- refs => ! duplicateCheckFn ( refs ) ,
179- refs => ( {
180- message : duplicateMessageFn ( refs ) ,
181- } ) ,
182- )
183- // categories weights are correct
184- . refine ( hasNonZeroWeightedRef , ( ) => ( {
185- message :
186- 'In a category there has to be at least one ref with weight > 0' ,
187- } ) ) ,
188- } ,
189- { description } ,
170+ return (
171+ z
172+ . object (
173+ {
174+ slug : slugSchema . describe (
175+ 'Human-readable unique ID, e.g. "performance"' ,
176+ ) ,
177+ refs : z
178+ . array ( refSchema )
179+ . min ( 1 )
180+ // refs are unique
181+ . refine (
182+ refs => ! duplicateCheckFn ( refs ) ,
183+ refs => ( {
184+ message : duplicateMessageFn ( refs ) ,
185+ } ) ,
186+ ) ,
187+ } ,
188+ { description } ,
189+ )
190+ // category weights are correct
191+ . superRefine ( ( { slug, refs } , ctx ) => {
192+ if ( refs . length === 0 ) {
193+ ctx . addIssue ( {
194+ code : z . ZodIssueCode . custom ,
195+ message : `In category ${ slug } , there has to be at least one ref` ,
196+ path : [ 'refs' ] ,
197+ } ) ;
198+ } else if ( ! hasNonZeroWeightedRef ( refs ) ) {
199+ const affectedRefs = refs . map ( ref => ref . slug ) . join ( ', ' ) ;
200+ ctx . addIssue ( {
201+ code : z . ZodIssueCode . custom ,
202+ message : `In category ${ slug } , there has to be at least one ref with weight > 0. Affected refs: ${ affectedRefs } ` ,
203+ path : [ 'refs' ] ,
204+ } ) ;
205+ }
206+ } )
190207 ) ;
191208}
192209
0 commit comments