diff --git a/package.json b/package.json index b5e3266..d4db758 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "valid-objects-ts", - "version": "1.0.8", + "version": "1.0.9", "main": ".dist/index.js", "types": ".dist/index.d.ts", "repository": "git@github.com:Travelport-Czech/valid-objects-ts.git", diff --git a/src/errors/ValidObjectLogicError.ts b/src/errors/ValidObjectLogicError.ts new file mode 100644 index 0000000..6aa35e6 --- /dev/null +++ b/src/errors/ValidObjectLogicError.ts @@ -0,0 +1,3 @@ +import { ExtendableError } from 'extendable-error' + +export class ValidObjectLogicError extends ExtendableError {} diff --git a/src/validObjects/ValidNumber.test.ts b/src/validObjects/ValidNumber.test.ts new file mode 100644 index 0000000..9e7bf12 --- /dev/null +++ b/src/validObjects/ValidNumber.test.ts @@ -0,0 +1,23 @@ +import { ValidNumber } from '@/validObjects/ValidNumber' +import { expect } from 'chai' + +describe('ValidNumber', () => { + it('Construct default', () => { + expect(new ValidNumber(0).value).to.equal(0) + expect(new ValidNumber(1).value).to.equal(1) + expect(new ValidNumber(-1).value).to.equal(-1) + expect(new ValidNumber(-1000000000).value).to.equal(-1000000000) + expect(new ValidNumber(1000000000).value).to.equal(1000000000) + }) + + it('Construct with range', () => { + expect(new ValidNumber(-2, -2, 2).value).to.equal(-2) + expect(new ValidNumber(0, -2, 2).value).to.equal(0) + expect(new ValidNumber(1, -2, 2).value).to.equal(1) + expect(new ValidNumber(2, -2, 2).value).to.equal(2) + + expect(() => new ValidNumber(-3, -2, 2)).to.throw("Invalid number 'Number (-3) can not be smaller than -2'.") + expect(() => new ValidNumber(0, 1, 2)).to.throw("Invalid number 'Number (0) can not be smaller than 1'.") + expect(() => new ValidNumber(3, 1, 2)).to.throw("Invalid number 'Number (3) can not be bigger than 2'.") + }) +}) diff --git a/src/validObjects/ValidNumber.ts b/src/validObjects/ValidNumber.ts index 27e2094..6611d15 100644 --- a/src/validObjects/ValidNumber.ts +++ b/src/validObjects/ValidNumber.ts @@ -1,4 +1,5 @@ import { InvalidNumberError } from '@/errors/InvalidNumberError' +import { ValidObjectLogicError } from '@/errors/ValidObjectLogicError' // tslint:disable-next-line:no-any const validate = (val: any): number => { @@ -16,8 +17,17 @@ export class ValidNumber { private readonly val: number // tslint:disable-next-line:no-any - constructor(val: any) { + constructor(val: any, rangeFromInclusive?: number, rangeToInclusive?: number) { + if (rangeFromInclusive !== undefined && rangeToInclusive !== undefined && rangeFromInclusive > rangeToInclusive) { + throw new ValidObjectLogicError(`Parameter rangeFromInclusive (${rangeFromInclusive}) can not be bigger then rangeToInclusive (${rangeToInclusive})`) + } this.val = validate(val) + if (rangeFromInclusive && this.value < rangeFromInclusive) { + throw new InvalidNumberError(`Number (${this.value}) can not be smaller than ${rangeFromInclusive}`) + } + if (rangeToInclusive && this.value > rangeToInclusive) { + throw new InvalidNumberError(`Number (${this.value}) can not be bigger than ${rangeToInclusive}`) + } } get value(): number {