Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: coerce whitespace strings correctly #2297

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/compile/validate/dataType.ts
Expand Up @@ -99,15 +99,15 @@ function coerceData(it: SchemaObjCxt, types: JSONType[], coerceTo: JSONType[]):
gen
.elseIf(
_`${dataType} == "boolean" || ${data} === null
|| (${dataType} == "string" && ${data} && ${data} == +${data})`
|| (${dataType} == "string" && ${data} && ${data}.trim() && ${data} == +${data})`
)
.assign(coerced, _`+${data}`)
return
case "integer":
gen
.elseIf(
_`${dataType} === "boolean" || ${data} === null
|| (${dataType} === "string" && ${data} && ${data} == +${data} && !(${data} % 1))`
|| (${dataType} === "string" && ${data} && ${data}.trim() && ${data} == +${data} && !(${data} % 1))`
)
.assign(coerced, _`+${data}`)
return
Expand Down
2 changes: 2 additions & 0 deletions spec/coercion.spec.ts
Expand Up @@ -29,6 +29,7 @@ const coercionRules = {
{from: "1a", to: undefined},
{from: "abc", to: undefined},
{from: "", to: undefined},
{from: " ", to: undefined},
],
boolean: [
{from: false, to: 0},
Expand All @@ -49,6 +50,7 @@ const coercionRules = {
{from: "1a", to: undefined},
{from: "abc", to: undefined},
{from: "", to: undefined},
{from: " ", to: undefined},
],
boolean: [
{from: false, to: 0},
Expand Down