-
-
Notifications
You must be signed in to change notification settings - Fork 209
fix: remove array items type checking #524
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
Conversation
@@ -152,28 +153,56 @@ buildTest({ | |||
'@data': ['test'] | |||
}) | |||
|
|||
test('invalid items throw', (t) => { | |||
test('coerce number to string type item', (t) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is patternProperties
still a thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know why there is a patternProperties in this test. It works without them ('invalid' string always doesn't match the object schema). The test will work differently after this PR, but not because of patternProperties.
How the test works now:
test('invalid items throw', (t) => {
t.plan(1)
const schema = {
type: 'object',
properties: {
args: {
type: 'array',
items: [
{
type: 'object',
patternProperties: {
'.*': {
type: 'string'
}
}
}
]
}
}
}
const stringify = build(schema)
t.throws(() => stringify({ args: ['invalid'] })) // throws an error because 'invalid' is not an object
})
How it will work:
test('invalid items throw', (t) => {
t.plan(1)
const schema = {
type: 'object',
properties: {
args: {
type: 'array',
items: [
{
type: 'object',
patternProperties: {
'.*': {
type: 'string'
}
}
}
]
}
}
}
const stringify = build(schema)
t.equal(stringify({ args: ['invalid'] }), '{"args":[{"0":"i","1":"n","2":"v","3":"a","4":"l","5":"i","6":"d"}]}') // it coerces 'invalid' string to the object schema
})
It will work just like the same schema with patternProperties works now without array.
test('invalid items throw', (t) => {
t.plan(1)
const schema = {
type: 'object',
patternProperties: {
'.*': {
type: 'string'
}
}
}
const stringify = build(schema)
t.equal(stringify('invalid'), '{"0":"i","1":"n","2":"v","3":"a","4":"l","5":"i","6":"d"}')
})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Eomm I decided to remove the test instead of updating it because this coercion looks like nonsense. If you want I can add the test like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of removing all the "validation" logic here, but I would move it to the next
branch.
I think the new output could warn a user that does not understand what is going on: I would prefer an empty output instead of {"0":"i","1":"n","2":"v","3":"a","4":"l","5":"i","6":"d"}
, but we can't do it without keeping the "validation" logic tho
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The output you see isn't related to the array at all. It's all about how FJS coerces string to object.
I have merged |
I'm ok for landing this in the next branch. |
For me, it's more like a fix, but if you all think it should be merged into the next, I'm ok with it. I want to clarify, why did we merge #511 into the master branch instead of the next branch? It has the same type of changes. |
Ah ok! Then I'm ok with a fix/ |
It is hard to tell. And here, we know that it will certainly break most of the people. It is more appropriate to wait for a major bump, not one PR for one major bump. |
As I said, I'm ok with it. P.S. #504 is completely safe to release. |
Type checking in this place is not only unnecessary (FJS is not a validator), it blocks type coercion for array items for no reason.