Skip to content

Commit 677660c

Browse files
authored
Validate identifers when reading features/groups/snapshots (#1317)
This is in order to simplify the schema: #1060 (comment)
1 parent 707f11b commit 677660c

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ function scrub(data: any) {
3838
return data as FeatureData;
3939
}
4040

41+
const identifierPattern = /^[a-z0-9-]*$/;
42+
4143
function* yamlEntries(root: string): Generator<[string, any]> {
4244
const filePaths = new fdir()
4345
.withBasePath()
@@ -48,9 +50,13 @@ function* yamlEntries(root: string): Generator<[string, any]> {
4850
for (const fp of filePaths) {
4951
// The feature identifier/key is the filename without extension.
5052
const { name: key } = path.parse(fp);
51-
const distPath = `${fp}.dist`;
53+
54+
if (!identifierPattern.test(key)) {
55+
throw new Error(`${key} is not a valid identifier (must be lowercase a-z, 0-9, and hyphens)`);
56+
}
5257

5358
const data = YAML.parse(fs.readFileSync(fp, { encoding: 'utf-8'}));
59+
const distPath = `${fp}.dist`;
5460
if (fs.existsSync(distPath)) {
5561
const dist = YAML.parse(fs.readFileSync(distPath, { encoding: 'utf-8'}));
5662
Object.assign(data, dist);

0 commit comments

Comments
 (0)