Skip to content

Commit

Permalink
Merge pull request #40 from canjs/update-dependencies
Browse files Browse the repository at this point in the history
Update dependencies
  • Loading branch information
m-mujica authored Dec 21, 2017
2 parents 41d403b + 1a07bad commit 9998e59
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 209 deletions.
23 changes: 23 additions & 0 deletions .jshintrc
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
}
}
141 changes: 73 additions & 68 deletions can-define-validate-validatejs.js
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;
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"homepage": "https://github.com/canjs/can-define-validate-validatejs",
"devDependencies": {
"bit-docs": "^0.0.8",
"can-compute": "^3.3.4",
"can-compute": "^4.0.0-pre.6",
"detect-cyclic-packages": "^1.1.0",
"http-server": "^0.10.0",
"jshint": "^2.9.5",
Expand All @@ -54,14 +54,14 @@
"testee",
"steal-tools"
],
"npmAlgorithm": "flat",
"npmDependencies": [
"steal-qunit"
],
"transpiler": "babel"
]
},
"dependencies": {
"can-define": "^1.4.7",
"can-assign": "^1.1.1",
"can-define": "^2.0.0-pre.21",
"can-reflect": "^1.11.1",
"can-util": "^3.9.0",
"can-validate": "^1.1.0",
"can-validate-validatejs": "^0.1.0"
Expand Down
Loading

0 comments on commit 9998e59

Please sign in to comment.