@darlean/valueobjects
Version:
Library for DDD-like value objects that can be validated, serialized and represented in other programming languages as native objects with native naming.
264 lines (263 loc) • 9.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.canonicalvalidation = exports.canonicalvalue = exports.CanonicalValue = exports.binaryvalidation = exports.binaryvalue = exports.BinaryValue = exports.momentvalidation = exports.momentvalue = exports.MomentValue = exports.DurationValue = exports.boolvalidation = exports.boolvalue = exports.BoolValue = exports.nonevalidation = exports.nonevalue = exports.NoneValue = exports.floatvalidation = exports.floatvalue = exports.FloatValue = exports.intvalidation = exports.intvalue = exports.IntValue = exports.stringvalidation = exports.stringvalue = exports.StringValue = exports.PrimitiveValue = void 0;
const canonical_1 = require("@darlean/canonical");
const valueobject_1 = require("./valueobject");
require("reflect-metadata");
const base_1 = require("./base");
class PrimitiveValue extends base_1.Value {
static from(value) {
const options = {
value
};
return Reflect.construct(this, [options]);
}
static fromCanonical(value, options) {
const valueoptions = {
canonical: value,
cacheCanonical: options?.cacheCanonical
};
return Reflect.construct(this, [valueoptions]);
}
static required() {
return (0, base_1.required)(this);
}
static optional() {
return (0, base_1.optional)(this);
}
constructor(options) {
super(options);
let v;
if (options.canonical) {
const canonical = (0, canonical_1.toCanonical)(options.canonical);
const logicalTypes = (0, base_1.checkLogicalTypes)(Object.getPrototypeOf(this));
const canonicalLogicalTypes = canonical.logicalTypes;
if (!canonical.is(logicalTypes)) {
throw new valueobject_1.ValidationError(`Incoming value of logical types '${canonicalLogicalTypes.join('.')}' is not compatible with '${logicalTypes.join('.')}'`);
}
if ((0, base_1.shouldCacheCanonical)(canonical, logicalTypes, options?.cacheCanonical)) {
this._canonical = canonical;
}
try {
v = this._fromCanonical(canonical);
}
catch (e) {
throw new valueobject_1.ValidationError(e instanceof Error ? e.message : e);
}
}
else {
v = options.value;
}
const msgs = [];
const validated = this._validate(v, (msg) => msgs.push(msg));
if (msgs.length > 0) {
throw new valueobject_1.ValidationError(msgs.join('; '));
}
this._value = (validated ?? v);
}
_peekCanonicalRepresentation() {
if (this._canonical) {
return this._canonical;
}
const canonical = this._toCanonical(this.value, this._logicalTypes);
this._canonical = canonical;
return canonical;
}
get value() {
return this._value;
}
get _logicalTypes() {
return (Reflect.getOwnMetadata(base_1.LOGICAL_TYPES, Object.getPrototypeOf(this)) ?? []);
}
equals(other) {
if (!(0, canonical_1.isCanonicalLike)(other)) {
return false;
}
return this._peekCanonicalRepresentation().equals(other);
}
_validate(v, fail) {
const validators = Reflect.getOwnMetadata(base_1.VALIDATORS, Object.getPrototypeOf(this));
if (validators) {
let failed = false;
for (const validator of validators) {
validator(v, (msg) => {
fail(msg);
failed = true;
});
if (failed) {
break;
}
}
}
}
}
exports.PrimitiveValue = PrimitiveValue;
class StringValue extends PrimitiveValue {
_fromCanonical(canonical) {
return canonical.stringValue;
}
_toCanonical(value, logicalTypes) {
return canonical_1.StringCanonical.from(value, logicalTypes);
}
_validate(v, fail) {
if (typeof v !== 'string') {
return fail(`Must be a string; not ${typeof v}`);
}
super._validate(v, fail);
}
}
exports.StringValue = StringValue;
exports.stringvalue = base_1.valueobject;
exports.stringvalidation = (base_1.validation);
(0, exports.stringvalue)('')(StringValue);
class IntValue extends PrimitiveValue {
_fromCanonical(canonical) {
return canonical.intValue;
}
_toCanonical(value, logicalTypes) {
return canonical_1.IntCanonical.from(value, logicalTypes);
}
_validate(v, fail) {
if (typeof v !== 'number') {
return fail(`Must be a number; not ${typeof v}`);
}
if (!Number.isInteger(v)) {
return fail(`Must be an integer number; not ${v}`);
}
super._validate(v, fail);
}
}
exports.IntValue = IntValue;
exports.intvalue = base_1.valueobject;
exports.intvalidation = (base_1.validation);
(0, exports.intvalue)('')(IntValue);
class FloatValue extends PrimitiveValue {
_fromCanonical(canonical) {
return canonical.floatValue;
}
_toCanonical(value, logicalTypes) {
return canonical_1.FloatCanonical.from(value, logicalTypes);
}
_validate(v, fail) {
if (typeof v !== 'number') {
return fail(`Must be a number; not ${typeof v}`);
}
if (!Number.isFinite(v)) {
return fail(`Must be a finite number; not ${v}`);
}
if (Number.isNaN(v)) {
return fail(`Must be a finite number; not ${v}`);
}
super._validate(v, fail);
}
}
exports.FloatValue = FloatValue;
exports.floatvalue = base_1.valueobject;
exports.floatvalidation = (base_1.validation);
(0, exports.floatvalue)('')(FloatValue);
class NoneValue extends PrimitiveValue {
_fromCanonical(canonical) {
return canonical.noneValue;
}
_toCanonical(_value, logicalTypes) {
return canonical_1.NoneCanonical.from(logicalTypes);
}
_validate(v, fail) {
if (typeof v !== 'undefined') {
return fail(`Must be undefined; not ${typeof v}`);
}
super._validate(v, fail);
}
}
exports.NoneValue = NoneValue;
exports.nonevalue = base_1.valueobject;
exports.nonevalidation = (base_1.validation);
(0, exports.nonevalue)('')(NoneValue);
class BoolValue extends PrimitiveValue {
_fromCanonical(canonical) {
return canonical.boolValue;
}
_toCanonical(value, logicalTypes) {
return canonical_1.BoolCanonical.from(value, logicalTypes);
}
_validate(v, fail) {
if (typeof v !== 'boolean') {
return fail(`Must be a boolean; not ${typeof v}`);
}
super._validate(v, fail);
}
}
exports.BoolValue = BoolValue;
exports.boolvalue = base_1.valueobject;
exports.boolvalidation = (base_1.validation);
(0, exports.boolvalue)('')(BoolValue);
class DurationValue extends FloatValue {
}
exports.DurationValue = DurationValue;
(0, exports.floatvalue)('')(DurationValue);
class MomentValue extends PrimitiveValue {
_fromCanonical(canonical) {
return canonical.momentValue;
}
_toCanonical(value, logicalTypes) {
return canonical_1.MomentCanonical.from(value, logicalTypes);
}
_validate(v, fail) {
if (!(v instanceof Date)) {
return fail(`Must be a Date; not ${typeof v}`);
}
super._validate(v, fail);
}
get ms() {
return this._value.valueOf();
}
static fromMilliseconds(ms) {
return (0, base_1.constructValue)(this, { value: new Date(ms) });
}
addDuration(duration) {
return (0, base_1.constructValue)(Object.getPrototypeOf(this), { value: new Date(this._value.valueOf() + duration.value) });
}
subtractDuration(duration) {
return (0, base_1.constructValue)(Object.getPrototypeOf(this), { value: new Date(this._value.valueOf() - duration.value) });
}
}
exports.MomentValue = MomentValue;
exports.momentvalue = base_1.valueobject;
exports.momentvalidation = (base_1.validation);
(0, exports.momentvalue)('')(MomentValue);
class BinaryValue extends PrimitiveValue {
_fromCanonical(canonical) {
return canonical.binaryValue;
}
_toCanonical(value, logicalTypes) {
return canonical_1.BinaryCanonical.from(value, logicalTypes);
}
_validate(v, fail) {
if (!Buffer.isBuffer(v)) {
return fail(`Must be a Buffer; not ${typeof v}`);
}
super._validate(v, fail);
}
}
exports.BinaryValue = BinaryValue;
exports.binaryvalue = base_1.valueobject;
exports.binaryvalidation = (base_1.validation);
(0, exports.binaryvalue)('')(BinaryValue);
class CanonicalValue extends PrimitiveValue {
_fromCanonical(canonical) {
return canonical;
}
_toCanonical(value, _logicalTypes) {
return value;
}
_validate(v, fail) {
if (!(0, canonical_1.isCanonical)(v)) {
return fail(`Must be an ICanonical; not ${typeof v}`);
}
super._validate(v, fail);
}
}
exports.CanonicalValue = CanonicalValue;
exports.canonicalvalue = base_1.valueobject;
exports.canonicalvalidation = (base_1.validation);
(0, exports.canonicalvalue)('')(CanonicalValue);