Skip to content

Commit

Permalink
Validate identifers when reading features/groups/snapshots
Browse files Browse the repository at this point in the history
This is in order to simplify the schema:
web-platform-dx#1060 (comment)
  • Loading branch information
foolip committed Jul 4, 2024
1 parent cb6c00b commit afe156e
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ function scrub(data: any) {
return data as FeatureData;
}

const identifierPattern = /^[a-z0-9-]*$/;

function* yamlEntries(root: string): Generator<[string, any]> {
const filePaths = new fdir()
.withBasePath()
Expand All @@ -48,9 +50,13 @@ function* yamlEntries(root: string): Generator<[string, any]> {
for (const fp of filePaths) {
// The feature identifier/key is the filename without extension.
const { name: key } = path.parse(fp);
const distPath = `${fp}.dist`;

if (!identifierPattern.test(key)) {
throw new Error(`${key} is not a valid identifier (must be lowercase a-z, 0-9, and hyphens)`);
}

const data = YAML.parse(fs.readFileSync(fp, { encoding: 'utf-8'}));
const distPath = `${fp}.dist`;
if (fs.existsSync(distPath)) {
const dist = YAML.parse(fs.readFileSync(distPath, { encoding: 'utf-8'}));
Object.assign(data, dist);
Expand Down

0 comments on commit afe156e

Please # to comment.