@decaf-ts/decorator-validation
Version:
simple decorator based validation engine
64 lines • 2.14 kB
JavaScript
import { apply, metadata } from "@decaf-ts/reflection";
import { ModelKeys } from "./constants.js";
import { Decoration } from "./Decoration.js";
/**
* @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
*/
export function prop(key = ModelKeys.ATTRIBUTE) {
return 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
*/
export function propMetadata(key, value) {
return apply(prop(), metadata(key, value));
}
//# sourceMappingURL=decorators.js.map