|
1 | | -import { z } from 'zod'; |
| 1 | +import { Schema, z } from 'zod'; |
2 | 2 | import { pluginConfigSchema } from './plugin-config'; |
3 | 3 | import { categoryConfigSchema } from './category-config'; |
4 | 4 | import { uploadConfigSchema } from './upload-config'; |
@@ -28,37 +28,49 @@ import { |
28 | 28 | * console.error('Invalid plugin config:', validationResult.error); |
29 | 29 | * } |
30 | 30 | */ |
31 | | -export const coreConfigSchema = z |
32 | | - .object({ |
33 | | - plugins: z.array(pluginConfigSchema, { |
34 | | - description: |
35 | | - 'List of plugins to be used (official, community-provided, or custom)', |
36 | | - }), |
37 | | - /** portal configuration for persisting results */ |
38 | | - persist: persistConfigSchema, |
39 | | - /** portal configuration for uploading results */ |
40 | | - upload: uploadConfigSchema.optional(), |
41 | | - categories: z |
42 | | - .array(categoryConfigSchema, { |
43 | | - description: 'Categorization of individual audits', |
44 | | - }) |
45 | | - // categories slugs are unique |
| 31 | +export const unrefinedCoreConfigSchema = z.object({ |
| 32 | + plugins: z.array(pluginConfigSchema, { |
| 33 | + description: |
| 34 | + 'List of plugins to be used (official, community-provided, or custom)', |
| 35 | + }), |
| 36 | + /** portal configuration for persisting results */ |
| 37 | + persist: persistConfigSchema, |
| 38 | + /** portal configuration for uploading results */ |
| 39 | + upload: uploadConfigSchema.optional(), |
| 40 | + categories: z |
| 41 | + .array(categoryConfigSchema, { |
| 42 | + description: 'Categorization of individual audits', |
| 43 | + }) |
| 44 | + // categories slugs are unique |
| 45 | + .refine( |
| 46 | + categoryCfg => !getDuplicateSlugCategories(categoryCfg), |
| 47 | + categoryCfg => ({ |
| 48 | + message: duplicateSlugCategoriesErrorMsg(categoryCfg), |
| 49 | + }), |
| 50 | + ), |
| 51 | +}); |
| 52 | + |
| 53 | +export const coreConfigSchema = refineCoreConfig(unrefinedCoreConfigSchema); |
| 54 | + |
| 55 | +/** |
| 56 | + * Add refinements to coreConfigSchema |
| 57 | + * workaround for zod issue: https://github.com/colinhacks/zod/issues/454 |
| 58 | + * |
| 59 | + */ |
| 60 | +export function refineCoreConfig(schema: Schema): Schema { |
| 61 | + return ( |
| 62 | + schema |
| 63 | + // categories point to existing audit or group refs |
46 | 64 | .refine( |
47 | | - categoryCfg => !getDuplicateSlugCategories(categoryCfg), |
48 | | - categoryCfg => ({ |
49 | | - message: duplicateSlugCategoriesErrorMsg(categoryCfg), |
| 65 | + coreCfg => !getMissingRefsForCategories(coreCfg), |
| 66 | + coreCfg => ({ |
| 67 | + message: missingRefsForCategoriesErrorMsg(coreCfg), |
50 | 68 | }), |
51 | | - ), |
52 | | - }) |
53 | | - // categories point to existing audit or group refs |
54 | | - .refine( |
55 | | - coreCfg => !getMissingRefsForCategories(coreCfg), |
56 | | - coreCfg => ({ |
57 | | - message: missingRefsForCategoriesErrorMsg(coreCfg), |
58 | | - }), |
| 69 | + ) |
59 | 70 | ); |
| 71 | +} |
60 | 72 |
|
61 | | -export type CoreConfigSchema = z.infer<typeof coreConfigSchema>; |
| 73 | +export type CoreConfig = z.infer<typeof coreConfigSchema>; |
62 | 74 |
|
63 | 75 | // helper for validator: categories point to existing audit or group refs |
64 | 76 | function missingRefsForCategoriesErrorMsg(coreCfg) { |
@@ -89,9 +101,8 @@ function getMissingRefsForCategories(coreCfg) { |
89 | 101 | if (Array.isArray(missingAuditRefs) && missingAuditRefs.length > 0) { |
90 | 102 | missingRefs.push(...missingAuditRefs); |
91 | 103 | } |
92 | | - const groupRefsFromCategory = coreCfg.categories.flatMap( |
93 | | - ({ metrics }) => |
94 | | - metrics.filter(({ ref }) => isGroupRef(ref)).map(({ ref }) => ref), // plg#group:perf |
| 104 | + const groupRefsFromCategory = coreCfg.categories.flatMap(({ metrics }) => |
| 105 | + metrics.filter(({ ref }) => isGroupRef(ref)).map(({ ref }) => ref), |
95 | 106 | ); |
96 | 107 | const groupRefsFromPlugins = coreCfg.plugins.flatMap(({ groups, meta }) => { |
97 | 108 | return groups.map(({ slug }) => `${meta.slug}#group:${slug}`); |
|
0 commit comments