Additional validation of custom macro keys
This commit is contained in:
parent
8dab4ecb06
commit
01d38f9218
|
@ -30,14 +30,25 @@ export class MacrosParser {
|
||||||
throw new Error('Macro key must be a string');
|
throw new Error('Macro key must be a string');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.#macros.has(key)) {
|
// Allowing surrounding whitespace would just create more confusion...
|
||||||
console.warn(`Macro ${key} is already registered`);
|
key = key.trim();
|
||||||
|
|
||||||
|
if (!key) {
|
||||||
|
throw new Error('Macro key must not be empty or whitespace only');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key.startsWith('{{') || key.endsWith('}}')) {
|
||||||
|
throw new Error('Macro key must not include the surrounding braces');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof value !== 'string' && typeof value !== 'function') {
|
if (typeof value !== 'string' && typeof value !== 'function') {
|
||||||
throw new Error('Macro value must be a string or a function that returns a string');
|
throw new Error('Macro value must be a string or a function that returns a string');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.#macros.has(key)) {
|
||||||
|
console.warn(`Macro ${key} is already registered`);
|
||||||
|
}
|
||||||
|
|
||||||
this.#macros.set(key, value);
|
this.#macros.set(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue