Skip to content

Commit

Permalink
fix: removed force validation, fix subsequent clear and reset, new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
foxhound87 committed Aug 20, 2016
1 parent ed1a1cf commit c9f5b30
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
11 changes: 3 additions & 8 deletions src/form.field.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ export default class Field {
}

@action
validate(force = false, showErrors = true, form = null) {
validate(showErrors = true, form = null) {
if (form) this.form = form;
this.setValid();

// exit on silent mode (on reset and clear)
if (this.silent === true) {
Expand All @@ -164,10 +165,7 @@ export default class Field {
}

// not execute if no valid function or ajv rules
if (!this.validateProperty && !this.form.ajv) {
this.setValid();
return;
}
if (!this.validateProperty && !this.form.ajv) return;

// Use "ajv" Rules
if (this.form.ajv) this.handleAjvValidationRules(showErrors);
Expand Down Expand Up @@ -196,9 +194,6 @@ export default class Field {
return;
}
}

this.setValid();
return;
}

handleValidateProperty(showErrors) {
Expand Down
10 changes: 5 additions & 5 deletions src/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,19 @@ export default class Form {
this.validateField(key, true)));
}

validateFields(force = true, showErrors = true) {
validateFields(showErrors = true) {
_.forEach(this.fields, (field) =>
field.validate(force, showErrors, this));
field.validate(showErrors, this));
}

validateField(key = null, recursive = false, force = true, showErrors = true) {
validateField(key = null, recursive = false, showErrors = true) {
if (!key) throw new Error('validateField: No field key provided');

// validate field by key
this.fields[key].validate(force, showErrors, this);
this.fields[key].validate(showErrors, this);

/*
validate related fields if specified
validate 'related' fields if specified
and recursive validation allowed
*/
if (!recursive) return;
Expand Down
27 changes: 27 additions & 0 deletions tests/data/form.m.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Form from '../../src/index';

const fields = {
username: {
label: 'Username',
value: 'SteveJobs',
},
email: {
label: 'Email',
value: '[email protected]',
},
password: {
label: 'Password',
value: 'thinkdifferent',
},
};

const schema = {
type: 'object',
properties: {
username: { type: 'string', minLength: 6, maxLength: 20 },
email: { type: 'string', format: 'email', minLength: 5, maxLength: 20 },
password: { type: 'string', minLength: 6, maxLength: 20 },
},
};

export default new Form({ fields, schema });
17 changes: 13 additions & 4 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ import $G from './data/form.g.js';
import $H from './data/form.h.js';
import $I from './data/form.i.js';
import $L from './data/form.l.js';
import $M from './data/form.m.js';

// do some stuff
// do some stuff (DIFFERENT FORMS)
$C.invalidate('The user already exist');
$D.update({ username: 'JonathanIve' });
$I.update({ username: 'JonathanIve' });
$I.reset();
$L.clear();
$I.reset(); // to original values
$L.clear(); // to empty values

// subsequent clear and reset (SAME FORM)
$M.clear(); // to empty values
$M.reset(); // to original values

describe('isValid', () => {
it('$A isValid should be true', () => expect($A.isValid).to.be.true);
Expand All @@ -29,7 +34,8 @@ describe('isValid', () => {
it('$G isValid should be true', () => expect($G.isValid).to.be.true);
it('$H isValid should be true', () => expect($H.isValid).to.be.true);
it('$I isValid should be true', () => expect($I.isValid).to.be.true);
it('$L isDirty should be false', () => expect($L.isValid).to.be.false);
it('$L isValid should be false', () => expect($L.isValid).to.be.false);
it('$M isValid should be true', () => expect($M.isValid).to.be.true);
});

describe('isDirty', () => {
Expand All @@ -43,6 +49,7 @@ describe('isDirty', () => {
it('$H isDirty should be true', () => expect($H.isDirty).to.be.true);
it('$I isDirty should be true', () => expect($I.isDirty).to.be.true);
it('$L isDirty should be false', () => expect($L.isDirty).to.be.false);
it('$M isDirty should be true', () => expect($M.isDirty).to.be.true);
});

describe('validate()', () => {
Expand All @@ -54,6 +61,8 @@ describe('validate()', () => {
it('$G validate() should return true', () => expect($G.validate()).to.be.true);
it('$H validate() should return true', () => expect($H.validate()).to.be.true);
it('$I validate() should return true', () => expect($I.validate()).to.be.true);
it('$L validate() should return false', () => expect($L.validate()).to.be.false);
it('$M validate() should return true', () => expect($M.validate()).to.be.true);
});

describe('fieldKeys()', () => {
Expand Down

0 comments on commit c9f5b30

Please sign in to comment.