-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcategory-config.ts
More file actions
78 lines (71 loc) · 2.18 KB
/
category-config.ts
File metadata and controls
78 lines (71 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { z } from 'zod';
import {
createDuplicateSlugsCheck,
createDuplicatesCheck,
} from './implementation/checks.js';
import {
metaSchema,
nonnegativeNumberSchema,
scorableSchema,
slugSchema,
weightedRefSchema,
} from './implementation/schemas.js';
import { formatRef } from './implementation/utils.js';
export const categoryRefSchema = weightedRefSchema(
'Weighted references to audits and/or groups for the category',
'Slug of an audit or group (depending on `type`)',
).extend({
type: z
.enum(['audit', 'group'])
.describe(
'Discriminant for reference kind, affects where `slug` is looked up',
),
plugin: slugSchema.describe(
'Plugin slug (plugin should contain referenced audit or group)',
),
});
export type CategoryRef = z.infer<typeof categoryRefSchema>;
export const categoryConfigSchema = scorableSchema(
'Category with a score calculated from audits and groups from various plugins',
categoryRefSchema,
createDuplicatesCheck(
serializeCategoryRefTarget,
duplicates =>
`Category has duplicate references: ${formatSerializedCategoryRefTargets(duplicates)}`,
),
)
.extend(
metaSchema({
titleDescription: 'Category Title',
docsUrlDescription: 'Category docs URL',
descriptionDescription: 'Category description',
description: 'Meta info for category',
}).shape,
)
.extend({
scoreTarget: nonnegativeNumberSchema
.max(1)
.describe('Pass/fail score threshold (0-1)')
.optional(),
});
export type CategoryConfig = z.infer<typeof categoryConfigSchema>;
const CATEGORY_REF_SEP = '||';
function serializeCategoryRefTarget(ref: CategoryRef): string {
return [ref.type, ref.plugin, ref.slug].join(CATEGORY_REF_SEP);
}
function formatSerializedCategoryRefTargets(keys: string[]): string {
return keys
.map(key => {
const [type, plugin, slug] = key.split(CATEGORY_REF_SEP) as [
'group' | 'audit',
string,
string,
];
return formatRef({ type, plugin, slug });
})
.join(', ');
}
export const categoriesSchema = z
.array(categoryConfigSchema)
.check(createDuplicateSlugsCheck('Category'))
.describe('Categorization of individual audits');