Skip to content

Commit

Permalink
feat: add isTrue and isFalse checks on boolean (#910)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdohnal authored Nov 30, 2020
1 parent 5316ab9 commit 630a641
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/boolean.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import inherits from './util/inherits';
import MixedSchema from './mixed';
import { boolean as locale } from './locale';
import isAbsent from './util/isAbsent';

export default BooleanSchema;

Expand All @@ -25,4 +27,28 @@ inherits(BooleanSchema, MixedSchema, {

return typeof v === 'boolean';
},

isTrue(message = locale.isValue) {
return this.test({
message,
name: 'is-value',
exclusive: true,
params: { value: 'true' },
test(value) {
return isAbsent(value) || value === true;
},
});
},

isFalse(message = locale.isValue) {
return this.test({
message,
name: 'is-value',
exclusive: true,
params: { value: 'false' },
test(value) {
return isAbsent(value) || value === false;
},
});
},
});
4 changes: 3 additions & 1 deletion src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ export let date = {
max: '${path} field must be at earlier than ${max}',
};

export let boolean = {};
export let boolean = {
isValue: '${path} field must be ${value}',
};

export let object = {
noUnknown: '${path} field has unspecified keys: ${unknown}',
Expand Down
30 changes: 30 additions & 0 deletions test/bool.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,34 @@ describe('Boolean types', () => {
}),
]);
});

it('should check isTrue correctly', () => {
return Promise.all([
bool()
.isTrue()
.isValid(true)
.should.eventually()
.equal(true),
bool()
.isTrue()
.isValid(false)
.should.eventually()
.equal(false),
]);
});

it('should check isFalse correctly', () => {
return Promise.all([
bool()
.isFalse()
.isValid(false)
.should.eventually()
.equal(true),
bool()
.isFalse()
.isValid(true)
.should.eventually()
.equal(false),
]);
});
});

0 comments on commit 630a641

Please sign in to comment.