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: handle invalid schema data #106

Merged
merged 2 commits into from
Jul 11, 2020
Merged

fix: handle invalid schema data #106

merged 2 commits into from
Jul 11, 2020

Conversation

lottamus
Copy link
Contributor

@lottamus lottamus requested a review from marbemac July 11, 2020 01:18
@lottamus lottamus requested a review from XVincentX as a code owner July 11, 2020 01:18
@lottamus lottamus self-assigned this Jul 11, 2020
@lottamus lottamus merged commit 6307aee into master Jul 11, 2020
@lottamus lottamus deleted the fix/handle-invalid-data branch July 11, 2020 01:24
@stoplight-bot
Copy link
Collaborator

🎉 This PR is included in version 3.1.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link
Contributor

@XVincentX XVincentX left a comment

Choose a reason for hiding this comment

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

Hmm, I saw this merged but I got a review request so just posting this for the posterity.

This is very wrong to me. The Schema type is already implying that data is an object and not undefined.

If this line is required it means somebody calling this function is lying/casting. That's not this function's problem; it should not be the its job of this function to validate the passed value; otherwise why do we have the types for?

You can see this effectively by the fact that you had to "lie" in the test by doing a ts-ignore.

I'd vote to fix this where the real problem is and not here @lottamus @marbemac

@XVincentX
Copy link
Contributor

@lottamus @marbemac Can you point out some more details about this so I can try to fix it?

@lottamus
Copy link
Contributor Author

@XVincentX yeah since we are transforming user input, we cannot assume the data property will actually be a Schema without doing some validation on it somewhere in the code. The easiest place was to add it in getExamplesFromSchema, but perhaps we should also update the typing to unknown or add some validator isSchema or something. I'd like to stay pretty lenient and not throw errors on invalid documents, that's why we have Spectral 🤓

@XVincentX
Copy link
Contributor

XVincentX commented Jul 17, 2020

I'd like to stay pretty lenient and not throw errors on invalid documents, that's why we have Spectral

I would not rely on this for the following reasons:

  1. Spectral is a linter, not a parser
  2. The assumption that Spectral has been ran before passing the input is very arguable (Prism does not use Spectral and won't)
  3. Spectral rules can be disabled; a Spectral's OK does not imply a valid document.

I wouldn't want to throw an exception either, but we could use the These monad to carry on parse results and errors at the same time (guess what library has such monad implemented).


That said — I understand now the issue and I've created a ticket to tame this although to be honest, I do not think we're going to have the time to make this right, but you never know.

@lottamus
Copy link
Contributor Author

lottamus commented Jul 17, 2020

Spectral is a linter, not a parser

True, but http-spec should assume user input will be invalid and we should thus update the typings to reflect. The goal of http-spec is to normalize the user input into a format that can be reliably consumed throughout our ecosystem. This means safely handling invalid documents and producing a consistent interface no matter the input.

The assumption that Spectral has been ran before passing the input is very arguable

The reasoning I was trying to make was that it's not http-specs job to report errors/warnings within the document. That job should be on Spectral.

Spectral rules can be disabled; a Spectral's OK does not imply a valid document.

Exactly, thus http-spec should not assume a valid document. We need to make sure our typings reflect that each part of the document could be completely incorrect lol.

@XVincentX
Copy link
Contributor

True, but http-spec should assume user input will be invalid and we should thus update the typings to reflect. The goal of http-spec is to normalize the user input into a format that can be reliably consumed throughout our ecosystem. This means safely handling invalid documents and producing a consistent interface no matter the input.

Completely agree with this. That's way I was pointing out that Spectral is not enough. I must have misunderstood your comment.

We need to make sure our typings reflect that each part of the document could be completely incorrect lol.

I agree with this also, but if so this specific is going on a different direction, right? (t: Schema but then if (isObject(t))) — and that is dangerous.


Seems like we're on the same page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants