Skip to content

Commit

Permalink
Add nullOrBoolean assert
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotorres97 authored and Americas committed Jul 2, 2021
1 parent 1bef034 commit fb3f9df
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The following set of extra asserts are provided by this package:
- [Json](#json)
- [NotEmpty](#notempty)
- [NullOrDate](#nullordate)
- [NullOrBoolean](#nullorboolean)
- [NullOrString](#nullorstring)
- [Phone](#phone) (requires `google-libphonenumber`)
- [PlainObject](#plainobject)
Expand Down Expand Up @@ -191,6 +192,9 @@ Tests if the value is valid json.
### NotEmpty
Tests if the value is not an empty (empty object, empty array, empty string, etc).

### NullOrBoolean
Tests if the value is a `null` or `boolean`.

### NullOrString
Tests if the value is a `null` or `string`, optionally within some boundaries.

Expand Down
33 changes: 33 additions & 0 deletions src/asserts/null-or-boolean-assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

/**
* Module dependencies.
*/

const { Violation } = require('validator.js');

/**
* Export `NullOrBooleanAssert`.
*/

module.exports = function nullOrBooleanAssert() {
/**
* Class name.
*/

this.__class__ = 'NullOrBoolean';

/**
* Validation algorithm.
*/

this.validate = value => {
if (value !== null && typeof value !== 'boolean') {
throw new Violation(this, value, { value: 'must_be_null_or_a_boolean' });
}

return true;
};

return this;
};
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const Ip = require('./asserts/ip-assert.js');
const Iso3166Country = require('./asserts/iso-3166-country-assert.js');
const Json = require('./asserts/json-assert.js');
const NotEmpty = require('./asserts/not-empty-assert.js');
const NullOrBoolean = require('./asserts/null-or-boolean-assert.js');
const NullOrDate = require('./asserts/null-or-date-assert.js');
const NullOrString = require('./asserts/null-or-string-assert.js');
const Phone = require('./asserts/phone-assert.js');
Expand Down Expand Up @@ -70,6 +71,7 @@ module.exports = {
Iso3166Country,
Json,
NotEmpty,
NullOrBoolean,
NullOrDate,
NullOrString,
Phone,
Expand Down
59 changes: 59 additions & 0 deletions test/asserts/null-or-boolean-assert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

/**
* Module dependencies.
*/

const { Assert: BaseAssert, Violation } = require('validator.js');
const NullOrBooleanAssert = require('../../src/asserts/null-or-boolean-assert');

/**
* Extend `Assert` with `NullOrBooleanAssert`.
*/

const Assert = BaseAssert.extend({
NullOrBoolean: NullOrBooleanAssert
});

/**
* Test `NullOrBooleanAssert`.
*/

describe('NullOrBooleanAssert', () => {
it('should throw an error if the input value is not a `null` or a boolean', () => {
const choices = [[], {}, 123, new Boolean(true), 'foo']; // eslint-disable-line no-new-wrappers

choices.forEach(choice => {
try {
Assert.nullOrBoolean().validate(choice);

fail();
} catch (e) {
expect(e).toBeInstanceOf(Violation);
expect(e.violation.value).toBe('must_be_null_or_a_boolean');
}
});
});

it('should expose `assert` equal to `NullOrBoolean`', () => {
try {
Assert.nullOrBoolean().validate({});

fail();
} catch (e) {
expect(e.show().assert).toBe('NullOrBoolean');
}
});

it('should accept `null`', () => {
Assert.nullOrBoolean().validate(null);
});

it('should accept a `true` boolean value', () => {
Assert.nullOrBoolean().validate(true);
});

it('should accept a `false` boolean value', () => {
Assert.nullOrBoolean().validate(false);
});
});
3 changes: 2 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('validator.js-asserts', () => {
it('should export all asserts', () => {
const assertNames = Object.keys(asserts);

expect(assertNames).toHaveLength(35);
expect(assertNames).toHaveLength(36);
expect(assertNames).toEqual(
expect.arrayContaining([
'AbaRoutingNumber',
Expand Down Expand Up @@ -42,6 +42,7 @@ describe('validator.js-asserts', () => {
'Iso3166Country',
'Json',
'NotEmpty',
'NullOrBoolean',
'NullOrDate',
'NullOrString',
'Phone',
Expand Down

0 comments on commit fb3f9df

Please sign in to comment.