Skip to content

Commit 447d2c6

Browse files
Fixed test reuse bug and removed the need for Math.sqrt constant
1 parent 2f3382b commit 447d2c6

File tree

7 files changed

+128
-124
lines changed

7 files changed

+128
-124
lines changed

fieldval.js

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,6 @@ var FieldVal = (function(){
464464
* to prevent multiple instances of FieldVal (due to being
465465
* a dependency) having not-strictly-equal constants. */
466466
FieldVal.ASYNC = -1;//Used to indicate async functions
467-
FieldVal.REQUIRED_ERROR = Math.sqrt;
468467
FieldVal.NOT_REQUIRED_BUT_MISSING = Math.floor;
469468

470469
FieldVal.FIELD_MISSING = 1;
@@ -606,41 +605,19 @@ var FieldVal = (function(){
606605
}
607606
shared_options.had_error = true;
608607

609-
if (response === FieldVal.REQUIRED_ERROR) {
610-
611-
if (shared_options.field_name!==undefined) {
612-
shared_options.validator.error(shared_options.field_name, FieldVal.create_error(FieldVal.MISSING_ERROR, flags));
613-
use_check_done();
614-
return;
615-
} else {
616-
if (shared_options.existing_validator) {
617-
618-
shared_options.validator.error(
619-
FieldVal.create_error(FieldVal.MISSING_ERROR, flags)
620-
);
621-
use_check_done();
622-
return;
623-
} else {
624-
shared_options.return_missing = true;
625-
use_check_done();
626-
return;
627-
}
628-
}
629-
} else if (response === FieldVal.NOT_REQUIRED_BUT_MISSING) {
608+
if (response === FieldVal.NOT_REQUIRED_BUT_MISSING) {
630609
//NOT_REQUIRED_BUT_MISSING means "don't process proceeding checks, but don't throw an error"
631610
use_check_done();
632611
} else {
633612

634-
if (shared_options.existing_validator) {
635-
if (shared_options.field_name!==undefined) {
636-
shared_options.validator.invalid(shared_options.field_name, response);
637-
} else {
638-
shared_options.validator.error(response);
639-
}
613+
if (shared_options.field_name!==undefined) {
614+
shared_options.validator.error(shared_options.field_name, response);
640615
use_check_done();
616+
return;
641617
} else {
642618
shared_options.validator.error(response);
643619
use_check_done();
620+
return;
644621
}
645622
}
646623
} else {
@@ -691,7 +668,6 @@ var FieldVal = (function(){
691668
},
692669
options: options,
693670
stop: false,
694-
return_missing: false,
695671
had_error: false
696672
};
697673

@@ -724,12 +700,6 @@ var FieldVal = (function(){
724700
}
725701
}
726702

727-
if (shared_options.return_missing) {
728-
finish(FieldVal.REQUIRED_ERROR);
729-
shared_options.validator.async_call_ended();
730-
return;
731-
}
732-
733703
if(!shared_options.existing_validator){
734704
finish(shared_options.validator.end());
735705
shared_options.validator.async_call_ended();
@@ -753,21 +723,30 @@ var FieldVal = (function(){
753723
}
754724
};
755725

726+
FieldVal.merge_flags_and_checks = function(flags, check){
727+
var new_flags = {};
728+
if(flags){
729+
for(var i in flags){
730+
if(flags.hasOwnProperty(i)){
731+
new_flags[i] = flags[i];
732+
}
733+
}
734+
}
735+
new_flags.check = check;
736+
return new_flags;
737+
}
738+
756739
FieldVal.required = function (required, flags) {//required defaults to true
757740
var check = function (value) {
758741
if (value === null || value === undefined) {
759742
if (required || required === undefined) {
760-
return FieldVal.REQUIRED_ERROR;
743+
return FieldVal.create_error(FieldVal.MISSING_ERROR, flags);
761744
}
762745

763746
return FieldVal.NOT_REQUIRED_BUT_MISSING;
764747
}
765748
};
766-
if (flags !== undefined) {
767-
flags.check = check;
768-
return flags;
769-
}
770-
return check;
749+
return FieldVal.merge_flags_and_checks(flags,check);
771750
};
772751

773752
FieldVal.type = function (desired_type, flags) {
@@ -776,7 +755,7 @@ var FieldVal = (function(){
776755

777756
var check = function (value, emit) {
778757

779-
var required_error = FieldVal.required(required)(value);
758+
var required_error = FieldVal.required(required, flags || {}).check(value);
780759

781760
if (required_error) {
782761
return required_error;
@@ -796,12 +775,7 @@ var FieldVal = (function(){
796775
}
797776
};
798777

799-
if (flags !== undefined) {
800-
flags.check = check;
801-
return flags;
802-
}
803-
804-
return check;
778+
return FieldVal.merge_flags_and_checks(flags,check);
805779
};
806780

807781
FieldVal.create_error = function (default_error, flags) {
@@ -1410,7 +1384,7 @@ var FieldVal = (function(){
14101384
}
14111385
if (value.length === 0) {
14121386
if(required || required===undefined){
1413-
return FieldVal.REQUIRED_ERROR;
1387+
return FieldVal.create_error(FieldVal.MISSING_ERROR, flags);
14141388
} else {
14151389
return FieldVal.NOT_REQUIRED_BUT_MISSING;
14161390
}
@@ -1697,9 +1671,7 @@ var FieldVal = (function(){
16971671
if(res===FieldVal.ASYNC){
16981672
throw new Error(".each used with async checks, use .each_async.");
16991673
}
1700-
if (res === FieldVal.REQUIRED_ERROR){
1701-
validator.missing("" + i);
1702-
} else if (res) {
1674+
if (res) {
17031675
validator.invalid("" + i, res);
17041676
}
17051677
};

fieldval.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fieldval",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"description": "fieldval",
55
"main": "fieldval.js",
66
"scripts": {

src/BasicVal.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ var BasicVal = (function(){
209209
}
210210
if (value.length === 0) {
211211
if(required || required===undefined){
212-
return FieldVal.REQUIRED_ERROR;
212+
return FieldVal.create_error(FieldVal.MISSING_ERROR, flags);
213213
} else {
214214
return FieldVal.NOT_REQUIRED_BUT_MISSING;
215215
}
@@ -496,9 +496,7 @@ var BasicVal = (function(){
496496
if(res===FieldVal.ASYNC){
497497
throw new Error(".each used with async checks, use .each_async.");
498498
}
499-
if (res === FieldVal.REQUIRED_ERROR){
500-
validator.missing("" + i);
501-
} else if (res) {
499+
if (res) {
502500
validator.invalid("" + i, res);
503501
}
504502
};

src/FieldVal.js

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,6 @@ var FieldVal = (function(){
464464
* to prevent multiple instances of FieldVal (due to being
465465
* a dependency) having not-strictly-equal constants. */
466466
FieldVal.ASYNC = -1;//Used to indicate async functions
467-
FieldVal.REQUIRED_ERROR = Math.sqrt;
468467
FieldVal.NOT_REQUIRED_BUT_MISSING = Math.floor;
469468

470469
FieldVal.FIELD_MISSING = 1;
@@ -606,41 +605,19 @@ var FieldVal = (function(){
606605
}
607606
shared_options.had_error = true;
608607

609-
if (response === FieldVal.REQUIRED_ERROR) {
610-
611-
if (shared_options.field_name!==undefined) {
612-
shared_options.validator.error(shared_options.field_name, FieldVal.create_error(FieldVal.MISSING_ERROR, flags));
613-
use_check_done();
614-
return;
615-
} else {
616-
if (shared_options.existing_validator) {
617-
618-
shared_options.validator.error(
619-
FieldVal.create_error(FieldVal.MISSING_ERROR, flags)
620-
);
621-
use_check_done();
622-
return;
623-
} else {
624-
shared_options.return_missing = true;
625-
use_check_done();
626-
return;
627-
}
628-
}
629-
} else if (response === FieldVal.NOT_REQUIRED_BUT_MISSING) {
608+
if (response === FieldVal.NOT_REQUIRED_BUT_MISSING) {
630609
//NOT_REQUIRED_BUT_MISSING means "don't process proceeding checks, but don't throw an error"
631610
use_check_done();
632611
} else {
633612

634-
if (shared_options.existing_validator) {
635-
if (shared_options.field_name!==undefined) {
636-
shared_options.validator.invalid(shared_options.field_name, response);
637-
} else {
638-
shared_options.validator.error(response);
639-
}
613+
if (shared_options.field_name!==undefined) {
614+
shared_options.validator.error(shared_options.field_name, response);
640615
use_check_done();
616+
return;
641617
} else {
642618
shared_options.validator.error(response);
643619
use_check_done();
620+
return;
644621
}
645622
}
646623
} else {
@@ -691,7 +668,6 @@ var FieldVal = (function(){
691668
},
692669
options: options,
693670
stop: false,
694-
return_missing: false,
695671
had_error: false
696672
};
697673

@@ -724,12 +700,6 @@ var FieldVal = (function(){
724700
}
725701
}
726702

727-
if (shared_options.return_missing) {
728-
finish(FieldVal.REQUIRED_ERROR);
729-
shared_options.validator.async_call_ended();
730-
return;
731-
}
732-
733703
if(!shared_options.existing_validator){
734704
finish(shared_options.validator.end());
735705
shared_options.validator.async_call_ended();
@@ -753,21 +723,30 @@ var FieldVal = (function(){
753723
}
754724
};
755725

726+
FieldVal.merge_flags_and_checks = function(flags, check){
727+
var new_flags = {};
728+
if(flags){
729+
for(var i in flags){
730+
if(flags.hasOwnProperty(i)){
731+
new_flags[i] = flags[i];
732+
}
733+
}
734+
}
735+
new_flags.check = check;
736+
return new_flags;
737+
}
738+
756739
FieldVal.required = function (required, flags) {//required defaults to true
757740
var check = function (value) {
758741
if (value === null || value === undefined) {
759742
if (required || required === undefined) {
760-
return FieldVal.REQUIRED_ERROR;
743+
return FieldVal.create_error(FieldVal.MISSING_ERROR, flags);
761744
}
762745

763746
return FieldVal.NOT_REQUIRED_BUT_MISSING;
764747
}
765748
};
766-
if (flags !== undefined) {
767-
flags.check = check;
768-
return flags;
769-
}
770-
return check;
749+
return FieldVal.merge_flags_and_checks(flags,check);
771750
};
772751

773752
FieldVal.type = function (desired_type, flags) {
@@ -776,7 +755,7 @@ var FieldVal = (function(){
776755

777756
var check = function (value, emit) {
778757

779-
var required_error = FieldVal.required(required)(value);
758+
var required_error = FieldVal.required(required, flags || {}).check(value);
780759

781760
if (required_error) {
782761
return required_error;
@@ -796,12 +775,7 @@ var FieldVal = (function(){
796775
}
797776
};
798777

799-
if (flags !== undefined) {
800-
flags.check = check;
801-
return flags;
802-
}
803-
804-
return check;
778+
return FieldVal.merge_flags_and_checks(flags,check);
805779
};
806780

807781
FieldVal.create_error = function (default_error, flags) {

0 commit comments

Comments
 (0)