1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | 1× 1× | /** * @module montage/core/meta/validation-rule * @requires montage/core/core * @requires core/exception * @requires core/promise * @requires core/logger */ var Montage = require("../core").Montage, PropertyValidationSemantics = require("./validation-semantics").PropertyValidationSemantics, Selector = require("../selector").Selector, deprecate = require("../deprecate"); /** * @class PropertyValidationRule * @extends Montage */ exports.PropertyValidationRule = Montage.specialize( /** @lends PropertyValidationRule# */ { /** * Initialize a newly allocated object descriptor validation rule. * @function * @param {string} rule name * @param {ObjectDescriptor} objectDescriptor * @returns itself */ initWithNameAndObjectDescriptor: { value: function (name, objectDescriptor) { this._name = name; this._owner = objectDescriptor; return this; } }, serializeSelf: { value: function (serializer) { serializer.setProperty("name", this.name); serializer.setProperty("objectDescriptor", this.owner, "reference"); // serializer.setProperty("validationSelector", this._validationSelector, "reference"); serializer.setProperty("messageKey", this.messageKey); serializer.setAllProperties(); } }, deserializeSelf: { value: function (deserializer) { this._name = deserializer.getProperty("name"); this._owner = deserializer.getProperty("objectDescriptor") || deserializer.getProperty("blueprint"); // this._validationSelector = deserializer.getProperty("validationSelector"); this._messageKey = deserializer.getProperty("messageKey"); // FIXME [PJYF Jan 8 2013] There is an API issue in the deserialization // We should be able to write deserializer.getProperties sight!!! var propertyNames = Montage.getSerializablePropertyNames(this); for (var i = 0, l = propertyNames.length; i < l; i++) { var propertyName = propertyNames[i]; this[propertyName] = deserializer.getProperty(propertyName); } } }, _owner: { value: null }, /** * Component description attached to this validation rule. * @type {ObjectDescriptor} */ owner: { get: function () { return this._owner; } }, /** * The identifier is the same as the name and is used to make the serialization of a ObjectDescriptor humane. * @type {string} */ identifier: { get: function () { return [ this.objectDescriptor.identifier, "rule", this.name ].join("_"); } }, _name: { value: "" }, /** * Name of the property being described */ name: { get: function () { return this._name; } }, _validationSelector: { value: null }, /** * Selector to evaluate to check this rule. * @type {Selector} */ validationSelector: { serializable: false, get: function () { if (!this._validationSelector) { this._validationSelector = Selector['false']; } return this._validationSelector; }, set: function (value) { this._validationSelector = value; } }, _messageKey: { value: "" }, /** * Message key to display when the rule fires. */ messageKey: { serializable: false, get: function () { if ((!this._messageKey) || (this._messageKey.length === 0)) { return this._name; } return this._messageKey; }, set: function (value) { this._messageKey = value; } }, _propertyValidationEvaluator: { value: null }, /** * Evaluates the rules based on the ObjectDescriptor and the properties. * @param {Object} object instance to evaluate the rule for * @returns {boolean} true if the rules fires, false otherwise. */ evaluateRule: { value: function (objectInstance) { if (this._propertyValidationEvaluator === null) { var propertyValidationSemantics = new PropertyValidationSemantics().initWithBlueprint(this.objectDescriptor); this._propertyValidationEvaluator = propertyValidationSemantics.compile(this.selector.syntax); } return this._propertyValidationEvaluator(objectInstance); } }, objectDescriptorModuleId: require("../core")._objectDescriptorModuleIdDescriptor, objectDescriptor: require("../core")._objectDescriptorDescriptor, /********************************************************************* * Deprecated methods */ /** * @deprecated * Initialize a newly allocated validation rule. * @deprecated * @function * @param {string} rule name * @param {ObjectDescriptor} objectDescriptor * @returns itself */ initWithNameAndBlueprint: { value: deprecate.deprecateMethod(void 0, function (name, blueprint) { return this.initWithNameAndObjectDescriptor(name, blueprint); }, "initWithNameAndBlueprint", "initWithNameAndObjectDescriptor") }, blueprintModuleId: require("../core")._objectDescriptorModuleIdDescriptor, blueprint: require("../core")._objectDescriptorDescriptor }); |