Skip to content

Commit cf555ae

Browse files
author
Mark
committed
add present validation
1 parent a86c162 commit cf555ae

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/Validations/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,30 @@ Validations.nullable = function (data, field, message, args, validations) {
15411541
});
15421542
};
15431543

1544+
/**
1545+
* @description Validate the value must exist
1546+
* values
1547+
* @method nullable
1548+
* @param {Object} data
1549+
* @param {String} field
1550+
* @param {String} message
1551+
* @param {Array} args
1552+
* @param {Array} validations
1553+
* @return {Boolean}
1554+
* @public
1555+
*/
1556+
Validations.present = function (data, field, message, args, validations) {
1557+
return new Promise(function (resolve, reject) {
1558+
const fieldValue = _.get(data, field);
1559+
1560+
if (typeof fieldValue !== 'undefined') {
1561+
resolve('validation passed');
1562+
}
1563+
1564+
reject(message);
1565+
});
1566+
};
1567+
15441568
/**
15451569
* aliases
15461570
*/

test/validations.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2909,4 +2909,38 @@ describe('Validations', function () {
29092909

29102910
});
29112911

2912+
describe('Present', function () {
2913+
it('should work fine when field value is exist', function *() {
2914+
const data = { username: 'DDD' };
2915+
const field = 'username';
2916+
const message = 'Username should be present';
2917+
const args = [];
2918+
const passes = yield Validations.present(data, field, message, args);
2919+
expect(passes).to.equal('validation passed');
2920+
});
2921+
2922+
it('should work fine when field value is null', function *() {
2923+
const data = { username: null };
2924+
const field = 'username';
2925+
const message = 'Username should be present';
2926+
const args = [];
2927+
const passes = yield Validations.present(data, field, message, args);
2928+
expect(passes).to.equal('validation passed');
2929+
});
2930+
2931+
it('should throw an error when the field value is a undefined', function *() {
2932+
const data = { username: undefined };
2933+
const field = 'username';
2934+
const message = 'Username should be a upper case string';
2935+
const args = [];
2936+
try {
2937+
const passes = yield Validations.present(data, field, message, args);
2938+
expect(passes).not.to.exist();
2939+
} catch (e) {
2940+
expect(e).to.equal(message);
2941+
}
2942+
});
2943+
2944+
});
2945+
29122946
});

0 commit comments

Comments
 (0)