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

Allow integer in number subschema with strictTypes #1938

Closed

Conversation

JacobLey
Copy link
Contributor

What issue does this pull request resolve?
resolves #1935

What changes did you make?
Add number to includesType check during strictTypes validation.

Is there anything that requires more attention while reviewing?
Might be an overly broad solution. Should check be more conservative about appending number type?

@@ -286,7 +286,7 @@ function checkContextTypes(it: SchemaObjCxt, types: JSONType[]): void {
strictTypesError(it, `type "${t}" not allowed by context "${it.dataTypes.join(",")}"`)
}
})
it.dataTypes = it.dataTypes.filter((t) => includesType(types, t))
it.dataTypes = it.dataTypes.filter((t) => includesType([...types, "number"], t))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure this is the offending line, just not sure if this is the optimal solution.

In my example from the issue, it.dataTypes === ['number'] and types === ['integer']

The existing logic would filter dataTypes down to an empty array, then throw on error on keywords like maximum without a type.

It is filtered because includesType does not detect any overlap, and would (by intentional design) only allow number in the base type.

includesType(['number'], 'integer') === true
includesType(['integer'], 'number') === false

My solution is to simply append number to the type array, to effectively allow the second case to be true as well.

Intuitively it seems like an overly broad solution, as number is not a valid type for every other type, and we still want to prevent number inside an integer.

However the lines just before this are performing that validation/filtering, so I'm pretty sure at this point both type arrays are are length <= 1.

If there is a better way to write this, or perhaps another way to attack this let me know!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's indeed more tricky than that... updated!

@epoberezkin epoberezkin added this to the 8.12 milestone Nov 13, 2022
@epoberezkin
Copy link
Member

updated in #2192

@epoberezkin epoberezkin closed this Jan 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

strictTypes does not expect "narrowing" integer with number keywords
2 participants