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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

add async validation support #4762

Merged
merged 1 commit into from Mar 3, 2020

Conversation

mastermunj
Copy link
Contributor

Add support for async validations. The need arises from allowing custom formats & keywords for ajv, which can also be async in nature.

Checklist

馃憠 Read and sign the CLA (Contributor License Agreement) 馃憟

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

馃憠 Check out how to submit a PR 馃憟

} catch (error) {
expect(error.message).to.equal(INVALID_MSG);
expect(error.code).to.equal('VALIDATION_FAILED');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We use await expect(aPromise).to.be.rejectedWith(...) style.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

let validationResult = validate(body);
let validationErrors: AJV.ErrorObject[] = [];
try {
validationResult = await validationResult;
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not very friendly to change the type of validationResult in the same function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing it out. I've simplified the logic. Is new one better & clear?

Copy link
Contributor

@raymondfeng raymondfeng left a comment

Choose a reason for hiding this comment

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

Please squash the commits into one.

@mastermunj
Copy link
Contributor Author

mastermunj commented Feb 29, 2020

Uh oh! I messed it up it seems.

Edit 1: Apologies for messing up. I will try to fix it with a hard reset. If it doesn't work, I will send another PR.

Edit 2: It seems to have been done properly now. Please help check if it's done right.

@mastermunj mastermunj force-pushed the async-validation branch 2 times, most recently from f023e0c to 25965ba Compare February 29, 2020 19:03
@dougal83
Copy link
Contributor

@mastermunj Don't worry if you make mistakes with Git, we've all been there. There are plenty of very helpful people who can help you fix what seems like a disaster. It's source control... most problems can be fixed.

Copy link
Member

@achrinza achrinza left a comment

Choose a reason for hiding this comment

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

Thank you @mastermunj for the PR 鉂わ笍

I've added a comment regarding the drop in test coverage and how we might be able to resolve it.

As what @dougal83 mentioned: No worries, if there's any git-related issue, we can help.

If things goes south, there's probably no need to create a new PR. At most a hard-reset against origin/master will bring things back on track.

Comment on lines 148 to 152
// When schema is not available, ajv returns null
if (validationResult || validationResult === null) {
debug('Request body passed AJV validation.');
return;
}
Copy link
Member

@achrinza achrinza Mar 1, 2020

Choose a reason for hiding this comment

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

Seems like this branch isn't fully covered with tests yet (hence the reduced test coverage).

Is there a case where the schema isn't available when AJV is called?

It'll be great if we can add a test case to prevent regression.

Here's the Coveralls report

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @achrinza, this test case doesn't pass without validationResult === null as promise returned by validate returns null.

My comment there seems misleading with schema is not available. Schema is available but body is optional and value is empty.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@achrinza did you get a chance to look into this?

Copy link
Member

@achrinza achrinza Mar 3, 2020

Choose a reason for hiding this comment

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

@mastermunj I would suggest editing the comment to be more accurate to the purpose of the conditional statement.

Though @raymondfeng has approved the PR and this issue is a non-blocker for me.

So everything LGTM 馃憤

Copy link
Member

Choose a reason for hiding this comment

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

My comment there seems misleading with schema is not available. Schema is available but body is optional and value is empty.

I would really like the commit to be updated before this pull request is landed, to avoid confusion in the future.

@achrinza achrinza added the REST Issues related to @loopback/rest package and REST transport in general label Mar 1, 2020
@raymondfeng
Copy link
Contributor

raymondfeng commented Mar 2, 2020

@mastermunj I suggest you further refine the commit message as follows:

feat(rest): add async validation support

BREAKING CHANGE: validateRequestBody is now an async function to allow asynchronous validations by custom Ajv keywords and formats. See https://ajv.js.org/#asynchronous-validation
for more details.

@mastermunj
Copy link
Contributor Author

@raymondfeng modified commit message as per your suggestion.

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

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

This is awesome, I really like how the pull request is focused on one thing only (enabling async validations) 馃憤

Let's fix the misleading commit please, before we proceed to land the pull request.

Comment on lines 148 to 152
// When schema is not available, ajv returns null
if (validationResult || validationResult === null) {
debug('Request body passed AJV validation.');
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

My comment there seems misleading with schema is not available. Schema is available but body is optional and value is empty.

I would really like the commit to be updated before this pull request is landed, to avoid confusion in the future.

BREAKING CHANGE: `validateRequestBody` is now an async function to allow asynchronous validations by custom Ajv keywords and formats. See https://ajv.js.org/#asynchronous-validation
for more details.
@dhmlau
Copy link
Member

dhmlau commented Mar 3, 2020

@slnode test please

@raymondfeng raymondfeng merged commit 5b9a1ef into loopbackio:master Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change feature REST Issues related to @loopback/rest package and REST transport in general
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants