@@ -25,55 +25,36 @@ export function executionMetaSchema(
25
25
} ) ;
26
26
}
27
27
28
- /**
29
- * Schema for a slug of a categories, plugins or audits.
30
- * @param description
31
- */
32
- export function slugSchema (
33
- description = 'Unique ID (human-readable, URL-safe)' ,
34
- ) {
35
- return z
36
- . string ( { description } )
37
- . regex ( slugRegex , {
38
- message :
39
- 'The slug has to follow the pattern [0-9a-z] followed by multiple optional groups of -[0-9a-z]. e.g. my-slug' ,
40
- } )
41
- . max ( MAX_SLUG_LENGTH , {
42
- message : `slug can be max ${ MAX_SLUG_LENGTH } characters long` ,
43
- } ) ;
44
- }
28
+ /** Schema for a slug of a categories, plugins or audits. */
29
+ export const slugSchema = z
30
+ . string ( { description : 'Unique ID (human-readable, URL-safe)' } )
31
+ . regex ( slugRegex , {
32
+ message :
33
+ 'The slug has to follow the pattern [0-9a-z] followed by multiple optional groups of -[0-9a-z]. e.g. my-slug' ,
34
+ } )
35
+ . max ( MAX_SLUG_LENGTH , {
36
+ message : `slug can be max ${ MAX_SLUG_LENGTH } characters long` ,
37
+ } ) ;
45
38
46
- /**
47
- * Schema for a general description property
48
- * @param description
49
- */
50
- export function descriptionSchema ( description = 'Description (markdown)' ) {
51
- return z . string ( { description } ) . max ( MAX_DESCRIPTION_LENGTH ) . optional ( ) ;
52
- }
39
+ /** Schema for a general description property */
40
+ export const descriptionSchema = z
41
+ . string ( { description : 'Description (markdown)' } )
42
+ . max ( MAX_DESCRIPTION_LENGTH )
43
+ . optional ( ) ;
53
44
54
- /**
55
- * Schema for a docsUrl
56
- * @param description
57
- */
58
- export function docsUrlSchema ( description = 'Documentation site' ) {
59
- return urlSchema ( description ) . optional ( ) . or ( z . string ( ) . max ( 0 ) ) ; // allow empty string (no URL validation)
60
- }
45
+ /* Schema for a URL */
46
+ export const urlSchema = z . string ( ) . url ( ) ;
61
47
62
- /**
63
- * Schema for a URL
64
- * @param description
65
- */
66
- export function urlSchema ( description : string ) {
67
- return z . string ( { description } ) . url ( ) ;
68
- }
48
+ /** Schema for a docsUrl */
49
+ export const docsUrlSchema = urlSchema
50
+ . optional ( )
51
+ . or ( z . literal ( '' ) )
52
+ . describe ( 'Documentation site' ) ; // allow empty string (no URL validation)
69
53
70
- /**
71
- * Schema for a title of a plugin, category and audit
72
- * @param description
73
- */
74
- export function titleSchema ( description = 'Descriptive name' ) {
75
- return z . string ( { description } ) . max ( MAX_TITLE_LENGTH ) ;
76
- }
54
+ /** Schema for a title of a plugin, category and audit */
55
+ export const titleSchema = z
56
+ . string ( { description : 'Descriptive name' } )
57
+ . max ( MAX_TITLE_LENGTH ) ;
77
58
78
59
/**
79
60
* Used for categories, plugins and audits
@@ -93,46 +74,39 @@ export function metaSchema(options?: {
93
74
} = options ?? { } ;
94
75
return z . object (
95
76
{
96
- title : titleSchema ( titleDescription ) ,
97
- description : descriptionSchema ( descriptionDescription ) ,
98
- docsUrl : docsUrlSchema ( docsUrlDescription ) ,
77
+ title : titleDescription
78
+ ? titleSchema . describe ( titleDescription )
79
+ : titleSchema ,
80
+ description : descriptionDescription
81
+ ? descriptionSchema . describe ( descriptionDescription )
82
+ : descriptionSchema ,
83
+ docsUrl : docsUrlDescription
84
+ ? docsUrlSchema . describe ( docsUrlDescription )
85
+ : docsUrlSchema ,
99
86
} ,
100
87
{ description } ,
101
88
) ;
102
89
}
103
90
104
- /**
105
- * Schema for a generalFilePath
106
- * @param description
107
- */
108
- export function filePathSchema ( description : string ) {
109
- return z
110
- . string ( { description } )
111
- . trim ( )
112
- . min ( 1 , { message : 'path is invalid' } ) ;
113
- }
91
+ /** Schema for a generalFilePath */
92
+ export const filePathSchema = z
93
+ . string ( )
94
+ . trim ( )
95
+ . min ( 1 , { message : 'path is invalid' } ) ;
114
96
115
- /**
116
- * Schema for a fileNameSchema
117
- * @param description
118
- */
119
- export function fileNameSchema ( description : string ) {
120
- return z
121
- . string ( { description } )
122
- . trim ( )
123
- . regex ( filenameRegex , {
124
- message : `The filename has to be valid` ,
125
- } )
126
- . min ( 1 , { message : 'file name is invalid' } ) ;
127
- }
97
+ /** Schema for a fileNameSchema */
98
+ export const fileNameSchema = z
99
+ . string ( )
100
+ . trim ( )
101
+ . regex ( filenameRegex , {
102
+ message : `The filename has to be valid` ,
103
+ } )
104
+ . min ( 1 , { message : 'file name is invalid' } ) ;
128
105
129
- /**
130
- * Schema for a positiveInt
131
- * @param description
132
- */
133
- export function positiveIntSchema ( description : string ) {
134
- return z . number ( { description } ) . int ( ) . nonnegative ( ) ;
135
- }
106
+ /** Schema for a positiveInt */
107
+ export const positiveIntSchema = z . number ( ) . int ( ) . positive ( ) ;
108
+
109
+ export const nonnegativeIntSchema = z . number ( ) . int ( ) . nonnegative ( ) ;
136
110
137
111
export function packageVersionSchema < TRequired extends boolean > ( options ?: {
138
112
versionDescription ?: string ;
@@ -154,24 +128,19 @@ export function packageVersionSchema<TRequired extends boolean>(options?: {
154
128
} > ;
155
129
}
156
130
157
- /**
158
- * Schema for a weight
159
- * @param description
160
- */
161
- export function weightSchema (
162
- description = 'Coefficient for the given score (use weight 0 if only for display)' ,
163
- ) {
164
- return positiveIntSchema ( description ) ;
165
- }
131
+ /** Schema for a weight */
132
+ export const weightSchema = nonnegativeIntSchema . describe (
133
+ 'Coefficient for the given score (use weight 0 if only for display)' ,
134
+ ) ;
166
135
167
136
export function weightedRefSchema (
168
137
description : string ,
169
138
slugDescription : string ,
170
139
) {
171
140
return z . object (
172
141
{
173
- slug : slugSchema ( slugDescription ) ,
174
- weight : weightSchema ( 'Weight used to calculate score' ) ,
142
+ slug : slugSchema . describe ( slugDescription ) ,
143
+ weight : weightSchema . describe ( 'Weight used to calculate score' ) ,
175
144
} ,
176
145
{ description } ,
177
146
) ;
@@ -187,7 +156,7 @@ export function scorableSchema<T extends ReturnType<typeof weightedRefSchema>>(
187
156
) {
188
157
return z . object (
189
158
{
190
- slug : slugSchema ( 'Human-readable unique ID, e.g. "performance"' ) ,
159
+ slug : slugSchema . describe ( 'Human-readable unique ID, e.g. "performance"' ) ,
191
160
refs : z
192
161
. array ( refSchema )
193
162
. min ( 1 )
@@ -199,8 +168,9 @@ export function scorableSchema<T extends ReturnType<typeof weightedRefSchema>>(
199
168
} ) ,
200
169
)
201
170
// categories weights are correct
202
- . refine ( hasWeightedRefsInCategories , ( ) => ( {
203
- message : `In a category there has to be at least one ref with weight > 0` ,
171
+ . refine ( hasNonZeroWeightedRef , ( ) => ( {
172
+ message :
173
+ 'In a category there has to be at least one ref with weight > 0' ,
204
174
} ) ) ,
205
175
} ,
206
176
{ description } ,
@@ -214,6 +184,6 @@ export type MaterialIcon = z.infer<typeof materialIconSchema>;
214
184
215
185
type Ref = { weight : number } ;
216
186
217
- function hasWeightedRefsInCategories ( categoryRefs : Ref [ ] ) {
218
- return categoryRefs . reduce ( ( acc , { weight } ) => weight + acc , 0 ) !== 0 ;
187
+ function hasNonZeroWeightedRef ( refs : Ref [ ] ) {
188
+ return refs . reduce ( ( acc , { weight } ) => weight + acc , 0 ) !== 0 ;
219
189
}
0 commit comments