-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from canjs/update-dependencies
Update dependencies
- Loading branch information
Showing
4 changed files
with
267 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"maxerr": 50, | ||
|
||
"curly": true, | ||
"eqeqeq": true, | ||
"freeze": true, | ||
"latedef": true, | ||
"noarg": true, | ||
"undef": true, | ||
"unused": true, | ||
"maxdepth": 4, | ||
"boss": true, | ||
"eqnull": true, | ||
"evil": true, | ||
"loopfunc": true, | ||
"browser": true, | ||
"node": true, | ||
"esnext": true, | ||
|
||
"globals": { | ||
"QUnit": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,93 @@ | ||
var formatErrors = require("can-validate").formatErrors; | ||
var validate = require("can-validate-validatejs"); | ||
var define = require("can-define"); | ||
var each = require("can-util/js/each/each"); | ||
var assign = require("can-util/js/assign/assign"); | ||
var assign = require("can-assign"); | ||
var canReflect = require("can-reflect"); | ||
var formatErrors = require("can-validate").formatErrors; | ||
var isEmptyObject = require("can-util/js/is-empty-object/is-empty-object"); | ||
|
||
var getMapConstraints = function (Map) { | ||
var constraints = {}; | ||
each(Map.prototype._define.definitions, function (prop, key) { | ||
if (prop.validate && !isEmptyObject(prop.validate)) { | ||
constraints[key] = prop.validate; | ||
} | ||
}); | ||
return constraints; | ||
var getMapConstraints = function(Map) { | ||
var constraints = {}; | ||
canReflect.eachKey(Map.prototype._define.definitions, function(prop, key) { | ||
if (prop.validate && !isEmptyObject(prop.validate)) { | ||
constraints[key] = prop.validate; | ||
} | ||
}); | ||
return constraints; | ||
}; | ||
|
||
var validateMap = function (Map, validator) { | ||
var mapDefinition = Map.prototype._define; | ||
|
||
Map.prototype.testSet = function() { | ||
var values = {}; | ||
var useNewObject = false; | ||
if (arguments.length) { | ||
// Check if testing many values or just one | ||
if (typeof arguments[0] === 'object' && Boolean(arguments[0])) { | ||
values = arguments[0]; | ||
useNewObject = Boolean(arguments[1]); | ||
} | ||
var validateMap = function(Map, validator) { | ||
var mapDefinition = Map.prototype._define; | ||
|
||
// Check if testing single value | ||
if (typeof arguments[0] === 'string') { | ||
values[arguments[0]] = arguments[1]; | ||
} | ||
Map.prototype.testSet = function() { | ||
var values = {}; | ||
var useNewObject = false; | ||
if (arguments.length) { | ||
// Check if testing many values or just one | ||
if (typeof arguments[0] === "object" && Boolean(arguments[0])) { | ||
values = arguments[0]; | ||
useNewObject = Boolean(arguments[1]); | ||
} | ||
|
||
// Merge values with existing map or with a new map | ||
if (useNewObject) { | ||
values = new Map(values); | ||
} else { | ||
var mapClone = this.serialize(); | ||
assign(mapClone, values); | ||
values = mapClone; | ||
} | ||
return validator(values); | ||
} else { | ||
return this.errors(); | ||
} | ||
// Check if testing single value | ||
if (typeof arguments[0] === "string") { | ||
values[arguments[0]] = arguments[1]; | ||
} | ||
|
||
}; | ||
// Merge values with existing map or with a new map | ||
if (useNewObject) { | ||
values = new Map(values); | ||
} else { | ||
var mapClone = this.serialize(); | ||
assign(mapClone, values); | ||
values = mapClone; | ||
} | ||
return validator(values); | ||
} else { | ||
return this.errors(); | ||
} | ||
}; | ||
|
||
Map.prototype.errors = function() { | ||
var _errors = this._errors; | ||
var errors; | ||
if(arguments.length) { | ||
var errorsObj = formatErrors(_errors, 'errors-object'); | ||
errors = []; | ||
Map.prototype.errors = function() { | ||
var _errors = this._errors; | ||
var errors; | ||
if (arguments.length) { | ||
var errorsObj = formatErrors(_errors, "errors-object"); | ||
errors = []; | ||
|
||
each(arguments, function (key) { | ||
[].push.apply(errors, errorsObj ? errorsObj[key]: []); | ||
}); | ||
errors = errors.length > 0 ? errors : undefined; | ||
} else { | ||
errors = _errors; | ||
} | ||
return errors; | ||
}; | ||
canReflect.eachIndex(arguments, function(key) { | ||
[].push.apply(errors, errorsObj ? errorsObj[key] : []); | ||
}); | ||
errors = errors.length > 0 ? errors : undefined; | ||
} else { | ||
errors = _errors; | ||
} | ||
return errors; | ||
}; | ||
|
||
define.property(Map.prototype, "_errors", { | ||
get: function(){ | ||
var errors = validator(this); | ||
return errors; | ||
} | ||
}, mapDefinition.dataInitializers, mapDefinition.computedInitializers); | ||
define.property( | ||
Map.prototype, | ||
"_errors", | ||
{ | ||
get: function() { | ||
var errors = validator(this); | ||
return errors; | ||
} | ||
}, | ||
mapDefinition.dataInitializers, | ||
mapDefinition.computedInitializers | ||
); | ||
}; | ||
|
||
var decorator = function(Map) { | ||
var constraints = getMapConstraints(Map); | ||
var validator = validate.many(constraints); | ||
var constraints = getMapConstraints(Map); | ||
var validator = validate.many(constraints); | ||
|
||
validateMap(Map, function (map) { | ||
var errors = validator(map); | ||
return formatErrors(errors, 'errors'); | ||
}); | ||
validateMap(Map, function(map) { | ||
var errors = validator(map); | ||
return formatErrors(errors, "errors"); | ||
}); | ||
}; | ||
|
||
decorator.validatejs = validate.validatejs; | ||
|
||
module.exports = decorator; | ||
module.exports = decorator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.