Skip to content

Commit 69f1bc1

Browse files
committed
build renewed
1 parent e1f0b7f commit 69f1bc1

1 file changed

Lines changed: 35 additions & 11 deletions

File tree

build/index.js

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
3232
*/
3333
var Validation = function () {
3434
function Validation(fields) {
35-
var errorsStorageName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'validationStorage';
35+
var validationStorageName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'validationStorage';
3636

3737
_classCallCheck(this, Validation);
3838

@@ -43,7 +43,7 @@ var Validation = function () {
4343
this.fields = allRulesInArrays(fields);
4444
this.fieldsToValidateList = [];
4545
this.fieldsToShowErrors = [];
46-
this.storage = errorsStorageName;
46+
this.validationStorageName = validationStorageName;
4747
this.statuses = ['validation-passed', 'prevalidation-failed', 'validation-failed'];
4848
}
4949

@@ -79,7 +79,7 @@ var Validation = function () {
7979
return toStorage[key] = _this._validateField(state[key], _this.fields[key], state, showErrorsOnStart);
8080
});
8181

82-
return Object.assign(state, _defineProperty({}, this.storage, toStorage));
82+
return Object.assign(state, _defineProperty({}, this.validationStorageName, toStorage));
8383
}
8484

8585
/**
@@ -119,14 +119,14 @@ var Validation = function () {
119119
// computing the state as a merge from prevState and stateUpdates to do the right validation
120120
var state = Object.assign({}, prevState, stateUpdates || {});
121121
// clean the service error storage field, so the rule will have no acces to it
122-
delete state[_this2.storage];
122+
delete state[_this2.validationStorageName];
123123
keysToValidate.map(function (key) {
124124
if (_this2.fields[key]) {
125125
toStorage[key] = _this2._validateField(state[key], _this2.fields[key], prevState, showChoosenErrors ? showErrorsHash[key] : showErrors);
126126
}
127127
});
128128
_this2.fieldsToShowErrors = [];
129-
return Object.assign(stateUpdates || {}, _defineProperty({}, _this2.storage, Object.assign({}, prevState[_this2.storage], toStorage)));
129+
return Object.assign(stateUpdates || {}, _defineProperty({}, _this2.validationStorageName, Object.assign({}, prevState[_this2.validationStorageName], toStorage)));
130130
};
131131
}
132132
}, {
@@ -176,7 +176,7 @@ var Validation = function () {
176176
var validationFailed = this.statuses[2];
177177

178178
keys.map(function (key) {
179-
var current = state[_this4.storage][key];
179+
var current = state[_this4.validationStorageName][key];
180180
// check every rule
181181
for (var i = 0; i < current.length; i++) {
182182
if (current[i] === validationFailed) {
@@ -194,18 +194,18 @@ var Validation = function () {
194194
}, {
195195
key: 'isFormValid',
196196
value: function isFormValid(state) {
197-
var errors = state[this.storage];
198-
if ((typeof errors === 'undefined' ? 'undefined' : _typeof(errors)) !== 'object') {
199-
throw new Error('Invalid errors parameter for fields, must be object');
197+
var storage = state[this.validationStorageName];
198+
if ((typeof storage === 'undefined' ? 'undefined' : _typeof(storage)) !== 'object') {
199+
throw new Error('Invalid fieldsMappedToStatuses object, must be object');
200200
}
201201

202-
var keys = Object.keys(errors);
202+
var keys = Object.keys(storage);
203203

204204
var _statuses2 = _slicedToArray(this.statuses, 1),
205205
validationPassed = _statuses2[0];
206206

207207
for (var i = 0; i < keys.length; i++) {
208-
var currentStatuses = errors[keys[i]];
208+
var currentStatuses = storage[keys[i]];
209209
for (var j = 0; j < currentStatuses.length; j++) {
210210
if (currentStatuses[j] !== validationPassed) {
211211
return false;
@@ -215,6 +215,30 @@ var Validation = function () {
215215
// if form valid return true
216216
return true;
217217
}
218+
}, {
219+
key: 'isFieldValid',
220+
value: function isFieldValid(state, fieldName) {
221+
var storage = state[this.validationStorageName];
222+
if ((typeof storage === 'undefined' ? 'undefined' : _typeof(storage)) !== 'object') {
223+
throw new Error('Invalid storage object, must be object');
224+
}
225+
var fieldStatuses = storage[fieldName];
226+
if (!fieldStatuses) {
227+
// TODO: how to disable warnings in production
228+
console.warn("Attempt to validate field that doesn't exist");
229+
return false;
230+
}
231+
232+
var _statuses3 = _slicedToArray(this.statuses, 1),
233+
validationPassed = _statuses3[0];
234+
235+
for (var j = 0; j < fieldStatuses.length; j++) {
236+
if (fieldStatuses[j] !== validationPassed) {
237+
return false;
238+
}
239+
}
240+
return true;
241+
}
218242
}]);
219243

220244
return Validation;

0 commit comments

Comments
 (0)