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

Improve Problem JSON Error Format for Validation #370

Merged
merged 9 commits into from
Jun 24, 2019

Conversation

lag-of-death
Copy link
Contributor

@lag-of-death lag-of-death commented Jun 19, 2019

Checklist

  • Tests have been adjusted (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

What kind of change does this PR introduce?

Feature.

What is the current behavior? What is the new behavior?

The current behavior gives validation error details as a JSON with a string (namely: Your request body is not valid) concatenated together.
Now, the details (422 validation errors) are just that JSON but under validation field. detail field is now just a string with a description of the problem. Also, in the JSON, path field was renamed to location and severitys field value is now represented with a string (like Error), not a number.

Does this PR introduce a breaking change?

This change might involve some changes in handling Prism-generated 422 responses.

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.

Good stuff here,

I suggest avoid creating a separate function and extend the fromTemplate to take in account an additional property that can be anything we need.

packages/http/src/types.ts Show resolved Hide resolved
packages/http-server/src/server.ts Outdated Show resolved Hide resolved
@@ -90,16 +102,37 @@ export class ProblemJsonError extends Error {
return error;
}

public static asValidationIssue(error: Error & { detail: IPrismDiagnostic[]; status?: number }): any {
Copy link
Contributor

Choose a reason for hiding this comment

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

The spec for the problem/json says that effectively additional field apart from the standard one are accepted; I'd suggest, instead of creating a new function to handle the specific case, we could extend the fromTemplate function to add an additional: unknown parameter that we can add to the payload, in this way the final code should be kind of simplified.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hm.. @XVincentX, did you mean fromTemplate or fromPlainError?

packages/http/src/types.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@philsturgeon philsturgeon left a comment

Choose a reason for hiding this comment

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

Seems like something funky is going on with details, it probably shouldn't change away from being string, but the output in tests looks good. Nicely done!

@XVincentX XVincentX force-pushed the feat/so-197/reword-422-validation-errors branch from ef2aa7b to 11a39c3 Compare June 24, 2019 09:22
@XVincentX
Copy link
Contributor

XVincentX commented Jun 24, 2019

@philsturgeon I took over and brought this over the finish line. I've extended the ProblemJson handler to accommodate additional fields — the spec allows them.

Also — for the future, the spec mandates that detail is always a string; so I had to modify a little bit the code.

P.S: I'm not too happy with the way I wrote the ProblemJson implementation, but unfortunately it's required because of the way Fastify handles the the Error serialisation :(

@XVincentX XVincentX force-pushed the feat/so-197/reword-422-validation-errors branch from 11a39c3 to f0e2209 Compare June 24, 2019 09:26
@@ -237,7 +231,9 @@ const createSpec = (specPath) => {

expect(reqRes).toMatchObject(masterFile);

expect(payload.detail).toContain("should have required property 'name'");
expect(payload.detail).toContain(
Copy link
Contributor

Choose a reason for hiding this comment

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

This line is less interesting than the previous assertion: checking it is moaning about required proper name. Can we assert that this new validation property has the validation we are interested in instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll fix it, however all this is going to be removed soon/needs to be converted to the new harness thing — is it worth to spend time on this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh yeah sorry i didnt notice it was in there. Ignore.

@XVincentX
Copy link
Contributor

I'll give @chris-miaskowski some time to jump into this as well

@XVincentX XVincentX self-requested a review June 24, 2019 13:54
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.

I'm auto accepting this :trollface:

@philsturgeon philsturgeon merged commit 9cefd3d into master Jun 24, 2019
@philsturgeon philsturgeon deleted the feat/so-197/reword-422-validation-errors branch June 24, 2019 22:19
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.

None yet

3 participants