domain-objects
Version:
A simple, convenient way to represent domain objects, leverage domain knowledge, and add runtime validation in your code base.
32 lines • 1.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DomainObject = void 0;
const hydrateNestedDomainObjects_1 = require("./hydrate/hydrateNestedDomainObjects");
const validate_1 = require("./validate/validate");
/**
* Domain Object
*
* Responsibilities:
* - optionally validate the properties that are passed into the constructor against the schema at runtime, if schema is supplied
* - assign all properties that are passed into constructor to self, after optional runtime validation
*/
class DomainObject {
constructor(props, options) {
var _a, _b;
// 1. validate with the schema if provided
const { schema } = this.constructor; // `this.constructor` does not get typed to DomainObject automatically by ts; https://stackoverflow.com/questions/33387318/access-to-static-properties-via-this-constructor-in-typescript
if (schema && !(((_a = options === null || options === void 0 ? void 0 : options.skip) === null || _a === void 0 ? void 0 : _a.schema) === true))
(0, validate_1.validate)({ props, schema, domainObjectName: this.constructor.name });
// 2. hydrate any nested props present; just overwrite the orig props for each "nested" key
const nested = ((_b = this.constructor.nested) !== null && _b !== void 0 ? _b : {});
const hydratedProps = (0, hydrateNestedDomainObjects_1.hydrateNestedDomainObjects)({
domainObjectName: this.constructor.name,
props,
nested,
});
// 3. assign all properties to self if passed validation
Object.assign(this, hydratedProps);
}
}
exports.DomainObject = DomainObject;
//# sourceMappingURL=DomainObject.js.map