summaryrefslogtreecommitdiffstats
path: root/toolkit/jetpack/sdk/util/contract.js
diff options
context:
space:
mode:
authorMatt A. Tobin <email@mattatobin.com>2018-02-09 06:46:43 -0500
committerMatt A. Tobin <email@mattatobin.com>2018-02-09 06:46:43 -0500
commitac46df8daea09899ce30dc8fd70986e258c746bf (patch)
tree2750d3125fc253fd5b0671e4bd268eff1fd97296 /toolkit/jetpack/sdk/util/contract.js
parent8cecf8d5208f3945b35f879bba3015bb1a11bec6 (diff)
downloadUXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.gz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.lz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.xz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.zip
Move Add-on SDK source to toolkit/jetpack
Diffstat (limited to 'toolkit/jetpack/sdk/util/contract.js')
-rw-r--r--toolkit/jetpack/sdk/util/contract.js55
1 files changed, 55 insertions, 0 deletions
diff --git a/toolkit/jetpack/sdk/util/contract.js b/toolkit/jetpack/sdk/util/contract.js
new file mode 100644
index 000000000..c689ea601
--- /dev/null
+++ b/toolkit/jetpack/sdk/util/contract.js
@@ -0,0 +1,55 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+module.metadata = {
+ "stability": "unstable"
+};
+
+const { validateOptions: valid } = require("../deprecated/api-utils");
+const method = require("method/core");
+
+// Function takes property validation rules and returns function that given
+// an `options` object will return validated / normalized options back. If
+// option(s) are invalid validator will throw exception described by rules.
+// Returned will also have contain `rules` property with a given validation
+// rules and `properties` function that can be used to generate validated
+// property getter and setters can be mixed into prototype. For more details
+// see `properties` function below.
+function contract(rules) {
+ const validator = (instance, options) => {
+ return valid(options || instance || {}, rules);
+ };
+ validator.rules = rules
+ validator.properties = function(modelFor) {
+ return properties(modelFor, rules);
+ }
+ return validator;
+}
+exports.contract = contract
+
+// Function takes `modelFor` instance state model accessor functions and
+// a property validation rules and generates object with getters and setters
+// that can be mixed into prototype. Property accessors update model for the
+// given instance. If you wish to react to property updates you can always
+// override setters to put specific logic.
+function properties(modelFor, rules) {
+ let descriptor = Object.keys(rules).reduce(function(descriptor, name) {
+ descriptor[name] = {
+ get: function() { return modelFor(this)[name] },
+ set: function(value) {
+ let change = {};
+ change[name] = value;
+ modelFor(this)[name] = valid(change, rules)[name];
+ }
+ }
+ return descriptor
+ }, {});
+ return Object.create(Object.prototype, descriptor);
+}
+exports.properties = properties;
+
+const validate = method("contract/validate");
+exports.validate = validate;