-
Notifications
You must be signed in to change notification settings - Fork 339
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
refactor: validation returns eithers #731
Conversation
This will be a follow up PR. This is just the beginning of breaking it down so that we can easily at least compose the steps (route, validate input, mock, validate output, send response) that is required for Hosted Prism. We'll iterate and move on as we go. |
006a15e
to
41b74a7
Compare
41b74a7
to
fc6d787
Compare
5ca08ae
to
de94910
Compare
inputValidations: IPrismDiagnostic[]; | ||
}; | ||
|
||
const inputValidation = ( |
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.
something more verb-ish, like:
const inputValidation = ( | |
const getInputValidation = ( |
describe('request is not set', () => { | ||
it('validates headers', validate(undefined, 3)); | ||
it('does not validate headers', validate(undefined, undefined, 0)); |
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.
there's a couple of these, does it mean that there's a user-facing change in Prism behavior or is it just a part of the refactor?
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.
Exactly, why the number of validation errors changed from 0 to 3?
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.
Good question. So previously our validators were ran even if there was no need to run them. You can see it here: https://github.com/stoplightio/prism/pull/731/files#diff-52fafd78c752aca126e4fd8c4ce567a6L45-L47
You can see that the functions were called anyway even if there was nothing to validate. In the following lines instead you can see there's a check to make sure it's the case to call them or otherwise call directly Either.right
In this tests, the validators were mocked to return an error in any case, so no matter what was happening, all the validators were always called and returning an error. Body + Query + Headers = 3 validators — that explains the number.
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 have really no improvement requests for this PR. Some parts were mindblowing. I took me double-espresso to understand validationSequence
.
I'd like to discuss that comment in context of further refactoring: https://github.com/stoplightio/prism/pull/731/files#diff-2991521f751aa9cf68976ceef512e19aR23
describe('request is not set', () => { | ||
it('validates headers', validate(undefined, 3)); | ||
it('does not validate headers', validate(undefined, undefined, 0)); |
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.
Exactly, why the number of validation errors changed from 0 to 3?
Co-Authored-By: Karol Maciaszek <karol@maciaszek.pl>
This PR is a start to a refactor of the validators approach.
As I mentioned somewhere, the idea is to have 3 PRs that will do the job
Here's what I did:
IPrismDiagnostic[]
, but rather anEither<NonEmptyArray<IPrismDiagnostic>, T>
— which means either a validatedT
or an array of validation errors. https://github.com/stoplightio/prism/pull/731/files#diff-2991521f751aa9cf68976ceef512e19afactory.ts
file where you can see that finally validation and mocking are two differentTaskEither
steps melted together with chain calls.