Pad: Add strict validation checks

This commit is contained in:
Richard Hansen 2021-11-27 03:37:34 -05:00
parent 2608a81654
commit 7c870f8a58
5 changed files with 166 additions and 238 deletions

View file

@ -188,6 +188,35 @@ class AttributePool {
}
return this;
}
/**
* Asserts that the data in the pool is consistent. Throws if inconsistent.
*/
check() {
if (!Number.isInteger(this.nextNum)) throw new Error('nextNum property is not an integer');
if (this.nextNum < 0) throw new Error('nextNum property is negative');
for (const prop of ['numToAttrib', 'attribToNum']) {
const obj = this[prop];
if (obj == null) throw new Error(`${prop} property is null`);
if (typeof obj !== 'object') throw new TypeError(`${prop} property is not an object`);
const keys = Object.keys(obj);
if (keys.length !== this.nextNum) {
throw new Error(`${prop} size mismatch (want ${this.nextNum}, got ${keys.length})`);
}
}
for (let i = 0; i < this.nextNum; ++i) {
const attr = this.numToAttrib[`${i}`];
if (!Array.isArray(attr)) throw new TypeError(`attrib ${i} is not an array`);
if (attr.length !== 2) throw new Error(`attrib ${i} is not an array of length 2`);
const [k, v] = attr;
if (k == null) throw new TypeError(`attrib ${i} key is null`);
if (typeof k !== 'string') throw new TypeError(`attrib ${i} key is not a string`);
if (v == null) throw new TypeError(`attrib ${i} value is null`);
if (typeof v !== 'string') throw new TypeError(`attrib ${i} value is not a string`);
const attrStr = String(attr);
if (this.attribToNum[attrStr] !== i) throw new Error(`attribToNum for ${attrStr} !== ${i}`);
}
}
}
module.exports = AttributePool;