diff --git a/packages/core/lib/plugins/tools/schema.keyword.coercion.js b/packages/core/lib/plugins/tools/schema.keyword.coercion.js index 876b6d81c..2fdd56877 100644 --- a/packages/core/lib/plugins/tools/schema.keyword.coercion.js +++ b/packages/core/lib/plugins/tools/schema.keyword.coercion.js @@ -22,49 +22,8 @@ export default { : [parentSchema.type]; switch (types[0]) { case "array": - // Still a work in process to handle all usages gen.if(codegen._`!Array.isArray(${data})`, () => { - // gen.assign(coerced, codegen._`[${data}]`); - const arTypes = Array.isArray(parentSchema.items?.type) - ? parentSchema.items?.type[0] - : parentSchema.items?.type - if(arTypes === 'string'){ - // Not, boolean coercion should not be enabled by default but with - // `{..., coercion: ["boolean_to_string"] }` - // Or: - // `{..., coercion: {boolean_to_string: true} }` - gen.if(codegen._`typeof ${data} === "boolean"`, () => { - gen.assign(coerced, codegen._`${data} ? ["1"] : [""]`); - }); - gen.if(codegen._`typeof ${data} === "number"`, () => { - gen.assign(coerced, codegen._`["" +${data}]`); - }); - gen.if(codegen._`typeof ${data} === "string"`, () => { - gen.assign(coerced, codegen._`[${data}]`); - }); - // Not working but this seems like the correct/recommanded approach - // gen.block() - // gen.if(codegen._`typeof ${data} === "string"`) - // gen.assign(coerced, codegen._`[${data}]`); - // gen.if(codegen._`typeof ${data} === "boolean"`) - // gen.assign(coerced, codegen._`${data} ? ["1"] : [""]`); - // gen.if(codegen._`typeof ${data} === "number"`); - // gen.assign(coerced, codegen._`["" +${data}]`); - // gen.else() - // gen.assign(coerced, codegen._`[${data}]`) - // gen.endBlock() - }else if(arTypes === 'boolean'){ - gen.if( - codegen._`typeof ${data} === "string" || typeof ${data} === "number"`, - () => { - gen.assign(coerced, codegen._`[${data} != ""]`); - } - ); - }else if(arTypes === 'integer'){ - gen.assign(coerced, codegen._`[+${data}]`) - }else{ - gen.assign(coerced, codegen._`[${data}]`) - } + gen.assign(coerced, codegen._`[${data}]`); }); break; case "boolean": @@ -97,12 +56,14 @@ export default { // `{..., coercion: ["boolean_to_string"] }` // Or: // `{..., coercion: {boolean_to_string: true} }` - gen.if(codegen._`typeof ${data} === "boolean"`, () => { - gen.assign(coerced, codegen._`${data} ? "1" : ""`); - }); - gen.if(codegen._`typeof ${data} === "number"`, () => { - gen.assign(coerced, codegen._`"" +${data}`); - }); + gen.block() + gen.if(codegen._`typeof ${data} === "boolean"`); + gen.assign(coerced, codegen._`${data} ? "1" : ""`) + gen.elseIf(codegen._`typeof ${data} === "number"`); + gen.assign(coerced, codegen._`"" +${data}`) + gen.else() + gen.assign(coerced, codegen._`${data}`) + gen.endBlock() break; } gen.if(codegen._`${coerced} !== undefined`, () => { diff --git a/packages/core/test/plugins/tools/schema.coercion.coffee b/packages/core/test/plugins/tools/schema.coercion.coffee index ca3cab0b6..29a0d667c 100644 --- a/packages/core/test/plugins/tools/schema.coercion.coffee +++ b/packages/core/test/plugins/tools/schema.coercion.coffee @@ -197,9 +197,7 @@ describe 'plugins.tools.schema.coercion', -> config.from_string_filled.should.eql ['ok'] config.from_object.should.eql [ {key: 'value'} ] - it 'to types', -> - # Should be merge with previous test - # Still a work in process to handle all usages + it 'with types', -> nikita $definitions: config: @@ -209,32 +207,38 @@ describe 'plugins.tools.schema.coercion', -> type: 'array' coercion: true items: - type: ["integer"] + type: ["integer", "string"] + coercion: true 'to_boolean_false_from_string': type: 'array' coercion: true items: - type: ["boolean"] + type: ["boolean", "string"] + coercion: true 'to_boolean_true_from_string': type: 'array' coercion: true items: - type: ["boolean"] + type: ["boolean", "string"] + coercion: true 'to_boolean_true_from_integer': type: 'array' coercion: true items: - type: ["boolean"] + type: ["boolean", "integer"] + coercion: true 'to_string_from_integer': type: 'array' coercion: true items: - type: ["string"] + type: ["string", "integer"] + coercion: true 'to_string_from_boolean': type: 'array' coercion: true items: - type: ["string"] + type: ["string", "boolean"] + coercion: true to_integer_from_string: '744' to_boolean_false_from_string: '' to_boolean_true_from_string: '744'