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

Undefined array values seem to bypass validation #131

Open
from-the-river-to-the-sea opened this issue Dec 16, 2016 · 3 comments
Open

Undefined array values seem to bypass validation #131

from-the-river-to-the-sea opened this issue Dec 16, 2016 · 3 comments

Comments

@from-the-river-to-the-sea

Hi there!

This may be related to #116.

I have a simple schema that just checks if all items in a given array are strings. However, undefined values seem to pass this validation check, which is undesired. Example code can be found here: https://runkit.com/57ec9a7a8b6a9f1400abb925/5854116ee921e7001333f496

Is this a problem with my schema, or a problem with the generated validator function?

Thanks,
Alex

@LinusU
Copy link
Collaborator

LinusU commented Dec 16, 2016

Hmm, this is an interesting one 🤔

undefined isn't a type in JSON so it's not immediately clear exactly how this should be handled...

I think that right now we are handling undefined as "no value" which is why it passes.

This has some more implications though, what would we expect the following to give?

const schema = {
  type: 'object',
  properties: {
    a: { type: 'string' }
  }
}

const data = {
  a: undefined
}

Since the a property is optional, I'm not sure that this should give an error 🤔

Any input is welcome :)

@from-the-river-to-the-sea
Copy link
Author

For me, this particular problem resulted in some interesting effects.

On the client, I was capturing some property of an array of DOM elements. There was a typo in my code, resulting in me just capturing an array full of undefined. This resulting array passed client-side validation, even though the intent of my schema was to ensure all items in the array are strings.

The array was then JSON serialized, sent across the wire, and received by my server. Some time later, my server would re-validate the data and unexpectedly fail. This is because undefined is serialized as null by JSON.stringify.

I often use JSON schemas in this manner to ensure that my code is doing the right thing, but in this case it doesn't seem to be able to offer me protection.

@LinusU
Copy link
Collaborator

LinusU commented Dec 21, 2016

Hmm, if JSON.stringify treats undefined as null, we should probably to the same here 👍

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

No branches or pull requests

2 participants