@ganbarodigital/ts-lib-value-objects
Version:
Helps you create value objects and refined types for safer software
48 lines • 1.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RefinedType = void 0;
const types_1 = require("../types");
/**
* RefinedType is a base class for defining a subset of any given type.
* The subset is defined by a contract / specification, and enforced by
* a DataGuarantee.
*
* If you are refining a `string` or a `number`, use `RefinedString` or
* `RefinedNumber` instead. They contain additional methods to help
* JavaScript auto-resolve to the wrapped primitive in some circumstances.
*
* `T` is the type to be wrapped.
*/
class RefinedType extends types_1.ValueObject {
/**
* define your own public constructor (or a static `from()` method if
* you prefer that style, or want to support overloading)
*
* @param input
* this is the value that will be stored, if it passes the
* DataGuarantee
* @param mustBe
* this is the function that enforces the contract / specification
* of this refined type
*
* child classes normally decide what this will be. You don't
* normally allow the end-caller to pass this in.
* @param onError
* the error handler that gets called if the DataGuarantee
* decides that the input doesn't meet the contract /
* specification
*
* this is normally supplied by the end-caller.
*
* child classes can make this optional, and provide a default
* error handler
*/
constructor(input, mustBe, onError) {
// enforce the contract
mustBe(input, onError);
// we're good to go
super(input);
}
}
exports.RefinedType = RefinedType;
//# sourceMappingURL=RefinedType.js.map