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

adds support for equality operator #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions can-validate/map/validate/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,17 @@ can.extend(can.Map.prototype, {
*
* @param {object} item A key/value object
* @param {object} opts Object that contains validation config.
* @param {object} otherItems Object that contains other attributes in the map
* @return {boolean} True if method found that the property can be saved; if
* validation fails and the property must validate (`mustValidate` property),
* this will be `false`.
*/
_validateOne: function (item, opts) {
_validateOne: function (item, opts, otherItems) {
var errors;
var allowSet = true;

// run validation
errors = can.validate.once(item.value, can.extend({}, opts), item.key);
errors = can.validate.once(item.value, can.extend({}, opts), item.key, otherItems);

// Process errors if we got them
if (errors && errors.length > 0) {
Expand Down Expand Up @@ -303,7 +304,7 @@ can.extend(can.Map.prototype, {
can.each(computes, function (item) {
item.compute.bind('change', function () {
itemObj.value = self.attr(itemObj.key);
self._validateOne(itemObj, processedObj);
self._validateOne(itemObj, processedObj, self.attr());
});
});

Expand All @@ -325,7 +326,7 @@ proto.__set = function (prop, value, current, success, error) {
// If validate opts are set and initing, validate properties only if validateOnInit is true
if ((validateOpts && !mapIniting) || (validateOpts && mapIniting && validateOpts.validateOnInit)) {
// Validate item
allowSet = this._validateOne({key: prop, value: value}, validateOpts);
allowSet = this._validateOne({key: prop, value: value}, validateOpts, this.attr());
Copy link
Contributor

@Macrofig Macrofig Jul 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably a better idea to serialize the map instead of using attr, what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm... on second thought, maybe attr is better. What would happen if you wanted to check equality against a property with a getter?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tested and it looks like .attr() doesn't retrieve the computed value from the getter (although it maybe should). For this to work, we'd need to look inside validateOpts, extract the property we need and .attr(prop) at this point. Not sure that's a great solution, though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm. Bummer. I haven't pulled this down to try it out yet. I'll try it out tomorrow if that's ok.

}
}

Expand Down
3 changes: 2 additions & 1 deletion can-validate/shims/validatejs.shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var Shim = can.Construct.extend({
* @return {undefined|array} Returns undefined if no errors, otherwise returns
* a list of errors.
*/
once: function (value, options, name) {
once: function (value, options, name, otherItems) {
var errors = [];
var opts = [];
var validationOpts = [];
Expand All @@ -60,6 +60,7 @@ var Shim = can.Construct.extend({
if (name) {
// Since name exists, use the main validate method but just pass one
// property to it. Need to structure the objects it expects first.
opts = can.extend({}, otherItems);
opts[name] = value;
validationOpts[name] = processOptions(options);

Expand Down