Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fallback to default attribute values when invalid values are used. #387

Merged
merged 2 commits into from

3 participants

@ericf
Owner

When an ATTRS setter returns Y.Attribute.INVALID_VALUE during initialization, and a default attribute value is defined, fallback to that value.

Fixes #2528732
Trumps #48

@ericf ericf Fallback to default attribute values when invalid values are used.
When an `ATTRS` setter returns `Y.Attribute.INVALID_VALUE` during
initialization, and a default attribute value is defined, fallback to
that value.

Fixes #2528732
ded76c0
@ericf ericf referenced this pull request
Closed

Fix for ticket# 2528732 #48

@sdesai

We should add a test with a failing validator and make sure the fix does the same thing for failed validation.

Owner

@sdesai will do. The code for the validator path already did this, but I'll add an explicit test for completeness.

Sounds good. I must have blacked out when writing the fallback for the validator, because I think I put the same feedback in the original request.

@tivac

yessssssssssssssssssssssssssssssssssssss

:+1: (I know this isn't a voting thing, I'm just excited)

@ericf ericf merged commit 9ac46fb into yui:dev-master
@ericf ericf deleted the ericf:attr-init-val branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 3, 2013
  1. @ericf

    Fallback to default attribute values when invalid values are used.

    ericf authored
    When an `ATTRS` setter returns `Y.Attribute.INVALID_VALUE` during
    initialization, and a default attribute value is defined, fallback to
    that value.
    
    Fixes #2528732
  2. @ericf
This page is out of date. Refresh to see the latest.
View
7 src/attribute/HISTORY.md
@@ -1,6 +1,13 @@
Attribute Change History
========================
+@VERSION@
+-----
+
+* Invalid values supplied during Attribute initialization that fail setter
+ validation will now fallback the default value defined in `ATTRS`.
+ [Ticket #2528732] [redbat]
+
3.8.0
-----
View
9 src/attribute/js/AttributeCore.js
@@ -630,8 +630,13 @@
retVal = setter.call(host, newVal, name);
if (retVal === INVALID_VALUE) {
- Y.log('Attribute: ' + attrName + ', setter returned Attribute.INVALID_VALUE for value:' + newVal, 'warn', 'attribute');
- allowSet = false;
+ if (initializing) {
+ Y.log('Attribute: ' + attrName + ', setter returned Attribute.INVALID_VALUE for value:' + newVal + ', initializing to default value', 'warn', 'attribute');
+ newVal = cfg.defaultValue;
+ } else {
+ Y.log('Attribute: ' + attrName + ', setter returned Attribute.INVALID_VALUE for value:' + newVal, 'warn', 'attribute');
+ allowSet = false;
+ }
} else if (retVal !== undefined){
Y.log('Attribute: ' + attrName + ', raw value: ' + newVal + ' modified by setter to:' + retVal, 'info', 'attribute');
newVal = retVal;
View
75 src/attribute/tests/unit/assets/attribute-core-tests.js
@@ -95,7 +95,7 @@ YUI.add('attribute-core-tests', function(Y) {
function FooBar(userVals) {
Y.Attribute.call(this, null, userVals);
- };
+ }
FooBar.ATTRS = {
foo:{
@@ -369,6 +369,79 @@ YUI.add('attribute-core-tests', function(Y) {
Y.Assert.areEqual(undefined, h.get("complex.Y.A"));
},
+ testDefaultSet: function() {
+ function FooBar(userVals) {
+ Y.Attribute.call(this, null, userVals);
+ }
+
+ FooBar.ATTRS = {
+ foo: {
+ value: 'foo',
+
+ setter: function (v) {
+ if (v !== 'A' && v !== 'B') {
+ return Y.Attribute.INVALID_VALUE;
+ }
+
+ return v;
+ }
+ },
+
+ bar: {
+ value: 'bar',
+
+ validator: function (v) {
+ return (v === 'A' || v === 'B');
+ }
+ }
+ };
+
+ // Straightup augment, no wrapper functions
+ Y.mix(FooBar, Y.Attribute, false, null, 1);
+
+ var h = new FooBar({
+ foo: 'zee',
+ bar: 'zee'
+ });
+
+ Y.Assert.areNotSame(undefined, h.get('foo'));
+ Y.Assert.areSame('foo', h.get('foo'));
+ Y.Assert.areNotSame(undefined, h.get('bar'));
+ Y.Assert.areSame('bar', h.get('bar'));
+
+ h.set('foo', 'invalid again');
+ h.set('bar', 'invalid again');
+ Y.Assert.areSame('foo', h.get('foo'));
+ Y.Assert.areSame('bar', h.get('bar'));
+
+ h.set('foo', 'A');
+ h.set('bar', 'A');
+ Y.Assert.areSame('A', h.get('foo'));
+ Y.Assert.areSame('A', h.get('bar'));
+
+ h = new FooBar({
+ foo: 'B',
+ bar: 'B'
+ });
+
+ Y.Assert.areNotSame(undefined, h.get('foo'));
+ Y.Assert.areNotSame('foo', h.get('foo'));
+ Y.Assert.areSame('B', h.get('foo'));
+ Y.Assert.areNotSame(undefined, h.get('bar'));
+ Y.Assert.areNotSame('bar', h.get('bar'));
+ Y.Assert.areSame('B', h.get('bar'));
+
+ h.set('foo', 'invalid');
+ h.set('bar', 'invalid');
+ Y.Assert.areSame('B', h.get('foo'));
+ Y.Assert.areSame('B', h.get('bar'));
+
+ h.set('foo', 'A');
+ h.set('bar', 'A');
+ Y.Assert.areSame('A', h.get('foo'));
+ Y.Assert.areSame('A', h.get('bar'));
+ },
+
testInitialValidation: function() {
var h = this.createHost({A:5});
Y.Assert.areEqual("AVal", h.get("A")); // Numerical value validation failure should revert to default value
Something went wrong with that request. Please try again.