Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #24

Merged
merged 3 commits into from
Jul 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fieldval",
"version": "0.4.3",
"version": "0.5.0",
"main": "fieldval.js",
"ignore": [
"coverage",
Expand All @@ -16,4 +16,4 @@
"link.sh",
".gitignore"
]
}
}
45 changes: 27 additions & 18 deletions fieldval.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var FieldVal = (function(){
return true;
};

function FieldVal(validating, existing_error) {
function FieldVal(validating, options) {
var fv = this;

fv.async_waiting = 0;
Expand All @@ -28,8 +28,12 @@ var FieldVal = (function(){
//Top level errors - added using .error()
fv.errors = [];

fv.options = options || {};
fv.ignore_unrecognized = fv.options.ignore_unrecognized;

var existing_error = fv.options.error;
if(existing_error!==undefined){
//Provided a (potentially undefined) existing error
//Provided a (potentially null) existing error

if(existing_error){
//Is not null
Expand Down Expand Up @@ -75,6 +79,7 @@ var FieldVal = (function(){

}
} else {
//The existing_error is null, which means a previous validator recognized all fields
for(var n in validating){
if(validating.hasOwnProperty(n)) {
fv.recognized_keys[n] = true;
Expand Down Expand Up @@ -115,7 +120,9 @@ var FieldVal = (function(){
}
}
}
return new FieldVal(current_value,current_error);
return new FieldVal(current_value,{
"error": current_error
});
};

FieldVal.get_error = function(){
Expand Down Expand Up @@ -381,24 +388,26 @@ var FieldVal = (function(){
}
}

var auto_unrecognized = fv.get_unrecognized();
var i, auto_key;
for (i = 0; i < auto_unrecognized.length; i++) {
auto_key = auto_unrecognized[i];
returning_invalid[auto_key] = {
error_message: "Unrecognized field.",
error: FieldVal.FIELD_UNRECOGNIZED
};
has_error = true;
}
if(!fv.ignore_unrecognized){
var auto_unrecognized = fv.get_unrecognized();
var i, auto_key;
for (i = 0; i < auto_unrecognized.length; i++) {
auto_key = auto_unrecognized[i];
returning_invalid[auto_key] = {
error_message: "Unrecognized field.",
error: FieldVal.FIELD_UNRECOGNIZED
};
has_error = true;
}

if (!is_empty(fv.invalid_keys)) {
for(var k in fv.invalid_keys){
if(fv.invalid_keys.hasOwnProperty(k)){
returning_invalid[k] = fv.invalid_keys[k];
if (!is_empty(fv.invalid_keys)) {
for(var k in fv.invalid_keys){
if(fv.invalid_keys.hasOwnProperty(k)){
returning_invalid[k] = fv.invalid_keys[k];
}
}
has_error = true;
}
has_error = true;
}


Expand Down
2 changes: 1 addition & 1 deletion fieldval.min.js

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fieldval",
"version": "0.4.3",
"version": "0.5.0",
"description": "fieldval",
"main": "fieldval.js",
"scripts": {
Expand All @@ -11,17 +11,17 @@
"url": "https://github.com/FieldVal/fieldval-js.git"
},
"devDependencies": {
"mocha": "*",
"gulp": "^3.6.1",
"gulp-concat": "^2.2.0",
"mocha": "^2.2.5",
"gulp": "^3.8.11",
"gulp-concat": "^2.5.2",
"gulp-imports" : "MarcusLongmuir/gulp-imports",
"gulp-istanbul": "^0.6.0",
"gulp-jshint": "^1.8.4",
"gulp-mocha": "^1.0.0",
"gulp-istanbul": "^0.9.0",
"gulp-jshint": "^1.11.0",
"gulp-mocha": "^2.1.0",
"gulp-nodemon": "*",
"gulp-uglify": "^0.2.1",
"gulp-util": "^2.2.14",
"gulp-watch": "^0.6.2",
"gulp-uglify": "^1.2.0",
"gulp-util": "^3.0.4",
"gulp-watch": "^4.2.4",
"sa-docs-to-json": "SuperAwesomeLtd/sa-docs-to-json"
}
}
}
45 changes: 27 additions & 18 deletions src/FieldVal.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var FieldVal = (function(){
return true;
};

function FieldVal(validating, existing_error) {
function FieldVal(validating, options) {
var fv = this;

fv.async_waiting = 0;
Expand All @@ -28,8 +28,12 @@ var FieldVal = (function(){
//Top level errors - added using .error()
fv.errors = [];

fv.options = options || {};
fv.ignore_unrecognized = fv.options.ignore_unrecognized;

var existing_error = fv.options.error;
if(existing_error!==undefined){
//Provided a (potentially undefined) existing error
//Provided a (potentially null) existing error

if(existing_error){
//Is not null
Expand Down Expand Up @@ -75,6 +79,7 @@ var FieldVal = (function(){

}
} else {
//The existing_error is null, which means a previous validator recognized all fields
for(var n in validating){
if(validating.hasOwnProperty(n)) {
fv.recognized_keys[n] = true;
Expand Down Expand Up @@ -115,7 +120,9 @@ var FieldVal = (function(){
}
}
}
return new FieldVal(current_value,current_error);
return new FieldVal(current_value,{
"error": current_error
});
};

FieldVal.get_error = function(){
Expand Down Expand Up @@ -381,24 +388,26 @@ var FieldVal = (function(){
}
}

var auto_unrecognized = fv.get_unrecognized();
var i, auto_key;
for (i = 0; i < auto_unrecognized.length; i++) {
auto_key = auto_unrecognized[i];
returning_invalid[auto_key] = {
error_message: "Unrecognized field.",
error: FieldVal.FIELD_UNRECOGNIZED
};
has_error = true;
}
if(!fv.ignore_unrecognized){
var auto_unrecognized = fv.get_unrecognized();
var i, auto_key;
for (i = 0; i < auto_unrecognized.length; i++) {
auto_key = auto_unrecognized[i];
returning_invalid[auto_key] = {
error_message: "Unrecognized field.",
error: FieldVal.FIELD_UNRECOGNIZED
};
has_error = true;
}

if (!is_empty(fv.invalid_keys)) {
for(var k in fv.invalid_keys){
if(fv.invalid_keys.hasOwnProperty(k)){
returning_invalid[k] = fv.invalid_keys[k];
if (!is_empty(fv.invalid_keys)) {
for(var k in fv.invalid_keys){
if(fv.invalid_keys.hasOwnProperty(k)){
returning_invalid[k] = fv.invalid_keys[k];
}
}
has_error = true;
}
has_error = true;
}


Expand Down
45 changes: 40 additions & 5 deletions test/FieldVal_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@ describe('FieldVal', function() {
}
})

it('should respect the ignore_unrecognized option', function() {
var validator_one = new FieldVal({
'my_unrecognized_key': 42
},{
ignore_unrecognized: true
});
assert.strictEqual(validator_one.end(),null);

var validator_two = new FieldVal({
'my_unrecognized_key': 42
});
assert.deepEqual(validator_two.end(),{
"invalid":{
"my_unrecognized_key":{
"error_message": "Unrecognized field.",
"error": 3
}
},
"error_message": 'One or more errors.',
"error": 5
});
})

it('should be able to continue from a previous validator that had errors', function(done) {
var my_data = {
'valid_one_key': "AB",
Expand Down Expand Up @@ -92,14 +115,19 @@ describe('FieldVal', function() {

validator_one.end(function(error_one){

var validator_two = new FieldVal(my_data, error_one);
var validator_two = new FieldVal(my_data, {
"error": error_one
});

//Make this key invalid on validator_two
validator_two.get('valid_one_key', BasicVal.string(true), BasicVal.prefix("ABC"))
validator_two.get("my_initially_unrecognized_key", BasicVal.boolean(true));

validator_two.get('my_object', BasicVal.object(true), function(val){
var inner_error_one = FieldVal.get_error("my_object", error_one);
var inner_validator_two = new FieldVal(val, inner_error_one);
var inner_validator_two = new FieldVal(val, {
"error": inner_error_one
});
inner_validator_two.get("inner_valid_one_key", BasicVal.string(true), BasicVal.prefix("DEF"));
inner_validator_two.get("inner_my_initially_unrecognized_key", BasicVal.boolean(true));
return inner_validator_two.end();
Expand Down Expand Up @@ -173,7 +201,9 @@ describe('FieldVal', function() {

validator_one.end(function(error_one){

var validator_two = new FieldVal(my_data, error_one);
var validator_two = new FieldVal(my_data, {
"error": error_one
});
var error_two = validator_two.end();

assert.deepEqual(error_two, null);
Expand Down Expand Up @@ -1286,7 +1316,10 @@ describe('FieldVal', function() {
it('return a new FieldVal instance if dug into an existing error if the key exists', function() {

var validator = new FieldVal(
get_example_data(), get_example_error()
get_example_data(),
{
"error": get_example_error()
}
);

var dug = validator.dig('first_inner','second_inner');
Expand Down Expand Up @@ -1372,7 +1405,9 @@ describe('FieldVal', function() {
it('should return no error if constructed with null existing error', function() {
var validator = new FieldVal({
my_field: 123
},null);
},{
"error": null
});
assert.strictEqual(null, validator.end());
})

Expand Down