Skip to content

Commit

Permalink
Add ValidBoolean
Browse files Browse the repository at this point in the history
  • Loading branch information
buresmi7 committed May 23, 2019
1 parent 8c29400 commit 3f739a3
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "valid-objects-ts",
"version": "1.0.10",
"version": "1.0.11",
"main": ".dist/index.js",
"types": ".dist/index.d.ts",
"repository": "[email protected]:Travelport-Czech/valid-objects-ts.git",
Expand Down
7 changes: 7 additions & 0 deletions src/errors/InvalidBooleanError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ValidObjectError } from '@/errors/ValidObjectError'

export class InvalidBooleanError extends ValidObjectError {
constructor(value: string) {
super("Invalid boolean '" + value + "'.")
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from '@/validObjects/ValidBoolean'
export * from '@/validObjects/ValidDate'
export * from '@/validObjects/ValidDateTime'
export * from '@/validObjects/ValidEmail'
Expand All @@ -9,6 +10,7 @@ export * from '@/validObjects/ValidLanguage'
export * from '@/validObjects/ValidIATALocation'
export * from '@/validObjects/ValidIATALocationList'

export * from '@/errors/InvalidBooleanError'
export * from '@/errors/InvalidDateError'
export * from '@/errors/InvalidDateTimeError'
export * from '@/errors/InvalidEmailError'
Expand Down
16 changes: 16 additions & 0 deletions src/tests/ValidBoolean.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ValidBoolean } from 'src'
import { expect } from 'chai'

describe('ValidBoolean', () => {
it('constructor', () => {
const testError = (val: string, err: string) => {
expect(() => new ValidBoolean(val)).to.throw(err)
}

expect(new ValidBoolean(true).value).to.eq(true)
expect(new ValidBoolean(true).toString()).to.eq('true')
expect(new ValidBoolean(false).value).to.eq(false)

testError('true', `Invalid boolean '"true" is type string'.`)
})
})
33 changes: 33 additions & 0 deletions src/validObjects/ValidBoolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { InvalidBooleanError } from '@/errors/InvalidBooleanError'

const validate = (val: unknown): boolean => {
if (typeof val !== 'boolean') {
throw new InvalidBooleanError(JSON.stringify(val) + ' is type ' + typeof val)
}

return val
}

export class ValidBoolean {
private readonly val: boolean

constructor(val: unknown) {
this.val = validate(val)
}

get value(): boolean {
return this.val
}

public toString() {
return this.val.toString()
}

get [Symbol.toStringTag]() {
return this.toString()
}

public toJSON() {
return this.toString()
}
}
6 changes: 2 additions & 4 deletions src/validObjects/ValidString.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { InvalidStringError } from '@/errors/InvalidStringError'

// tslint:disable-next-line:no-any
const validate = (val: any): string => {
const validate = (val: unknown): string => {
if (typeof val !== 'string') {
throw new InvalidStringError(JSON.stringify(val) + ' is type ' + typeof val)
}
Expand All @@ -15,8 +14,7 @@ const validate = (val: any): string => {
export class ValidString {
private readonly val: string

// tslint:disable-next-line:no-any
constructor(val: any) {
constructor(val: unknown) {
this.val = validate(val)
}

Expand Down

0 comments on commit 3f739a3

Please sign in to comment.