Skip to content

Commit e1f0b7f

Browse files
authored
Merge pull request #16 from nullgr/devel
Devel
2 parents 01e4e2a + 237fdb1 commit e1f0b7f

4 files changed

Lines changed: 65 additions & 13 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",

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ If there are many rules, the their priority will be similar to the array order.
156156

157157
## Api
158158

159-
### constructor({FieldsDescription}, errorsStorageName = 'validationStorage')
159+
### constructor({FieldsDescription}, validationStorageName = 'validationStorage')
160160
Describe in the constructor all the fields, that you will check. Like in the [example](#form-example).
161161
By default all validation data will be added to the 'validationStorage' key of the state object. You can change it, if you need. You can describe for each field [1 or many rules](#creating-validation-rules).
162162

@@ -228,7 +228,11 @@ this.setState(
228228
Use this method inside the render function, like in the [example](#form-example). It will return the object with fields keys and their error messages. If the field is valid there will be an empty error string.
229229

230230
### isFormValid({state})
231-
Use this method to check, if the field is valid. It will return true, if all the fields in the form are valid. See the [example](#form-example)
231+
Use this method to check, if the form is valid. It will return true, if all the fields in the form are valid. See the [example](#form-example)
232+
233+
### isFieldValid({state}, fieldName)
234+
Use this method to check, if particular field is valid. Returns true if it is valid, false otherwise
235+
232236

233237
## Compatibility
234238
This package id fully compatible with the React v.16, because it uses state updater functions inside.

__tests__/index.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,27 @@ describe('Unit tests for Validation class', () => {
190190
state = Object.assign({}, state, result);
191191
expect(Validator.isFormValid(state)).toEqual(true);
192192
});
193+
194+
test('Validator.isFieldValid(state,"login") method returns false', () => {
195+
const updater = Validator.validate({
196+
login: ''
197+
});
198+
const result = updater(state);
199+
state = Object.assign({}, state, result);
200+
expect(Validator.isFieldValid(state, 'login')).toEqual(false);
201+
});
202+
203+
test('Validator.isFieldValid(state,"password") method returns true', () => {
204+
expect(Validator.isFieldValid(state, 'password')).toEqual(true);
205+
});
206+
207+
test('Validator.isFieldValid(state,"login") method returns true after login field became validated', () => {
208+
const updater = Validator.validate({
209+
login: 'peterson'
210+
});
211+
const result = updater(state);
212+
state = Object.assign({}, state, result);
213+
expect(Validator.isFieldValid(state, 'login')).toEqual(true);
214+
});
193215
});
194216
});

src/index.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Validation {
3939

4040
constructor(
4141
fields: FieldsDescription,
42-
errorsStorageName: string = 'validationStorage'
42+
validationStorageName: string = 'validationStorage'
4343
) {
4444
if (typeof fields !== 'object') {
4545
throw new Error('Invalid fields parameter for fields, must be object');
@@ -48,7 +48,7 @@ class Validation {
4848
this.fields = allRulesInArrays(fields);
4949
this.fieldsToValidateList = [];
5050
this.fieldsToShowErrors = [];
51-
this.storage = errorsStorageName;
51+
this.validationStorageName = validationStorageName;
5252
this.statuses = [
5353
'validation-passed',
5454
'prevalidation-failed',
@@ -94,7 +94,7 @@ class Validation {
9494
);
9595

9696
return Object.assign(state, {
97-
[this.storage]: toStorage
97+
[this.validationStorageName]: toStorage
9898
});
9999
}
100100

@@ -129,7 +129,7 @@ class Validation {
129129
// computing the state as a merge from prevState and stateUpdates to do the right validation
130130
let state = Object.assign({}, prevState, stateUpdates || {});
131131
// clean the service error storage field, so the rule will have no acces to it
132-
delete state[this.storage];
132+
delete state[this.validationStorageName];
133133
keysToValidate.map(key => {
134134
if (this.fields[key]) {
135135
toStorage[key] = this._validateField(
@@ -142,7 +142,11 @@ class Validation {
142142
});
143143
this.fieldsToShowErrors = [];
144144
return Object.assign(stateUpdates || {}, {
145-
[this.storage]: Object.assign({}, prevState[this.storage], toStorage)
145+
[this.validationStorageName]: Object.assign(
146+
{},
147+
prevState[this.validationStorageName],
148+
toStorage
149+
)
146150
});
147151
};
148152
}
@@ -185,7 +189,7 @@ class Validation {
185189
const validationFailed = this.statuses[2];
186190

187191
keys.map(key => {
188-
const current = state[this.storage][key];
192+
const current = state[this.validationStorageName][key];
189193
// check every rule
190194
for (let i = 0; i < current.length; i++) {
191195
if (current[i] === validationFailed) {
@@ -202,15 +206,15 @@ class Validation {
202206
}
203207

204208
isFormValid(state: Object): boolean {
205-
const errors = state[this.storage];
206-
if (typeof errors !== 'object') {
207-
throw new Error('Invalid errors parameter for fields, must be object');
209+
const storage = state[this.validationStorageName];
210+
if (typeof storage !== 'object') {
211+
throw new Error('Invalid fieldsMappedToStatuses object, must be object');
208212
}
209213

210-
const keys = Object.keys(errors);
214+
const keys = Object.keys(storage);
211215
const [validationPassed] = this.statuses;
212216
for (let i = 0; i < keys.length; i++) {
213-
const currentStatuses = errors[keys[i]];
217+
const currentStatuses = storage[keys[i]];
214218
for (let j = 0; j < currentStatuses.length; j++) {
215219
if (currentStatuses[j] !== validationPassed) {
216220
return false;
@@ -220,6 +224,27 @@ class Validation {
220224
// if form valid return true
221225
return true;
222226
}
227+
228+
isFieldValid(state: Object, fieldName: string): boolean {
229+
const storage = state[this.validationStorageName];
230+
if (typeof storage !== 'object') {
231+
throw new Error('Invalid storage object, must be object');
232+
}
233+
const fieldStatuses = storage[fieldName];
234+
if (!fieldStatuses) {
235+
// TODO: how to disable warnings in production
236+
console.warn("Attempt to validate field that doesn't exist");
237+
return false;
238+
}
239+
240+
const [validationPassed] = this.statuses;
241+
for (let j = 0; j < fieldStatuses.length; j++) {
242+
if (fieldStatuses[j] !== validationPassed) {
243+
return false;
244+
}
245+
}
246+
return true;
247+
}
223248
}
224249

225250
const allRulesInArrays = (

0 commit comments

Comments
 (0)