-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.js
More file actions
216 lines (188 loc) · 6.51 KB
/
Copy pathvalidate.js
File metadata and controls
216 lines (188 loc) · 6.51 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env node
/**
* Speechstack recipe validation.
*
* Runs:
* 1. JSON schema validation (via AJV)
* 2. ID and slug uniqueness
* 3. ID-must-match-filename check
* 4. Vendor allowlist check
* 5. Use case / industry controlled vocabulary check
* 6. Prompt file existence check
* 7. Title rules (no em/en dashes, no vendor brand names)
*
* Exits 0 on success, 1 on failure. Designed to run both locally and in CI.
*/
const fs = require('fs');
const path = require('path');
const Ajv = require('ajv');
const addFormats = require('ajv-formats');
const ROOT = path.join(__dirname, '..');
const RECIPES_DIR = path.join(ROOT, 'recipes');
const PROMPTS_DIR = path.join(RECIPES_DIR, 'prompts');
const SCHEMA_PATH = path.join(ROOT, 'schema', 'recipe.schema.json');
const VENDORS_PATH = path.join(ROOT, 'data', 'vendors.json');
const USE_CASES_PATH = path.join(ROOT, 'data', 'use-cases.json');
const INDUSTRIES_PATH = path.join(ROOT, 'data', 'industries.json');
const errors = [];
const warnings = [];
function readJson(p) {
return JSON.parse(fs.readFileSync(p, 'utf8'));
}
const schema = readJson(SCHEMA_PATH);
const vendors = readJson(VENDORS_PATH);
const useCases = readJson(USE_CASES_PATH);
const industries = readJson(INDUSTRIES_PATH);
const ajv = new Ajv({ allErrors: true, strict: false });
addFormats(ajv);
const validate = ajv.compile(schema);
// Brand tokens derived from data/vendors.json. We take the first space-delimited
// word of each vendor entry, then the first hyphen chunk of that word — so
// "Deepgram Nova-3" → "Deepgram" and "GPT-4o-mini" → "GPT". `Custom` and `Web`
// are sentinel values, not brands, so we skip them.
const VENDOR_BRAND_SKIP = new Set(['Custom', 'Web']);
function escapeRegExp(s) {
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function getVendorBrandTokens(vendorsData) {
const tokens = new Set();
for (const category of Object.values(vendorsData)) {
if (!Array.isArray(category)) continue;
for (const value of category) {
if (typeof value !== 'string') continue;
const firstWord = value.split(/\s+/)[0] || '';
const brand = firstWord.split('-')[0];
if (brand && !VENDOR_BRAND_SKIP.has(brand)) tokens.add(brand);
}
}
return [...tokens];
}
function checkTitleRules(title, brandTokens) {
const issues = [];
if (/[—–]/.test(title)) {
issues.push(
'title contains an em or en dash — use a colon or rewrite the title. ' +
'Good titles describe the use case in plain words.'
);
}
for (const token of brandTokens) {
const re = new RegExp(`\\b${escapeRegExp(token)}\\b`, 'i');
if (re.test(title)) {
issues.push(
`title contains vendor name "${token}" — keep titles focused on the use case. ` +
'The stack is already shown elsewhere on the recipe card.'
);
}
}
return issues;
}
const brandTokens = getVendorBrandTokens(vendors);
const recipeFiles = fs
.readdirSync(RECIPES_DIR)
.filter((f) => f.endsWith('.json') && !f.startsWith('_'));
const seenIds = new Set();
const seenSlugs = new Set();
for (const file of recipeFiles) {
const filePath = path.join(RECIPES_DIR, file);
const baseName = file.replace(/\.json$/, '');
let recipe;
try {
recipe = readJson(filePath);
} catch (e) {
errors.push(`[${file}] Invalid JSON: ${e.message}`);
continue;
}
// 1. Schema validation
if (!validate(recipe)) {
for (const err of validate.errors) {
errors.push(`[${file}] Schema: ${err.instancePath || '/'} ${err.message}`);
}
continue;
}
// 2. ID must match filename
if (recipe.id !== baseName) {
errors.push(
`[${file}] ID mismatch: file is "${baseName}.json" but id is "${recipe.id}". They must match.`
);
}
// 3. ID uniqueness
if (seenIds.has(recipe.id)) {
errors.push(`[${file}] Duplicate id: "${recipe.id}" is used in multiple files.`);
}
seenIds.add(recipe.id);
// 4. Slug uniqueness
if (seenSlugs.has(recipe.slug)) {
errors.push(`[${file}] Duplicate slug: "${recipe.slug}" is used in multiple files.`);
}
seenSlugs.add(recipe.slug);
// 5. Vendor allowlist
if (!vendors.framework.includes(recipe.framework)) {
errors.push(
`[${file}] Unknown framework "${recipe.framework}". Allowed: ${vendors.framework.join(', ')}. ` +
`To add a new framework, edit data/vendors.json in the same PR.`
);
}
if (recipe.pipeline) {
for (const layer of ['stt', 'llm', 'tts', 'telephony']) {
const value = recipe.pipeline[layer];
if (value === null && layer === 'telephony') continue;
if (!vendors[layer].includes(value)) {
errors.push(
`[${file}] Unknown ${layer} vendor "${value}". Allowed values are in data/vendors.json. ` +
`To add a new vendor, edit data/vendors.json in the same PR.`
);
}
}
}
// 6. Controlled vocabularies
if (!useCases.includes(recipe.use_case)) {
errors.push(
`[${file}] Unknown use_case "${recipe.use_case}". Allowed: ${useCases.join(', ')}.`
);
}
if (!industries.includes(recipe.industry)) {
errors.push(
`[${file}] Unknown industry "${recipe.industry}". Allowed: ${industries.join(', ')}.`
);
}
// 7. Title rules: no em/en dashes, no vendor brand names
if (typeof recipe.title === 'string') {
for (const issue of checkTitleRules(recipe.title, brandTokens)) {
errors.push(`[${file}] ${issue}`);
}
}
// 8. Prompt file existence
if (recipe.prompt_file) {
const promptPath = path.join(RECIPES_DIR, recipe.prompt_file);
if (!fs.existsSync(promptPath)) {
errors.push(
`[${file}] prompt_file references "${recipe.prompt_file}" but the file does not exist.`
);
}
}
// 9. Either raw_prompt OR prompt_file should be present (warning, not error)
if (!recipe.raw_prompt && !recipe.prompt_file) {
warnings.push(
`[${file}] No raw_prompt or prompt_file. The recipe will be missing prompt content on the site.`
);
}
// 10. Both is suspicious
if (recipe.raw_prompt && recipe.prompt_file) {
warnings.push(
`[${file}] Both raw_prompt and prompt_file are set. The site will prefer prompt_file.`
);
}
}
// Report
if (warnings.length) {
console.log('\n⚠️ Warnings:');
for (const w of warnings) console.log(' ' + w);
}
if (errors.length) {
console.log('\n❌ Validation failed:');
for (const e of errors) console.log(' ' + e);
console.log(`\n${errors.length} error(s) across ${recipeFiles.length} recipe(s).`);
process.exit(1);
}
console.log(`\n✅ All ${recipeFiles.length} recipe(s) valid.`);
process.exit(0);