Skip to content

Commit

Permalink
fix: coercion of array to scalar that should fail validation
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Aug 14, 2020
1 parent d4d1a13 commit 73f612f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
46 changes: 18 additions & 28 deletions lib/dot/coerce.def
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,45 @@
, $coerced = 'coerced' + $lvl;
}}
var {{=$dataType}} = typeof {{=$data}};
{{? it.opts.coerceTypes == 'array'}}
if ({{=$dataType}} == 'object' && Array.isArray({{=$data}})) {{=$dataType}} = 'array';
{{?}}

var {{=$coerced}} = undefined;

{{ var $bracesCoercion = ''; }}
{{~ $coerceToTypes:$type:$i }}
{{? $i }}
if ({{=$coerced}} === undefined) {
{{ $bracesCoercion += '}'; }}
{{?}}

{{? it.opts.coerceTypes == 'array' && $type != 'array' }}
if ({{=$dataType}} == 'array' && {{=$data}}.length == 1) {
{{=$coerced}} = {{=$data}} = {{=$data}}[0];
{{=$dataType}} = typeof {{=$data}};
/*if ({{=$dataType}} == 'object' && Array.isArray({{=$data}})) {{=$dataType}} = 'array';*/
}
{{?}}
{{? it.opts.coerceTypes == 'array' }}
if ({{=$dataType}} == 'object' && Array.isArray({{=$data}}) && {{=$data}}.length == 1) {
{{=$data}} = {{=$data}}[0];
{{=$dataType}} = typeof {{=$data}};
if ({{=it.util.checkDataType(it.schema.type, $data, it.opts.strictNumbers)}}) {{=$coerced}} = {{=$data}};
}
{{?}}

if ({{=$coerced}} !== undefined) ;
{{~ $coerceToTypes:$type:$i }}
{{? $type == 'string' }}
if ({{=$dataType}} == 'number' || {{=$dataType}} == 'boolean')
else if ({{=$dataType}} == 'number' || {{=$dataType}} == 'boolean')
{{=$coerced}} = '' + {{=$data}};
else if ({{=$data}} === null) {{=$coerced}} = '';
{{?? $type == 'number' || $type == 'integer' }}
if ({{=$dataType}} == 'boolean' || {{=$data}} === null
else if ({{=$dataType}} == 'boolean' || {{=$data}} === null
|| ({{=$dataType}} == 'string' && {{=$data}} && {{=$data}} == +{{=$data}}
{{? $type == 'integer' }} && !({{=$data}} % 1){{?}}))
{{=$coerced}} = +{{=$data}};
{{?? $type == 'boolean' }}
if ({{=$data}} === 'false' || {{=$data}} === 0 || {{=$data}} === null)
else if ({{=$data}} === 'false' || {{=$data}} === 0 || {{=$data}} === null)
{{=$coerced}} = false;
else if ({{=$data}} === 'true' || {{=$data}} === 1)
{{=$coerced}} = true;
{{?? $type == 'null' }}
if ({{=$data}} === '' || {{=$data}} === 0 || {{=$data}} === false)
else if ({{=$data}} === '' || {{=$data}} === 0 || {{=$data}} === false)
{{=$coerced}} = null;
{{?? it.opts.coerceTypes == 'array' && $type == 'array' }}
if ({{=$dataType}} == 'string' || {{=$dataType}} == 'number' || {{=$dataType}} == 'boolean' || {{=$data}} == null)
else if ({{=$dataType}} == 'string' || {{=$dataType}} == 'number' || {{=$dataType}} == 'boolean' || {{=$data}} == null)
{{=$coerced}} = [{{=$data}}];
{{?}}
{{~}}

{{= $bracesCoercion }}

if ({{=$coerced}} === undefined) {
else {
{{# def.error:'type' }}
} else {
}

if ({{=$coerced}} !== undefined) {
{{# def.setParentData }}
{{=$data}} = {{=$coerced}};
{{? !$dataLvl }}if ({{=$parentData}} !== undefined){{?}}
Expand Down
22 changes: 16 additions & 6 deletions spec/coercion.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ coercionArrayRules.string.array = [
{ from: [123], to: '123' },
{ from: [true], to: 'true'},
{ from: [null], to: ''},
{ from: [{}], to: undefined },
{ from: ['abc', 'def'], to: undefined },
{ from: [], to: undefined }
];
Expand All @@ -154,30 +155,39 @@ coercionArrayRules.number.array = [
{ from: ['1.5'], to: 1.5 },
{ from: [true], to: 1 },
{ from: [null], to: 0 },
// { from: ['abc'], to: undefined },
{ from: ['abc'], to: undefined },
{ from: [{}], to: undefined },
];
coercionArrayRules.integer.array = [
{ from: [1], to: 1 },
{ from: ['1'], to: 1 },
{ from: [true], to: 1 },
{ from: [null], to: 0 }
{ from: [null], to: 0 },
{ from: [1.5], to: undefined },
{ from: ['abc'], to: undefined },
{ from: [{}], to: undefined },
];
coercionArrayRules.boolean.array = [
{ from: [true], to: true },
{ from: ['true'], to: true },
{ from: [1], to: true },
// { from: ['abc'], to: undefined },
// { from: [2], to: undefined },
{ from: [null], to: false },
{ from: ['abc'], to: undefined },
{ from: [2], to: undefined },
{ from: [{}], to: undefined },
];
coercionArrayRules.null.array = [
{ from: [null], to: null },
{ from: [''], to: null },
{ from: [0], to: null },
{ from: [false], to: null },
// {from: [true], to: undefined},
{ from: ['abc'], to: undefined },
{ from: [1], to: undefined },
{ from: [true], to: undefined },
{ from: [{}], to: undefined },
];
coercionArrayRules.object.array = [
{ from: [{}], to: undefined }
{ from: [{}], to: undefined }
];

coercionArrayRules.array = {
Expand Down

0 comments on commit 73f612f

Please sign in to comment.