UNPKG

@vulcan-sql/core

Version:
176 lines 5.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConstraintDiscriminator = exports.TypeConstraint = exports.EnumConstraint = exports.RegexConstraint = exports.MaxLengthConstraint = exports.MinLengthConstraint = exports.MaxValueConstraint = exports.MinValueConstraint = exports.RequiredConstraint = exports.Constraint = void 0; const lodash_1 = require("lodash"); const utils_1 = require("../utils"); class Constraint { static Required() { return new RequiredConstraint(); } static MinValue(minValue, exclusive) { return new MinValueConstraint(minValue, exclusive); } static MaxValue(maxValue, exclusive) { return new MaxValueConstraint(maxValue, exclusive); } static MinLength(minLength) { return new MinLengthConstraint(minLength); } static MaxLength(maxLength) { return new MaxLengthConstraint(maxLength); } static Regex(regex) { return new RegexConstraint(regex); } static Enum(list) { return new EnumConstraint(list); } static Type(type) { return new TypeConstraint(type); } } exports.Constraint = Constraint; class RequiredConstraint extends Constraint { constructor() { super(...arguments); this.__type = 'Required'; } compose() { // No matter what other required constraint is, we always return a required constraint return new RequiredConstraint(); } } exports.RequiredConstraint = RequiredConstraint; class MinValueConstraint extends Constraint { constructor(minValue, exclusive = false) { super(); this.minValue = minValue; this.exclusive = exclusive; this.__type = 'MinValue'; } getMinValue() { return this.minValue; } isExclusive() { return this.exclusive; } compose(constraint) { if (constraint.getMinValue() === this.getMinValue()) { return new MinValueConstraint(this.getMinValue(), constraint.isExclusive() || this.isExclusive()); } if (constraint.getMinValue() > this.getMinValue()) { return constraint; } return this; } } exports.MinValueConstraint = MinValueConstraint; class MaxValueConstraint extends Constraint { constructor(maxValue, exclusive = false) { super(); this.maxValue = maxValue; this.exclusive = exclusive; this.__type = 'MaxValue'; } getMaxValue() { return this.maxValue; } isExclusive() { return this.exclusive; } compose(constraint) { if (constraint.getMaxValue() === this.getMaxValue()) { return new MaxValueConstraint(this.getMaxValue(), constraint.isExclusive() || this.isExclusive()); } if (constraint.getMaxValue() < this.getMaxValue()) { return constraint; } return this; } } exports.MaxValueConstraint = MaxValueConstraint; class MinLengthConstraint extends Constraint { constructor(minLength) { super(); this.minLength = minLength; this.__type = 'MinLength'; } getMinLength() { return this.minLength; } compose(constraint) { return new MinLengthConstraint(Math.max(this.minLength, constraint.getMinLength())); } } exports.MinLengthConstraint = MinLengthConstraint; class MaxLengthConstraint extends Constraint { constructor(maxLength) { super(); this.maxLength = maxLength; this.__type = 'MaxLength'; } getMaxLength() { return this.maxLength; } compose(constraint) { return new MaxLengthConstraint(Math.min(this.maxLength, constraint.getMaxLength())); } } exports.MaxLengthConstraint = MaxLengthConstraint; class RegexConstraint extends Constraint { constructor(regex) { super(); this.regex = regex; this.__type = 'Regex'; } getRegex() { return this.regex; } compose() { throw new utils_1.InternalError(`Cannot use multiple RegexConstraint at the same time.`); } } exports.RegexConstraint = RegexConstraint; class EnumConstraint extends Constraint { constructor(list) { super(); this.list = list; this.__type = 'Enum'; } getList() { return this.list; } compose(constraint) { return new EnumConstraint((0, lodash_1.intersection)(this.getList(), constraint.getList())); } } exports.EnumConstraint = EnumConstraint; class TypeConstraint extends Constraint { constructor(type) { super(); this.type = type; this.__type = 'Type'; } getType() { return this.type; } compose() { throw new utils_1.InternalError(`Cannot use multiple TypeConstraint at the same time.`); } } exports.TypeConstraint = TypeConstraint; // https://github.com/typestack/class-transformer/tree/master#providing-more-than-one-type-option exports.ConstraintDiscriminator = { property: '__type', subTypes: [ { value: RequiredConstraint, name: 'Required' }, { value: MinValueConstraint, name: 'MinValue' }, { value: MaxValueConstraint, name: 'MaxValue' }, { value: MinLengthConstraint, name: 'MinLength' }, { value: MaxLengthConstraint, name: 'MaxLength' }, { value: RegexConstraint, name: 'Regex' }, { value: EnumConstraint, name: 'Enum' }, { value: TypeConstraint, name: 'Type' }, ], }; //# sourceMappingURL=constraints.js.map