Skip to content

Commit

Permalink
refactor(core): schema coercion simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Jun 14, 2024
1 parent c0d25b9 commit 4f5555f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 57 deletions.
57 changes: 9 additions & 48 deletions packages/core/lib/plugins/tools/schema.keyword.coercion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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`, () => {
Expand Down
22 changes: 13 additions & 9 deletions packages/core/test/plugins/tools/schema.coercion.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
Expand Down

0 comments on commit 4f5555f

Please sign in to comment.