From 41f152b80ac10de22f330ec240196db484a8a479 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Sat, 27 May 2017 15:05:41 +0200 Subject: simplify Checkable.Class annotation and allow extra fields in /keys response --- src/checkable.ts | 70 +++++++++++++++++--------------------------------------- 1 file changed, 21 insertions(+), 49 deletions(-) (limited to 'src/checkable.ts') diff --git a/src/checkable.ts b/src/checkable.ts index 6bfaea013..24eebc713 100644 --- a/src/checkable.ts +++ b/src/checkable.ts @@ -206,55 +206,27 @@ export namespace Checkable { * This annotation adds the implementation of the `checked` * static method. */ - export function Class(target: any) { - target.checked = (v: 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) => { - 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 Class(opts: {extra?: boolean, validate?: boolean} = {}) { + return (target: any) => { + target.checked = (v: any) => { + let cv = checkValue(v, { + propertyKey: "(root)", + type: target, + extraAllowed: !!opts.extra, + checker: checkValue + }, ["(root)"]); + if (opts.validate) { + 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; + } } -- cgit v1.2.3