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

Allow custom validators in ApiHandler #21

Merged
merged 4 commits into from
Apr 7, 2022

Conversation

s-fletcher
Copy link
Contributor

  • Deprecates ajv and schemas options inside of ApiHandler
  • Allows validators to be passed in as an alternative to schemas
  • Sets up all AJV logic to be wiped in next major release.
  • Adds prettier settings to environment (copied from @faceteer/facet repo)

@s-fletcher s-fletcher linked an issue Apr 1, 2022 that may be closed by this pull request
@codecov
Copy link

codecov bot commented Apr 1, 2022

Codecov Report

Merging #21 (c791e8b) into main (b68b2f6) will increase coverage by 0.49%.
The diff coverage is 95.65%.

@@            Coverage Diff             @@
##             main      #21      +/-   ##
==========================================
+ Coverage   83.22%   83.72%   +0.49%     
==========================================
  Files          11       11              
  Lines         322      338      +16     
  Branches       76       78       +2     
==========================================
+ Hits          268      283      +15     
- Misses         50       55       +5     
+ Partials        4        0       -4     
Impacted Files Coverage Δ
handlers/api-handler.ts 92.04% <95.65%> (+0.37%) ⬆️
handlers/queue-handler.ts 78.26% <0.00%> (ø)
response/failed-response.ts 80.00% <0.00%> (ø)
response/success-response.ts 69.23% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b68b2f6...c791e8b. Read the comment docs.

@github-actions

This comment has been minimized.

@github-actions
Copy link

github-actions bot commented Apr 1, 2022

Unit Test Results

  1 files  ±0    4 suites  ±0   5s ⏱️ ±0s
17 tests +2  17 ✔️ +2  0 💤 ±0  0 ±0 
18 runs  +3  18 ✔️ +3  0 💤 ±0  0 ±0 

Results for commit c791e8b. ± Comparison against base commit b68b2f6.

This pull request removes 3 and adds 5 tests. Note that renamed tests count towards both.
Api Handler Api Handler Handles Invalid Requests ‑ Api Handler Api Handler Handles Invalid Requests
Api Handler Api Handler validates ‑ Api Handler Api Handler validates
Api Handler Returns a 400 on validation failures ‑ Api Handler Returns a 400 on validation failures
Api Handler Api Handler with Schemas Handles Invalid Requests ‑ Api Handler Api Handler with Schemas Handles Invalid Requests
Api Handler Api Handler with Validators Handles Invalid Requests ‑ Api Handler Api Handler with Validators Handles Invalid Requests
Api Handler Api Handler with schemas validates ‑ Api Handler Api Handler with schemas validates
Api Handler Returns a 400 on schema validation failures ‑ Api Handler Returns a 400 on schema validation failures
Api Handler Returns a 400 on validator validation failures ‑ Api Handler Returns a 400 on validator validation failures

♻️ This comment has been updated with latest results.

Comment on lines +190 to +192
if (validators) {
validateBodyResult = validateInput(validators.body, event.body);
validateQueryResult = validateInput(validators.query, queryBody);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The intention here is to give validators top priority if both validation methods are specified.

@s-fletcher s-fletcher marked this pull request as ready for review April 2, 2022 00:11
Copy link
Contributor

@bill-kerr bill-kerr left a comment

Choose a reason for hiding this comment

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

Thanks for doing this! One comment on the argument type of the validate function

body?: JSONSchemaType<B>;
query?: JSONSchemaType<Q>;
response?: JSONSchemaType<R>;
};

validators?: {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice--thanks for making this optional. We have a few routes that don't require schemas--and passing in an empty object felt werid.

handlers/api-handler.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@bill-kerr bill-kerr left a comment

Choose a reason for hiding this comment

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

LFG!

@s-fletcher s-fletcher merged commit 00466f9 into main Apr 7, 2022
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.

Non-opinionated validator for ApiHandler
2 participants