From 73f612f100f2ae3ccc813a56d09b74593e3fed80 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Fri, 14 Aug 2020 13:17:07 +0100 Subject: [PATCH] fix: coercion of array to scalar that should fail validation --- lib/dot/coerce.def | 46 +++++++++++++++++-------------------------- spec/coercion.spec.js | 22 +++++++++++++++------ 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/lib/dot/coerce.def b/lib/dot/coerce.def index 86e0e18af..c947ed6af 100644 --- a/lib/dot/coerce.def +++ b/lib/dot/coerce.def @@ -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){{?}} diff --git a/spec/coercion.spec.js b/spec/coercion.spec.js index 1a8426af1..587671529 100644 --- a/spec/coercion.spec.js +++ b/spec/coercion.spec.js @@ -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 } ]; @@ -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 = {