Skip to content

Commit

Permalink
use default prop value if given value is empty string (fixes aframevr…
Browse files Browse the repository at this point in the history
  • Loading branch information
ngokevin committed Nov 8, 2016
1 parent 8011746 commit c863464
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/core/schema.js
Expand Up @@ -119,8 +119,12 @@ module.exports.parseProperties = function (propData, schema, getPartialData, com
* Deserialize a single property.
*/
function parseProperty (value, propDefinition) {
value = (value === undefined || value === null) ? propDefinition.default : value;
return propDefinition.parse(value);
// Use default value if value is falsy.
if (value === undefined || value === null || value === '') {
value = propDefinition.default;
}
// Invoke property type parser.
return propDefinition.parse(value, propDefinition.default);
}
module.exports.parseProperty = parseProperty;

Expand Down
15 changes: 13 additions & 2 deletions tests/core/propertyTypes.test.js
Expand Up @@ -5,7 +5,7 @@ var propertyTypes = PropertyTypes.propertyTypes;
var register = PropertyTypes.registerPropertyType;

suite('propertyTypes', function () {
suite('assetParse', function () {
suite('asset', function () {
var parse = propertyTypes.asset.parse;

setup(function () {
Expand Down Expand Up @@ -55,6 +55,17 @@ suite('propertyTypes', function () {
});
});

suite('int', function () {
var parse = propertyTypes.int.parse;

test('parses int', function () {
assert.equal(parse('5'), 5);
assert.equal(parse(5), 5);
assert.equal(parse('4.5'), 4);
assert.equal(parse(4.5), 4);
});
});

suite('selector', function () {
var parse = propertyTypes.selector.parse;
var stringify = propertyTypes.selector.stringify;
Expand Down Expand Up @@ -144,7 +155,7 @@ suite('propertyTypes', function () {
});
});

suite('srcParse', function () {
suite('src', function () {
var parse = propertyTypes.src.parse;

setup(function () {
Expand Down
8 changes: 8 additions & 0 deletions tests/core/schema.test.js
Expand Up @@ -51,6 +51,14 @@ suite('schema', function () {
assert.shallowDeepEqual(parsed, {x: 1, y: 2, z: 3});
});

test('uses default value if value is falsy', function () {
var schemaPropDef = processSchema({type: 'int', default: 2});
assert.equal(parseProperty(null, schemaPropDef), 2);
assert.equal(parseProperty('', schemaPropDef), 2);
assert.equal(parseProperty(undefined, schemaPropDef), 2);
assert.equal(parseProperty(0, schemaPropDef), 0);
});

test('can parse using inline parse', function () {
var schemaPropDef = {
default: 'xyz',
Expand Down

0 comments on commit c863464

Please sign in to comment.