From 075f326ac4cd68ef0277188cd5a6e619d93abcec Mon Sep 17 00:00:00 2001 From: Sam Verschueren Date: Fri, 27 Oct 2017 07:35:54 +0200 Subject: [PATCH] Add boolean validator (#13) --- source/lib/predicates/boolean.ts | 28 ++++++++++++++++++++++++++++ source/ow.ts | 8 ++++++++ source/test/boolean.ts | 23 +++++++++++++++++++++++ tslint.json | 2 +- 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 source/lib/predicates/boolean.ts create mode 100644 source/test/boolean.ts diff --git a/source/lib/predicates/boolean.ts b/source/lib/predicates/boolean.ts new file mode 100644 index 0000000..1050efa --- /dev/null +++ b/source/lib/predicates/boolean.ts @@ -0,0 +1,28 @@ +import { Predicate, Context } from './predicate'; + +export class BooleanPredicate extends Predicate { + + constructor(context?: Context) { + super('boolean', context); + } + + /** + * Test a boolean to be true. + */ + get true() { + return this.addValidator({ + message: value => `Expected ${value} to be true`, + validator: value => value === true + }); + } + + /** + * Test a boolean to be false. + */ + get false() { + return this.addValidator({ + message: value => `Expected ${value} to be false`, + validator: value => value === false + }); + } +} diff --git a/source/ow.ts b/source/ow.ts index 20d1215..f1d6213 100644 --- a/source/ow.ts +++ b/source/ow.ts @@ -2,6 +2,7 @@ import { ArgumentError } from './lib/argument-error'; import { Predicate, validatorSymbol } from './lib/predicates/predicate'; import { StringPredicate } from './lib/predicates/string'; import { NumberPredicate } from './lib/predicates/number'; +import { BooleanPredicate } from './lib/predicates/boolean'; export interface Ow { (value: any, predicate: Predicate): void; @@ -13,6 +14,10 @@ export interface Ow { * Test the value to be a number. */ number: NumberPredicate; + /** + * Test the value to be a boolean. + */ + boolean: BooleanPredicate; } const main = (value: any, predicate: Predicate) => { @@ -30,6 +35,9 @@ Object.defineProperties(main, { }, number: { get: () => new NumberPredicate() + }, + boolean: { + get: () => new BooleanPredicate() } }); diff --git a/source/test/boolean.ts b/source/test/boolean.ts new file mode 100644 index 0000000..26414ec --- /dev/null +++ b/source/test/boolean.ts @@ -0,0 +1,23 @@ +import test from 'ava'; +import * as m from '..'; + +test('boolean', t => { + t.notThrows(() => m(true, m.boolean)); + t.throws(() => m('12', m.boolean), 'Expected argument to be of type `boolean` but received type `string`'); +}); + +test('boolean.true', t => { + t.notThrows(() => m(true, m.boolean.true)); + t.notThrows(() => m(Boolean(true), m.boolean.true)); + t.notThrows(() => m(Boolean(1), m.boolean.true)); + t.throws(() => m(false, m.boolean.true), 'Expected false to be true'); + t.throws(() => m(Boolean(0), m.boolean.true), 'Expected false to be true'); +}); + +test('boolean.false', t => { + t.notThrows(() => m(false, m.boolean.false)); + t.notThrows(() => m(Boolean(false), m.boolean.false)); + t.notThrows(() => m(Boolean(0), m.boolean.false)); + t.throws(() => m(true, m.boolean.false), 'Expected true to be false'); + t.throws(() => m(Boolean(1), m.boolean.false), 'Expected true to be false'); +}); diff --git a/tslint.json b/tslint.json index 4cc6fe3..587fa41 100644 --- a/tslint.json +++ b/tslint.json @@ -70,7 +70,7 @@ "newline-before-return": true, "new-parens": true, "no-angle-bracket-type-assertion": true, - "no-boolean-literal-compare": true, + "no-boolean-literal-compare": false, "no-consecutive-blank-lines": true, "no-irregular-whitespace": true, "no-reference-import": true,