Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add isTrue and isFalse checks on boolean #910

Merged
merged 1 commit into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -51,7 +51,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 @@ -74,4 +74,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),
]);
});
});