Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions addon/presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ import unwrapProxy from 'ember-validators/utils/unwrap-proxy';
* @param {Object} options
* @param {Boolean} options.presence If true validates that the given value is not empty,
* if false, validates that the given value is empty.
* @param {Boolean} options.ignoreBlank If true, treats an empty or whitespace string as not present
* @param {Boolean} options.ignoreBlank If true, treats a whitespace string as not present
* @param {Boolean} options.allowEmpty If true, treats an empty string as present
* @param {Object} model
* @param {String} attribute
*/
export default function validatePresence(value, options, model, attribute) {
let { presence, ignoreBlank } = options;
let { presence, ignoreBlank, allowEmpty } = options;
let v = unwrapProxy(value);
let _isPresent = ignoreBlank ? isPresent(v) : !isEmpty(v);

if (!_isPresent && allowEmpty && v === '') {
_isPresent = true;
}

assert(
`[validator:presence] [${attribute}] option 'presence' is required`,
isPresent(presence)
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/validators/presence-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ test('presence - value present', function (assert) {
assert.true(processResult(result));
});

test('presence - value empty', function (assert) {
assert.expect(1);

options = { presence: true };

result = validate('', cloneOptions(options));
assert.strictEqual(processResult(result), "This field can't be blank");
});

test('presence with allowEmpty - value empty', function (assert) {
assert.expect(1);

options = { presence: true, allowEmpty: true };

result = validate('', cloneOptions(options));
assert.true(processResult(result));
});

test('presence - value blank', function (assert) {
assert.expect(1);

Expand Down