Skip to content

Conversation

@alemagio
Copy link
Contributor

Checklist

Copy link
Member

@zekth zekth left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Member

@RafaelGSS RafaelGSS left a comment

Choose a reason for hiding this comment

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

Not sure if this is unexpected behavior. The following snippet sent an empty array by default

const schema = {
  type: 'object',
  properties: {
    arr: {
      type: 'array',
      items: {
        type: 'number'
      },
      default: [],
    }
  }
}

const stringify = fastJson(schema)
const valueStr = stringify({}) // {"arr":[]}

Although throwing an error doesn't look good.

@alemagio
Copy link
Contributor Author

@RafaelGSS Added a unit test with your case.
I'm not having any error, am I missing somenthing?

@RafaelGSS
Copy link
Member

RafaelGSS commented Oct 12, 2020

No, I said the current implementation(master) seems to have expected behavior since null is sent. I'll wait for some contributors to give their opinion.

Btw the last test case is not necessary, is already being tested here

@alemagio
Copy link
Contributor Author

No, I said the current implementation(master) seems to have expected behavior since null is sent. I'll wait for some contributors to give their opinion.

Btw the last test case is not necessary, is already being tested here

Oh ok.
I'll remove last test in the meantime.

}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure passing null for the array would validate this schema? Can you check with ajv?

My understanding is that null is not a valid array.

Copy link
Member

Choose a reason for hiding this comment

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

const schema = {
  type: 'object',
  properties: {
    foo: {
      $id: '#/properties/foo',
      type: 'array',
      default: [],
      items: {
        $id: '#/properties/foo/items'
      }
    }
  }
}
const obj = {
  foo: null
}
var Ajv = require('ajv')
var ajv = new Ajv()
var validate = ajv.compile(schema)
var valid = validate(obj)
if (!valid) console.log(validate.errors)
// output
// [
//   {
//     keyword: 'type',
//     dataPath: '.foo',
//     schemaPath: '#/properties/foo/type',
//     params: { type: 'array' },
//     message: 'should be array'
//   }
// ]

Indeed

@zekth zekth self-requested a review October 13, 2020 08:35
@alemagio
Copy link
Contributor Author

I followed the issue, too strictly actually.
Tried with ajv and null is not valid when type is array.
I think a good way to handle this is to throw an error with a more informative message.
What do you think?

@mcollina
Copy link
Member

I think a good way to handle this is to throw an error with a more informative message.

Yes, go for it!

@alemagio
Copy link
Contributor Author

Updated the code.
Now in case something expected to be an array is something else a clearer error is thrown.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina merged commit b909e00 into fastify:master Oct 14, 2020
@alemagio
Copy link
Contributor Author

alemagio commented Oct 14, 2020

@mcollina Would you mind adding hacktoberfest-accepted label to this PR so I can complete my Hacktoberfest? 😄

@alemagio
Copy link
Contributor Author

Thanks 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

null array value throws TypeError: Cannot read property 'length' of null

4 participants