add validators to checkable classes

This commit is contained in:
Florian Dold 2016-11-14 00:57:29 +01:00
parent 48bac7d4a9
commit dca6d303c1
2 changed files with 28 additions and 2 deletions

View File

@ -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) { export function Value(type: any) {
if (!type) { if (!type) {
throw Error("Type does not exist yet (wrong order of definitions?)"); throw Error("Type does not exist yet (wrong order of definitions?)");

View File

@ -360,8 +360,15 @@ interface Merchant {
instance?: string; instance?: string;
} }
@Checkable.Class @Checkable.ClassWithValidator
export class Contract { export class Contract {
validate() {
if (this.exchanges.length == 0) {
throw Error("no exchanges in contract");
}
}
@Checkable.String @Checkable.String
H_wire: string; H_wire: string;