@rxap/xml-parser
Version:
Provides a set of decorators and services for parsing and serializing XML documents into TypeScript classes. It simplifies the process of mapping XML elements and attributes to class properties, handling data validation, and serializing objects back into
44 lines • 2.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RequiredElementMixin = void 0;
exports.ElementRequired = ElementRequired;
const reflect_metadata_1 = require("@rxap/reflect-metadata");
const metadata_keys_1 = require("../metadata-keys");
const utilities_1 = require("../utilities");
/**
* Decorator function that marks a property as required within an element parser.
* It ensures that the property is defined and throws an error if the property is already associated with another element parser.
*
* @param {RequiredElementOptions} options - Configuration options for the required element, defaulting to `{ required: true }`.
* This allows customization of the requirement behavior, although currently only supports the `required` flag.
*
* @returns {Function} A decorator function that takes a target object and a property key, and applies the required configuration.
*
* @throws {Error} Throws an error if the property is already associated with an element parser, indicating incorrect decorator order.
*
* @example
* ```typescript
* class Example {
* @ElementRequired()
* @ElementTextContent()
* public title: string;
* }
* ```
* In this example, the `title` property of the `Example` class is marked as required and associated with text content from an element.
* The `@ElementRequired` decorator must be used after any `@Element...` decorators to ensure proper metadata registration.
*/
function ElementRequired(options = { required: true }) {
return function (target, propertyKey) {
if ((0, utilities_1.FindElementParserInstanceForPropertyKey)(target.constructor, propertyKey)) {
throw new Error('The @ElementRequired decorator must be use after the @Element(Attribute|Child|ChildTextContent|TextContent) decorator');
}
(0, reflect_metadata_1.mergeWithMetadata)(metadata_keys_1.ElementParserMetaData.OPTIONS, options, target, propertyKey);
};
}
class RequiredElementMixin {
get required() {
return !!this.options.required;
}
}
exports.RequiredElementMixin = RequiredElementMixin;
//# sourceMappingURL=required-element.mixin.js.map