Add more docs, add more checks, add more tests

This commit is contained in:
Pieter Vander Vennet 2021-03-14 01:40:35 +01:00
parent a50b9fba59
commit 2c3c110624
7 changed files with 162 additions and 13 deletions

View file

@ -82,8 +82,11 @@ export default class TagRenderingConfig {
this.multiAnswer = json.multiAnswer ?? false
if (json.mappings) {
this.mappings = json.mappings.map((mapping, i) => {
if (mapping.then === undefined) {
throw `${context}.mapping[${i}]: Invalid mapping: if without body`
}
@ -104,7 +107,7 @@ export default class TagRenderingConfig {
hideInAnswer: hideInAnswer
};
if (this.question) {
if (hideInAnswer !== true && !mp.if.isUsableAsAnswer()) {
if (hideInAnswer !== true && mp.if !== undefined && !mp.if.isUsableAsAnswer()) {
throw `${context}.mapping[${i}].if: This value cannot be used to answer a question, probably because it contains a regex or an OR. Either change it or set 'hideInAnswer'`
}
@ -128,6 +131,32 @@ export default class TagRenderingConfig {
if(this.render && this.question && this.freeform === undefined){
throw `${context}: Detected a tagrendering which takes input without freeform key in ${context}`
}
if(!json.multiAnswer && this.mappings !== undefined && this.question !== undefined){
let keys = []
for (let i = 0; i < this.mappings.length; i++){
const mapping = this.mappings[i];
if(mapping.if === undefined){
throw `${context}.mappings[${i}].if is undefined`
}
keys.push(...mapping.if.usedKeys())
}
keys = Utils.Dedup(keys)
for (let i = 0; i < this.mappings.length; i++){
const mapping = this.mappings[i];
if(mapping.hideInAnswer){
continue
}
const usedKeys = mapping.if.usedKeys();
for (const expectedKey of keys) {
if(usedKeys.indexOf(expectedKey) < 0){
const msg = `${context}.mappings[${i}]: This mapping only defines values for ${usedKeys.join(', ')}, but it should also give a value for ${expectedKey}`
console.warn(msg)
}
}
}
}
if (this.question !== undefined && json.multiAnswer) {
if ((this.mappings?.length ?? 0) === 0) {