Skip to content

Commit cb5a73f

Browse files
committed
Changed names and replaced error throwing with console warn
1 parent 5db62d8 commit cb5a73f

3 files changed

Lines changed: 22 additions & 22 deletions

File tree

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"keyword-spacing": ["warn", {"after": true}],
2020
"jsx-quotes": ["warn", "prefer-double"],
2121
"no-extra-boolean-cast": "off",
22+
"no-console": 0,
2223
"no-multi-spaces": "warn",
2324
"no-spaced-func": "warn",
2425
"no-unused-vars": "warn",

__tests__/index.test.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,5 @@ describe('Unit tests for Validation class', () => {
212212
state = Object.assign({}, state, result);
213213
expect(Validator.isFieldValid(state, 'login')).toEqual(true);
214214
});
215-
216-
test('Validator.isFieldValid(state,"asdasd") method throws error due to attempt to validate not existed field', () => {
217-
expect(() => {
218-
Validator.isFieldValid(state, 'asdasd');
219-
}).toThrow();
220-
});
221215
});
222216
});

src/index.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ class Validation {
3737
storage: string;
3838
statuses: Array<string>;
3939

40-
// TODO: errorsStorageName is misleading, storage contains fields mapped to statuses but not errors
4140
constructor(
4241
fields: FieldsDescription,
43-
errorsStorageName: string = 'validationStorage'
42+
validationStorageName: string = 'validationStorage'
4443
) {
4544
if (typeof fields !== 'object') {
4645
throw new Error('Invalid fields parameter for fields, must be object');
@@ -49,7 +48,7 @@ class Validation {
4948
this.fields = allRulesInArrays(fields);
5049
this.fieldsToValidateList = [];
5150
this.fieldsToShowErrors = [];
52-
this.storage = errorsStorageName;
51+
this.validationStorageName = validationStorageName;
5352
this.statuses = [
5453
'validation-passed',
5554
'prevalidation-failed',
@@ -95,7 +94,7 @@ class Validation {
9594
);
9695

9796
return Object.assign(state, {
98-
[this.storage]: toStorage
97+
[this.validationStorageName]: toStorage
9998
});
10099
}
101100

@@ -130,7 +129,7 @@ class Validation {
130129
// computing the state as a merge from prevState and stateUpdates to do the right validation
131130
let state = Object.assign({}, prevState, stateUpdates || {});
132131
// clean the service error storage field, so the rule will have no acces to it
133-
delete state[this.storage];
132+
delete state[this.validationStorageName];
134133
keysToValidate.map(key => {
135134
if (this.fields[key]) {
136135
toStorage[key] = this._validateField(
@@ -143,7 +142,11 @@ class Validation {
143142
});
144143
this.fieldsToShowErrors = [];
145144
return Object.assign(stateUpdates || {}, {
146-
[this.storage]: Object.assign({}, prevState[this.storage], toStorage)
145+
[this.validationStorageName]: Object.assign(
146+
{},
147+
prevState[this.validationStorageName],
148+
toStorage
149+
)
147150
});
148151
};
149152
}
@@ -186,7 +189,7 @@ class Validation {
186189
const validationFailed = this.statuses[2];
187190

188191
keys.map(key => {
189-
const current = state[this.storage][key];
192+
const current = state[this.validationStorageName][key];
190193
// check every rule
191194
for (let i = 0; i < current.length; i++) {
192195
if (current[i] === validationFailed) {
@@ -203,15 +206,15 @@ class Validation {
203206
}
204207

205208
isFormValid(state: Object): boolean {
206-
const fieldsMappedToStatuses = state[this.storage];
207-
if (typeof fieldsMappedToStatuses !== 'object') {
209+
const storage = state[this.validationStorageName];
210+
if (typeof storage !== 'object') {
208211
throw new Error('Invalid fieldsMappedToStatuses object, must be object');
209212
}
210213

211-
const keys = Object.keys(fieldsMappedToStatuses);
214+
const keys = Object.keys(storage);
212215
const [validationPassed] = this.statuses;
213216
for (let i = 0; i < keys.length; i++) {
214-
const currentStatuses = fieldsMappedToStatuses[keys[i]];
217+
const currentStatuses = storage[keys[i]];
215218
for (let j = 0; j < currentStatuses.length; j++) {
216219
if (currentStatuses[j] !== validationPassed) {
217220
return false;
@@ -223,13 +226,15 @@ class Validation {
223226
}
224227

225228
isFieldValid(state: Object, fieldName: string): boolean {
226-
const fieldsMappedToStatuses = state[this.storage];
227-
if (typeof fieldsMappedToStatuses !== 'object') {
228-
throw new Error('Invalid fieldsMappedToStatuses object, must be object');
229+
const storage = state[this.validationStorageName];
230+
if (typeof storage !== 'object') {
231+
throw new Error('Invalid storage object, must be object');
229232
}
230-
const fieldStatuses = fieldsMappedToStatuses[fieldName];
233+
const fieldStatuses = storage[fieldName];
231234
if (!fieldStatuses) {
232-
throw new Error("Attempt to validate field that doesn't exist");
235+
// TODO: how to disable warnings in production
236+
console.warn("Attempt to validate field that doesn't exist");
237+
return false;
233238
}
234239

235240
const [validationPassed] = this.statuses;

0 commit comments

Comments
 (0)