Skip to content

Commit

Permalink
Added isRequiredIf to exports
Browse files Browse the repository at this point in the history
Added isRequiredIf to exports, fixed bug and test in isRequiredIf, added
check for undefined allValues and updated docs
  • Loading branch information
madcat23 committed Dec 12, 2017
1 parent 2a7fe3b commit 61cce86
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 71 deletions.
124 changes: 60 additions & 64 deletions bin/simple-object-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@
}
});

var _isEqualTo = __webpack_require__(15);

Object.defineProperty(exports, 'isEqualTo', {
enumerable: true,
get: function get() {
return _interopRequireDefault(_isEqualTo).default;
}
});

var _utils = __webpack_require__(7);

Object.defineProperty(exports, 'containsError', {
Expand Down Expand Up @@ -714,86 +723,73 @@
value: true
});

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _validator = __webpack_require__(1);

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _validator2 = _interopRequireDefault(_validator);

var _utils = __webpack_require__(7);

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var defaultMessageCreator = function defaultMessageCreator(fieldName1, fieldName2) {
return fieldName1 + ' is required when ' + fieldName2 + ' is given.';
};
exports.default = (0, _validator2.default)(function (value, param, allValues) {
if (typeof param !== 'function') {
throw Error('Parameter must be a function.');
}
var condition = param(allValues);
if (condition) {
return !(0, _utils.isValueEmpty)(value);
}
return true;
}, function (param, name) {
return name + ' is required.';
});

// eslint-disable-next-line max-len
var regularIsRequiredIf = function regularIsRequiredIf(property1, name1, property2, name2) {
var messageCreator = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : defaultMessageCreator;
var nameTransformer = arguments[5];
return function (value, result) {
if (typeof value === 'undefined') {
return result;
}
/***/ },
/* 15 */
/***/ function(module, exports, __webpack_require__) {

var fieldName1 = nameTransformer ? nameTransformer(name1) : name1;
var fieldName2 = nameTransformer ? nameTransformer(name2) : name2;
'use strict';

var value1 = value[property1];
var value2 = value[property2];
Object.defineProperty(exports, "__esModule", {
value: true
});

if (!(0, _utils.isValueEmpty)(value2) && (0, _utils.isValueEmpty)(value1)) {
return _extends({}, result, _defineProperty({}, property1, messageCreator(fieldName1, fieldName2, value1, value2)));
}
return result;
};
};
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

var isRequiredIf = function isRequiredIf(param1, param2) {
/*
* Called to override messageCreator and/or nameTransformer function
*/
if (!Array.isArray(param1) && (typeof param1 === 'undefined' ? 'undefined' : _typeof(param1)) === 'object') {
var _ret = function () {
var messageCreator = param1.messageCreator ? param1.messageCreator : undefined;
var nameTransformer = param1.nameTransformer ? param1.nameTransformer : undefined;

// eslint-disable-next-line max-len
return {
v: function v(_ref, _ref2) {
var _ref4 = _slicedToArray(_ref, 2),
p1 = _ref4[0],
n1 = _ref4[1];

var _ref3 = _slicedToArray(_ref2, 2),
p2 = _ref3[0],
n2 = _ref3[1];

return regularIsRequiredIf(p1, n1, p2, n2, messageCreator, nameTransformer);
}
};
}();
var _validator = __webpack_require__(1);

if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
var _validator2 = _interopRequireDefault(_validator);

/*
* Called in a regular way
*/
var _utils = __webpack_require__(7);

var _param = _slicedToArray(param1, 2),
property1 = _param[0],
name1 = _param[1];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var _param2 = _slicedToArray(param2, 2),
property2 = _param2[0],
name2 = _param2[1];
var ERROR_MESSAGE = 'Parameter must be an object like { property: \'foo\', name: \'bar\' } or { value: values => values.foo.bar, name: \'bar\' }.';

return regularIsRequiredIf(property1, name1, property2, name2, undefined, undefined);
};
exports.default = (0, _validator2.default)(function (value, param, allValues) {
if ((typeof param === 'undefined' ? 'undefined' : _typeof(param)) !== 'object') {
throw Error(ERROR_MESSAGE);
}
if (typeof param.value !== 'function' && typeof param.property !== 'string') {
throw Error(ERROR_MESSAGE);
}

if (typeof allValues === 'undefined') {
return true;
}

exports.default = isRequiredIf;
var otherValue = typeof param.property === 'string' ? allValues[param.property] : param.value(allValues);

if ((0, _utils.isValueEmpty)(value) && (0, _utils.isValueEmpty)(otherValue)) {
return true;
}
return value === otherValue;
}, function (param, name) {
if (typeof param.name === 'string') {
return name + ' must be equal to ' + param.name + '.';
}
return 'The values must be equal.';
});

/***/ }
/******/ ])));
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ <h2 id="is-equal-to">isEqualTo</h2>
baz: isRequired('Baz')
}),
bar: isEqualTo({
value: values => values.foo.baz,
value: values => values.foo && values.foo.baz,
name: 'Baz'
})('Bar')
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-object-validation",
"version": "1.1.1",
"version": "1.1.2",
"description": "Library for JavaScript Object validation",
"main": "./bin/simple-object-validation.js",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export { default as isRequired } from './validators/isRequired'
export { default as isNumeric } from './validators/isNumeric'
export { default as isAlphanumeric } from './validators/isAlphanumeric'
export { default as isInteger } from './validators/isInteger'
export { default as isRequiredIf } from './reducers/isRequiredIf'
export { default as isRequiredIf } from './validators/isRequiredIf'
export { default as isEqualTo } from './validators/isEqualTo'
export { containsError } from './utils/utils'
4 changes: 4 additions & 0 deletions src/validators/isEqualTo.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export default validator(
throw Error(ERROR_MESSAGE)
}

if (typeof allValues === 'undefined') {
return true
}

const otherValue = typeof param.property === 'string' ? allValues[param.property] : param.value(allValues)

if (isValueEmpty(value) && isValueEmpty(otherValue)) {
Expand Down
2 changes: 1 addition & 1 deletion src/validators/isRequiredIf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default validator(
if (condition) {
return !isValueEmpty(value)
}
return undefined
return true
},
(param, name) => `${name} is required.`,
)
9 changes: 6 additions & 3 deletions src/validators/isRequiredIf.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import isRequiredIf from './isRequiredIf'
/* global test, it, expect */

test('string -> OK', () => {
test('required if', () => {
const validate = isRequiredIf(values => values.otherValue === 'foo')('Field')

const values = {
Expand All @@ -10,10 +10,13 @@ test('string -> OK', () => {
}

expect(validate('bar', values)).toBeUndefined()
expect(validate('', values)).toEqual('Field is required.')

const values2 = {
foo: 'bar',
otherValue: 'bar',
otherValue: 'fooXXX',
}
expect(validate('bar', values2)).toEqual('Field is required.')

expect(validate('bar', values2)).toBeUndefined()
expect(validate('', values2)).toBeUndefined()
})

0 comments on commit 61cce86

Please sign in to comment.