Skip to content

Commit

Permalink
fix: IndexConfig should not allow broken config
Browse files Browse the repository at this point in the history
BREAKING CHANGE: some indexing configurations will no longer be accepted
  • Loading branch information
dominique-pfister committed Sep 25, 2024
1 parent 6a9ed46 commit ee7c379
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
13 changes: 13 additions & 0 deletions packages/helix-shared-config/src/IndexConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,17 @@ export class IndexConfig extends SchemaDerivedConfig {
this._version = this._cfg.version;
return this;
}

/**
* Validates the loaded configuration and coerces types and sets defaulst
*/
async validate() {
await super.validate();

if (this._document?.errors?.length) {
const detail = this._document.errors.map(({ message }) => (message)).join('\n');
throw new Error(`Invalid index configuration:
${detail}`);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,18 @@ describe('Index Config Loading', () => {
assert.deepEqual(actual, expected);
});

it('Does not trip over broken config', async () => {
it('Trips over broken config', async () => {
const cfg = new IndexConfig()
.withConfigPath(path.resolve(SPEC_ROOT, 'broken.yaml'));

await cfg.init();

assert.strictEqual(cfg.getQuery('foo', 'bar'), undefined);
await assert.rejects(async () => cfg.init(), /Implicit map keys need to be followed by map value/);
});

const actual = cfg.toJSON();
const expected = JSON.parse(await fs.readFile(path.resolve(SPEC_ROOT, 'broken.json'), 'utf-8'));
it('Trips over config with duplicate property', async () => {
const cfg = new IndexConfig()
.withConfigPath(path.resolve(SPEC_ROOT, 'duplicate.yaml'));

assert.deepEqual(actual, expected);
await assert.rejects(async () => cfg.init(), /Map keys must be unique at line 10, column 7/);
});

it('Does not trip over non-existing config', async () => {
Expand Down
17 changes: 0 additions & 17 deletions packages/helix-shared-config/test/specs/queryconfigs/broken.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 1

indices:
blog-posts:
properties:
author:
select: main > div:nth-of-type(3) > p:nth-of-type(1)
value: |
${match('by (.*)')}
author:
select: other
value: ${match('by (.*)')}

0 comments on commit ee7c379

Please sign in to comment.