simplify Checkable.Class annotation and allow extra fields in /keys response

This commit is contained in:
Florian Dold 2017-05-27 15:05:41 +02:00
parent f9e1ad0624
commit 41f152b80a
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
3 changed files with 34 additions and 62 deletions

View File

@ -206,56 +206,28 @@ export namespace Checkable {
* This annotation adds the implementation of the `checked` * This annotation adds the implementation of the `checked`
* static method. * static method.
*/ */
export function Class(target: any) { export function Class(opts: {extra?: boolean, validate?: boolean} = {}) {
target.checked = (v: any) => { return (target: any) => {
return checkValue(v, {
propertyKey: "(root)",
type: target,
checker: checkValue
}, ["(root)"]);
};
return target;
}
/**
* A checker for a class (see [[Class]) that allows
* extra properties to exist on the JSON object being validated.
*/
export function ClassWithExtra(target: any) {
target.checked = (v: any) => {
return checkValue(v, {
propertyKey: "(root)",
type: target,
extraAllowed: true,
checker: checkValue
}, ["(root)"]);
};
return target;
}
/**
* A validator for a class that can have an additional validate
* method. The validate method is a member method of type `() => void`
* that throws an exception on invalidation errors.
*/
export function ClassWithValidator(target: any) {
target.checked = (v: any) => { target.checked = (v: any) => {
let cv = checkValue(v, { let cv = checkValue(v, {
propertyKey: "(root)", propertyKey: "(root)",
type: target, type: target,
extraAllowed: !!opts.extra,
checker: checkValue checker: checkValue
}, ["(root)"]); }, ["(root)"]);
if (opts.validate) {
let instance = new target(); let instance = new target();
if (typeof instance.validate !== "function") { if (typeof instance.validate !== "function") {
throw Error("invalid Checkable annotion: validate method required"); throw Error("invalid Checkable annotion: validate method required");
} }
// May throw exception // May throw exception
instance.validate.call(cv); instance.validate.call(cv);
}
return cv; return cv;
}; };
return target; return target;
} }
}
/** /**

View File

@ -28,7 +28,7 @@
*/ */
import { Checkable } from "./checkable"; import { Checkable } from "./checkable";
@Checkable.Class @Checkable.Class()
export class AmountJson { export class AmountJson {
@Checkable.Number @Checkable.Number
value: number; value: number;
@ -106,7 +106,7 @@ export interface CurrencyRecord {
} }
@Checkable.Class @Checkable.Class()
export class CreateReserveResponse { export class CreateReserveResponse {
/** /**
* Exchange URL where the bank should create the reserve. * Exchange URL where the bank should create the reserve.
@ -187,7 +187,7 @@ export class DenominationRecord {
/** /**
* Denomination as found in the /keys response from the exchange. * Denomination as found in the /keys response from the exchange.
*/ */
@Checkable.Class @Checkable.Class()
export class Denomination { export class Denomination {
@Checkable.Value(AmountJson) @Checkable.Value(AmountJson)
value: AmountJson; value: AmountJson;
@ -304,7 +304,7 @@ export interface PaybackRequest {
coin_sig: string; coin_sig: string;
} }
@Checkable.Class @Checkable.Class()
export class PaybackConfirmation { export class PaybackConfirmation {
/** /**
* public key of the reserve that will receive the payback. * public key of the reserve that will receive the payback.
@ -477,7 +477,7 @@ export interface CoinRecord {
} }
@Checkable.Class @Checkable.Class()
export class ExchangeHandle { export class ExchangeHandle {
@Checkable.String @Checkable.String
master_pub: string; master_pub: string;
@ -524,7 +524,7 @@ interface Merchant {
instance?: string; instance?: string;
} }
@Checkable.ClassWithValidator @Checkable.Class({validate: true})
export class Contract { export class Contract {
validate() { validate() {

View File

@ -89,7 +89,7 @@ export interface CoinWithDenom {
* Element of the payback list that the * Element of the payback list that the
* exchange gives us in /keys. * exchange gives us in /keys.
*/ */
@Checkable.Class @Checkable.Class()
export class Payback { export class Payback {
@Checkable.String @Checkable.String
h_denom_pub: string; h_denom_pub: string;
@ -99,7 +99,7 @@ export class Payback {
/** /**
* Structure that the exchange gives us in /keys. * Structure that the exchange gives us in /keys.
*/ */
@Checkable.Class @Checkable.Class({extra: true})
export class KeysJson { export class KeysJson {
@Checkable.List(Checkable.Value(Denomination)) @Checkable.List(Checkable.Value(Denomination))
denoms: Denomination[]; denoms: Denomination[];
@ -129,7 +129,7 @@ export class KeysJson {
} }
@Checkable.Class @Checkable.Class()
class WireFeesJson { class WireFeesJson {
@Checkable.Value(AmountJson) @Checkable.Value(AmountJson)
wire_fee: AmountJson; wire_fee: AmountJson;
@ -150,7 +150,7 @@ class WireFeesJson {
} }
@Checkable.ClassWithExtra @Checkable.Class({extra: true})
class WireDetailJson { class WireDetailJson {
@Checkable.String @Checkable.String
type: string; type: string;
@ -162,7 +162,7 @@ class WireDetailJson {
} }
@Checkable.Class @Checkable.Class()
export class CreateReserveRequest { export class CreateReserveRequest {
/** /**
* The initial amount for the reserve. * The initial amount for the reserve.
@ -180,7 +180,7 @@ export class CreateReserveRequest {
} }
@Checkable.Class @Checkable.Class()
export class ConfirmReserveRequest { export class ConfirmReserveRequest {
/** /**
* Public key of then reserve that should be marked * Public key of then reserve that should be marked
@ -193,7 +193,7 @@ export class ConfirmReserveRequest {
} }
@Checkable.Class @Checkable.Class()
export class OfferRecord { export class OfferRecord {
@Checkable.Value(Contract) @Checkable.Value(Contract)
contract: Contract; contract: Contract;