@decaf-ts/decorator-validation
Version:
simple decorator based validation engine
68 lines • 2.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.prop = prop;
exports.propMetadata = propMetadata;
const reflection_1 = require("@decaf-ts/reflection");
const constants_1 = require("./constants.cjs");
const Decoration_1 = require("./Decoration.cjs");
/**
* @description Property decorator factory for model attributes
* @summary Creates a decorator that marks class properties as model attributes
*
* @param {string} [key=ModelKeys.ATTRIBUTE] - The metadata key under which to store the property name
* @return {function(object, any?): void} - Decorator function that registers the property
* @function prop
* @category Property Decorators
*
* @mermaid
* sequenceDiagram
* participant D as Decorator
* participant M as Model
*
* D->>M: Check if key exists
* alt key exists
* M-->>D: Return existing props array
* else key doesn't exist
* D->>M: Create new props array
* end
* D->>M: Check if property exists
* alt property not in array
* D->>M: Add property to array
* end
*/
function prop(key = constants_1.ModelKeys.ATTRIBUTE) {
return Decoration_1.Decoration.for(key)
.define(function prop(model, propertyKey) {
let props;
if (Object.prototype.hasOwnProperty.call(model, key)) {
props = model[key];
}
else {
Object.defineProperty(model, key, {
enumerable: false,
configurable: false,
writable: false,
value: [],
});
props = model[key];
}
if (!props.includes(propertyKey))
props.push(propertyKey);
})
.apply();
}
/**
* @description Combined property decorator factory for metadata and attribute marking
* @summary Creates a decorator that both marks a property as a model attribute and assigns metadata to it
*
* @template V
* @param {string} key - The metadata key
* @param {V} value - The metadata value to associate with the property
* @return {Function} - Combined decorator function
* @function propMetadata
* @category Property Decorators
*/
function propMetadata(key, value) {
return (0, reflection_1.apply)(prop(), (0, reflection_1.metadata)(key, value));
}
//# sourceMappingURL=decorators.js.map