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

Validate response json in [apm appveyor cdnjs clojars gem] also affects [npm] #1883

Merged
merged 11 commits into from
Aug 10, 2018

Conversation

chris48s
Copy link
Member

This fleshes out the stub validators added in #1743 into something a bit more robust. I've not looked at splitting into fetch() and render() here - just the validation. I'll save that for another PR.

I did find the first few quite took quite a long time, but once I'd done a few I got the hang of it. I think we can make this easier for new contributors with good documentation and examples, but it will need some explanation.

99fc836 and cf04796 aren't strictly related, but I spotted the examples were broken while I was working on that class and its a quick fix.

})
)
.min(1)
.required()
Copy link
Member Author

Choose a reason for hiding this comment

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

I hit an edge case here. Depending on which downloads badge we are building (dt/dtv or dv) we request a different endpoint on rubygems.org. This means there is more than one possible response schema. We can cover it by using Joi.alternatives().try() like I've done here, but I wonder if it could be better to think in terms of a URL pattern/schema pair here instead of a one-to-one badge/schema relationship? Any thoughts? Am I just over-complicating things? Are there other situations where we might hit a more complicated version of this?

Copy link
Member

Choose a reason for hiding this comment

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

Yea, the npm downloads badge had the same issue. There I handled it with a switch:

const schema = isRange ? rangeResponseSchema : pointResponseSchema

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes - that's a much better way of doing it

@shields-ci
Copy link

Warnings
⚠️

This PR modified service code for gem but not its test code. That's okay so long as it's refactoring existing code.

⚠️

This PR modified service code for apm but not its test code. That's okay so long as it's refactoring existing code.

⚠️

This PR modified service code for appveyor but not its test code. That's okay so long as it's refactoring existing code.

⚠️

This PR modified service code for cdnjs but not its test code. That's okay so long as it's refactoring existing code.

⚠️

This PR modified service code for clojars but not its test code. That's okay so long as it's refactoring existing code.

⚠️

This PR modified service code for npm but not its test code. That's okay so long as it's refactoring existing code.

Messages
📖

✨ Thanks for your contribution to Shields, @chris48s!

Generated by 🚫 dangerJS

Copy link
Member

@paulmelnikow paulmelnikow left a comment

Choose a reason for hiding this comment

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

Looking good. I'm assuming all you changed in the services (other than the commits you mention) is the split into new files and the schema. It's hard to see if anything else has changed.


const Joi = require('joi')

const positiveInteger = Joi.number()
Copy link
Member

Choose a reason for hiding this comment

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

nonnegativeInteger?

@@ -6,24 +6,27 @@ const { NotFound } = require('../errors')
const { addv: versionText } = require('../../lib/text-formatters')
const { version: versionColor } = require('../../lib/color-formatters')

const cdnjsSchema = Joi.object({
version: Joi.string(),
Copy link
Member

Choose a reason for hiding this comment

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

I think you want version: Joi.string().required().

Copy link
Member Author

Choose a reason for hiding this comment

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

On both CDNJS and Clojars the version key is intentionally optional because {} should also pass validation.

Copy link
Member

Choose a reason for hiding this comment

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

Could you add a comment in the schema explaining why?

const url = `https://rubygems.org/api/v1/owners/${user}/gems.json`
const json = await this._requestJson({
url,
schema: Joi.array(),
Copy link
Member

Choose a reason for hiding this comment

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

Looks like this one is pending ^^

Copy link
Member Author

Choose a reason for hiding this comment

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

I intentionally left this because in this badge we are not using any of the data in the array - we are just counting the number of elements in it. As long the response is an array we're fine. I've moved the declaration out of the class and made it required() in 410b329 though. I don't think we need to be any more specific

keywords: ['atom'],
},
]
}
Copy link
Member

Choose a reason for hiding this comment

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

❤️

@paulmelnikow paulmelnikow added the service-badge New or updated service badge label Aug 10, 2018
@chris48s
Copy link
Member Author

It's hard to see if anything else has changed.

All the moving stuff around is in 8121a90 to try and make it easier to separate that from the commits that make functional changes


const rankSchema = Joi.array()
.items(
Joi.alternatives().try(
Copy link
Member

Choose a reason for hiding this comment

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

Do you want to do the same here?

@chris48s chris48s merged commit f05a349 into badges:master Aug 10, 2018
@shields-deployment
Copy link

This pull request was merged to master branch. Now this change is waiting for deployment.
Deploys usually happen every few weeks. After deployment changes are copied to gh-pages branch.

This badge displays deployment status:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
service-badge New or updated service badge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants