aboutsummaryrefslogtreecommitdiff
path: root/extension/background/checkable.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2016-01-05 18:37:21 +0100
committerFlorian Dold <florian.dold@gmail.com>2016-01-05 18:37:21 +0100
commit9dc0d89118dba960e672e024411f3ef0af94f5c6 (patch)
tree4678d0ed2f89e1e5bd1e2f70b4b2e6a2b1c52a7a /extension/background/checkable.ts
parent91fc8d56f02628a257b6681a379ef6f28824b24e (diff)
skeleton for decorator-based schema validation
Diffstat (limited to 'extension/background/checkable.ts')
-rw-r--r--extension/background/checkable.ts87
1 files changed, 87 insertions, 0 deletions
diff --git a/extension/background/checkable.ts b/extension/background/checkable.ts
new file mode 100644
index 000000000..63fbd3646
--- /dev/null
+++ b/extension/background/checkable.ts
@@ -0,0 +1,87 @@
+/*
+ This file is part of TALER
+ (C) 2016 GNUnet e.V.
+
+ TALER is free software; you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation; either version 3, or (at your option) any later version.
+
+ TALER is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along with
+ TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
+ */
+
+"use strict";
+
+/**
+ * Decorators for type-checking JSON into
+ * an object.
+ * @module Checkable
+ * @author Florian Dold
+ */
+
+
+namespace Checkable {
+ let chkSym = Symbol("checkable");
+
+ function checkNumber(target, prop) {
+ return true;
+ }
+
+ function checkString(target, prop) {
+ return true;
+ }
+
+ export function Class(target) {
+ target.checked = (v) => {
+ let props = target.prototype[chkSym].props;
+ console.log("hello, world");
+ let remainingPropNames = new Set(Object.getOwnPropertyNames(v));
+
+ for (let prop of props) {
+ remainingPropNames.delete(prop);
+ console.log("prop", prop);
+ }
+
+ if (remainingPropNames.size != 0) {
+ throw Error("superfluous properties " + JSON.stringify(remainingPropNames.values()));
+ }
+ };
+ return target;
+ }
+
+ export function Value(type) {
+ function deco(target) {
+ }
+
+ return deco;
+ }
+
+ export function List(type) {
+ function deco(target) {
+ }
+
+ return deco;
+ }
+
+ export function Number(target: Object, propertyKey: string | symbol): void {
+ let chk = target[chkSym];
+ if (!chk) {
+ chk = {props: []};
+ target[chkSym] = chk;
+ }
+ chk.props.push({propertyKey: propertyKey, checker: checkNumber});
+ }
+
+ export function String(target: Object, propertyKey: string | symbol): void {
+ let chk = target[chkSym];
+ if (!chk) {
+ chk = {props: []};
+ target[chkSym] = chk;
+ }
+ chk.props.push({propertyKey: propertyKey, checker: checkString});
+ }
+}