Skip to content
Permalink
Browse files

Fixes for booleans; Cast to strings; Empty string casts to `false`

  • Loading branch information
Ajaxy committed Aug 14, 2019
1 parent 22a0fa5 commit 11ec40903568f3595eeaabe36720ab15876ea3cc
Showing with 35 additions and 3 deletions.
  1. +3 −1 lib/index.js
  2. +32 −2 tests/all.test.js
@@ -23,14 +23,16 @@ function castProp(value, propertyDef, options = {}) {
const realDef = findNotNullDef(propertyDef);

switch (resolveType(realDef)) {
case 'string':
return String(value);
case 'integer':
const int = parseInt(value, 10);
return options.allowNaN ? int : (int || 0);
case 'number':
const float = parseFloat(value);
return options.allowNaN ? float : (float || 0);
case 'boolean':
return value !== 'false';
return value === 'false' ? false : Boolean(value);
case 'array':
return Array.isArray(value) ? value.map(member => castProp(member, realDef.items, options)) : undefined;
case 'object':
@@ -41,7 +41,13 @@ describe('All', () => {
w: '',
x: '',
y: '',
z: '', // `true` with boolean
z: '', // `false` with boolean
aa: true,
ab: false,
ac: 777,
ad: true,
ae: false,
af: 777,
}, {
type: 'object',
properties: {
@@ -184,6 +190,24 @@ describe('All', () => {
z: {
type: 'boolean',
},
aa: {
type: 'boolean',
},
ab: {
type: 'boolean',
},
ac: {
type: 'boolean',
},
ad: {
type: 'string',
},
ae: {
type: 'string',
},
af: {
type: 'string',
},
},
})).toEqual({
a: 'value',
@@ -224,7 +248,13 @@ describe('All', () => {
w: '',
x: 0,
y: 0,
z: true,
z: false,
aa: true,
ab: false,
ac: true,
ad: 'true',
ae: 'false',
af: '777',
});
});
});

0 comments on commit 11ec409

Please sign in to comment.
You can’t perform that action at this time.