UNPKG

value-object-lib

Version:

TypeScript library for creating and validating reusable, robust, and secure Value Objects—strings, numbers, emails, UUIDs, dates, enums, phone numbers, and more. Enables immutable domain logic and centralized validation for DDD-aligned applications.

323 lines (306 loc) 8.83 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { BooleanValueObject: () => BooleanValueObject, DateValueObject: () => DateValueObject, EmailValueObject: () => EmailValueObject, EnumValueObject: () => EnumValueObject, NonEmptyStringValueObject: () => NonEmptyStringValueObject, NonNegativeNumberValueObject: () => NonNegativeNumberValueObject, NumberValueObject: () => NumberValueObject, PhoneNumberValueObject: () => PhoneNumberValueObject, PositiveNumberValueObject: () => PositiveNumberValueObject, StringValueObject: () => StringValueObject, UUIDValueObject: () => UUIDValueObject }); module.exports = __toCommonJS(index_exports); // src/response/response.ts var ResponseDto = class _ResponseDto { constructor(success, message, data, code) { this.success = success; this.message = message; this.data = data; this.code = code; } get error() { return `${this.code}: ${this.message}`; } static success(message = "Request successful", data) { return new _ResponseDto(true, message, data); } static error(message, code, data) { return new _ResponseDto(false, message, data, code); } }; // src/errors/domain.error.ts var DomainError = class extends Error { constructor(message) { super(message); Object.setPrototypeOf(this, new.target.prototype); } toHttpException() { const error = ResponseDto.error(this.message); return new Error(error.error); } }; // src/errors/value-object.error.ts var ValueObjectValidationError = class extends DomainError { code = "E-WORKER"; constructor(field, value, message) { super( `Validation failed for field "${field}" with value "${value}": ${message}` ); } }; // src/value-objects/value-object.ts var ValueObject = class { props; constructor(props) { this.props = Object.freeze(props); } toJSON() { return this.props; } equals(vo) { if (vo === void 0) return false; return JSON.stringify(this.props) === JSON.stringify(vo.props); } }; // src/value-objects/boolean.value-object.ts var BooleanValueObject = class _BooleanValueObject extends ValueObject { constructor(field, value) { _BooleanValueObject.validate(field, value); super({ value }); } get value() { return this.props.value; } toString() { return String(this.value); } static validate(field, value) { if (typeof value !== "boolean") { throw new ValueObjectValidationError( field, value, "Value must be a boolean." ); } } }; // src/value-objects/date.value-object.ts var DateValueObject = class _DateValueObject extends ValueObject { constructor(field, value) { _DateValueObject.validate(field, value); super({ value }); } static validate(field, value) { if (!(value instanceof Date) || isNaN(value.getTime())) { throw new ValueObjectValidationError(field, value, "Invalid date."); } } get value() { return this.props.value; } toString() { return String(this.value); } }; // src/value-objects/string.value-object.ts var StringValueObject = class _StringValueObject extends ValueObject { constructor(field, value) { _StringValueObject.validate(field, value); super({ value }); } static validate(field, value) { if (typeof value !== "string") { throw new ValueObjectValidationError( field, value, "Value must be a string." ); } } get value() { return this.props.value; } toString() { return this.value; } }; // src/value-objects/email.value-object.ts var EmailValueObject = class _EmailValueObject extends StringValueObject { constructor(field, value) { super(field, value); _EmailValueObject.validate(field, value); } static validate(field, value) { StringValueObject.validate(field, value); const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(value)) { throw new ValueObjectValidationError( field, value, "Value must be a valid email address." ); } } }; // src/value-objects/enum.value-object.ts var EnumValueObject = class _EnumValueObject extends ValueObject { constructor(field, value, enumType) { _EnumValueObject.validate(field, value, enumType); super({ value }); } static validate(field, value, enumType) { const enumValues = Object.values(enumType); if (!enumValues.includes(value)) { throw new ValueObjectValidationError( field, value, `Value must be one of the allowed enum values: ${enumValues.join( ", " )}.` ); } } get value() { return this.props.value; } toString() { return String(this.value); } }; // src/value-objects/non-empty-string.value-object.ts var NonEmptyStringValueObject = class _NonEmptyStringValueObject extends StringValueObject { constructor(field, value) { super(field, value); _NonEmptyStringValueObject.validate(field, value); } static validate(field, value) { if (value.trim() === "") { throw new ValueObjectValidationError( field, value, "String cannot be empty." ); } } }; // src/value-objects/number.value-object.ts var NumberValueObject = class _NumberValueObject extends ValueObject { constructor(field, value) { _NumberValueObject.validate(field, value); super({ value }); } static validate(field, value) { if (typeof value !== "number" || !Number.isFinite(value)) { throw new ValueObjectValidationError(field, value, "Value must be a valid number."); } } get value() { return this.props.value; } toString() { return String(this.value); } }; // src/value-objects/non-negative-number.value-object.ts var NonNegativeNumberValueObject = class _NonNegativeNumberValueObject extends NumberValueObject { constructor(field, value) { super(field, value); _NonNegativeNumberValueObject.validate(field, value); } static validate(field, value) { if (value < 0) { throw new ValueObjectValidationError( field, value, "Number must be non-negative." ); } } }; // src/value-objects/phone-number.value-object.ts var PhoneNumberValueObject = class _PhoneNumberValueObject extends StringValueObject { constructor(field, value) { super(field, value); _PhoneNumberValueObject.validate(field, value); } static validate(field, value) { const phoneRegex = /^\+\d{1,3}\d{9,12}$/; if (!phoneRegex.test(value)) { throw new ValueObjectValidationError( field, value, "Value must be a valid phone number with country code and proper length." ); } } }; // src/value-objects/positive-number.value-object.ts var PositiveNumberValueObject = class _PositiveNumberValueObject extends NumberValueObject { constructor(field, value) { super(field, value); _PositiveNumberValueObject.validate(field, value); } static validate(field, value) { if (value <= 0) { throw new ValueObjectValidationError( field, value, "Number must be positive." ); } } }; // src/value-objects/uuid.value-object.ts var UUIDValueObject = class _UUIDValueObject extends StringValueObject { constructor(field, value) { super(field, value); _UUIDValueObject.validate(field, value); } static validate(field, value) { if (!value || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test( value )) { throw new ValueObjectValidationError( field, value, "Invalid UUID format." ); } } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { BooleanValueObject, DateValueObject, EmailValueObject, EnumValueObject, NonEmptyStringValueObject, NonNegativeNumberValueObject, NumberValueObject, PhoneNumberValueObject, PositiveNumberValueObject, StringValueObject, UUIDValueObject });