Skip to content

Commit d277fb6

Browse files
committed
Added isFieldValid method to Validation
1 parent 01e4e2a commit d277fb6

3 files changed

Lines changed: 52 additions & 6 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Validation {
3737
storage: string;
3838
statuses: Array<string>;
3939

40+
// TODO: errorsStorageName is misleading, storage contains fields mapped to statuses but not errors
4041
constructor(
4142
fields: FieldsDescription,
4243
errorsStorageName: string = 'validationStorage'
@@ -202,15 +203,15 @@ class Validation {
202203
}
203204

204205
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');
206+
const fieldsMappedToStatuses = state[this.storage];
207+
if (typeof fieldsMappedToStatuses !== 'object') {
208+
throw new Error('Invalid fieldsMappedToStatuses object, must be object');
208209
}
209210

210-
const keys = Object.keys(errors);
211+
const keys = Object.keys(fieldsMappedToStatuses);
211212
const [validationPassed] = this.statuses;
212213
for (let i = 0; i < keys.length; i++) {
213-
const currentStatuses = errors[keys[i]];
214+
const currentStatuses = fieldsMappedToStatuses[keys[i]];
214215
for (let j = 0; j < currentStatuses.length; j++) {
215216
if (currentStatuses[j] !== validationPassed) {
216217
return false;
@@ -220,6 +221,25 @@ class Validation {
220221
// if form valid return true
221222
return true;
222223
}
224+
225+
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+
}
230+
const fieldStatuses = fieldsMappedToStatuses[fieldName];
231+
if (!fieldStatuses) {
232+
throw new Error("Attempt to validate field that doesn't exist");
233+
}
234+
235+
const [validationPassed] = this.statuses;
236+
for (let j = 0; j < fieldStatuses.length; j++) {
237+
if (fieldStatuses[j] !== validationPassed) {
238+
return false;
239+
}
240+
}
241+
return true;
242+
}
223243
}
224244

225245
const allRulesInArrays = (

0 commit comments

Comments
 (0)