aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2016-11-14 00:57:29 +0100
committerFlorian Dold <florian.dold@gmail.com>2016-11-14 00:57:29 +0100
commitdca6d303c1d15d49305f538dd62df7a65cdfcc38 (patch)
treeac820902e91eb32b0b33f9cf5a1850c4079674dc /src
parent48bac7d4a9d2846b0309dab0fb92e1c36f854a92 (diff)
downloadwallet-core-dca6d303c1d15d49305f538dd62df7a65cdfcc38.tar.xz
add validators to checkable classes
Diffstat (limited to 'src')
-rw-r--r--src/checkable.ts21
-rw-r--r--src/types.ts9
2 files changed, 28 insertions, 2 deletions
diff --git a/src/checkable.ts b/src/checkable.ts
index 89d0c7150..b6ef49fac 100644
--- a/src/checkable.ts
+++ b/src/checkable.ts
@@ -156,6 +156,25 @@ export namespace Checkable {
}
+ export function ClassWithValidator(target: any) {
+ target.checked = (v: any) => {
+ let cv = checkValue(v, {
+ propertyKey: "(root)",
+ type: target,
+ checker: checkValue
+ }, ["(root)"]);
+ let instance = new target();
+ if (typeof instance.validate !== "function") {
+ throw Error("invalid Checkable annotion: validate method required");
+ }
+ // May throw exception
+ instance.validate.call(cv);
+ return cv;
+ };
+ return target;
+ }
+
+
export function Value(type: any) {
if (!type) {
throw Error("Type does not exist yet (wrong order of definitions?)");
@@ -259,4 +278,4 @@ export namespace Checkable {
}
return chk;
}
-} \ No newline at end of file
+}
diff --git a/src/types.ts b/src/types.ts
index 39d374069..8f24fda90 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -360,8 +360,15 @@ interface Merchant {
instance?: string;
}
-@Checkable.Class
+@Checkable.ClassWithValidator
export class Contract {
+
+ validate() {
+ if (this.exchanges.length == 0) {
+ throw Error("no exchanges in contract");
+ }
+ }
+
@Checkable.String
H_wire: string;