michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: module.metadata = { michael@0: "stability": "unstable" michael@0: }; michael@0: michael@0: const { validateOptions: valid } = require("../deprecated/api-utils"); michael@0: michael@0: // Function takes property validation rules and returns function that given michael@0: // an `options` object will return validated / normalized options back. If michael@0: // option(s) are invalid validator will throw exception described by rules. michael@0: // Returned will also have contain `rules` property with a given validation michael@0: // rules and `properties` function that can be used to generate validated michael@0: // property getter and setters can be mixed into prototype. For more details michael@0: // see `properties` function below. michael@0: function contract(rules) { michael@0: function validator(options) { michael@0: return valid(options || {}, rules); michael@0: } michael@0: validator.rules = rules michael@0: validator.properties = function(modelFor) { michael@0: return properties(modelFor, rules); michael@0: } michael@0: return validator; michael@0: } michael@0: exports.contract = contract michael@0: michael@0: // Function takes `modelFor` instance state model accessor functions and michael@0: // a property validation rules and generates object with getters and setters michael@0: // that can be mixed into prototype. Property accessors update model for the michael@0: // given instance. If you wish to react to property updates you can always michael@0: // override setters to put specific logic. michael@0: function properties(modelFor, rules) { michael@0: let descriptor = Object.keys(rules).reduce(function(descriptor, name) { michael@0: descriptor[name] = { michael@0: get: function() { return modelFor(this)[name] }, michael@0: set: function(value) { michael@0: let change = {}; michael@0: change[name] = value; michael@0: modelFor(this)[name] = valid(change, rules)[name]; michael@0: } michael@0: } michael@0: return descriptor michael@0: }, {}); michael@0: return Object.create(Object.prototype, descriptor); michael@0: } michael@0: exports.properties = properties