aboutsummaryrefslogtreecommitdiff
path: root/extension/lib/wallet/checkable.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2016-02-17 17:51:25 +0100
committerFlorian Dold <florian.dold@gmail.com>2016-02-17 17:51:25 +0100
commit059de061aba79d9e823cc43497038e1722434b61 (patch)
treeaf9650621e507a4c5f53098ff295b8c0dd3ee745 /extension/lib/wallet/checkable.ts
parent874d083ec371441d2f2b31281652fd8f82cc5489 (diff)
contract schema
Diffstat (limited to 'extension/lib/wallet/checkable.ts')
-rw-r--r--extension/lib/wallet/checkable.ts37
1 files changed, 35 insertions, 2 deletions
diff --git a/extension/lib/wallet/checkable.ts b/extension/lib/wallet/checkable.ts
index 9a31e4e17..27ea9bf74 100644
--- a/extension/lib/wallet/checkable.ts
+++ b/extension/lib/wallet/checkable.ts
@@ -77,6 +77,15 @@ export namespace Checkable {
}
+ function checkOptional(target, prop, path): any {
+ console.assert(prop.propertyKey);
+ prop.elementChecker(target,
+ prop.elementProp,
+ path.concat([prop.propertyKey]));
+ return target;
+ }
+
+
function checkValue(target, prop, path): any {
let type = prop.type;
if (!type) {
@@ -84,7 +93,8 @@ export namespace Checkable {
}
let v = target;
if (!v || typeof v !== "object") {
- throw new SchemaError(`expected object for ${path}, got ${typeof v} instead`);
+ throw new SchemaError(
+ `expected object for ${path.join(".")}, got ${typeof v} instead`);
}
let props = type.prototype[chkSym].props;
let remainingPropNames = new Set(Object.getOwnPropertyNames(v));
@@ -119,7 +129,7 @@ export namespace Checkable {
propertyKey: "(root)",
type: target,
checker: checkValue
- }, []);
+ }, ["(root)"]);
};
return target;
}
@@ -164,6 +174,29 @@ export namespace Checkable {
}
+ export function Optional(type) {
+ let stub = {};
+ type(stub, "(optional-element)");
+ let elementProp = mkChk(stub).props[0];
+ let elementChecker = elementProp.checker;
+ if (!elementChecker) {
+ throw Error("assertion failed");
+ }
+ function deco(target: Object, propertyKey: string | symbol): void {
+ let chk = mkChk(target);
+ chk.props.push({
+ elementChecker,
+ elementProp,
+ propertyKey: propertyKey,
+ checker: checkOptional,
+ optional: true,
+ });
+ }
+
+ return deco;
+ }
+
+
export function Number(target: Object, propertyKey: string | symbol): void {
let chk = mkChk(target);
chk.props.push({propertyKey: propertyKey, checker: checkNumber});