wiki: Thinking in LoopBack - validate the spec using a 3rd party validator
In https://github.com/strongloop/loopback-next/wiki/Thinking-in-LoopBack, we have a section about validating the Swagger/OpenAPI spec.
In order to ensure this specification is valid, we need to write a basic validator.
Let's not reinvent the wheel and show our users how to integrate swagger-parser into their test suite.
Additionally, show how to add custom schema linting on top of the generic validation, in the spirit of @ritch comment below:
Maybe I didn't make the point clearly enough - you aren't just validating the swagger syntax - you are also validating your own semantics - this is more like linting than validating, maybe that would be a better name.
#475 is adding app.validateApiSpec() method that can be used here.
Based on our recent discussion with @ritch, for the initial beta release it's enough to show how to use swagger-parser for validating OpenAPI spec. Something along the following lines:
it('has valid api spec', async () => {
await validateApiSpec(app.getApiSpec());
});
It'd be good to incorporate this validation to hello-world tutorial code for LB.Next beta along with Authentication component and custom sequence based on Authentication. Hello-world already has a simple OpenAPI spec.
If the hello-world approach makes sense, the size estimate of this portion is: 1.
It'd be good to incorporate this validation to hello-world tutorial code for LB.Next beta along with Authentication component and custom sequence based on Authentication. Hello-world already has a simple OpenAPI spec.
Let's keep this GH issue focused on API validation only please. I agree it would be good to incorporate api validation hello-world tutorial, possibly to other loopback-next-* example repositories too.
I created https://github.com/strongloop/loopback-next/issues/490 to keep track of adding Authentication to hello-world.
Added spec valiation to hello-world and fixed the spec: https://github.com/strongloop/loopback-next-hello-world/commit/2db4dcbe4c18091531468cedd6f593b33defecbd
Added spec valiation to hello-world and fixed the spec: strongloop/loopback-next-hello-world@2db4dcb
Could you please send a pull request with that change?
Edited two sections of the wiki Thinking in LoopBack chapter::
Validating the API specificationsection:
https://github.com/strongloop/loopback-next/wiki/Thinking-in-LoopBack#validating-the-api-specification
Testing section:
https://github.com/strongloop/loopback-next/wiki/Thinking-in-LoopBack#testing
@Setogit good start. IMO, the first snippet showing implementation of validateApiSpec is not needed. What's more important: make the second code snippet with the mocha test self-contained, so that users can simply copy and paste it into their project. Also please apply https://github.com/strongloop/loopback-next/pull/484/commits/8f422d32e91fc4e73c6a2cf6143168f5776472cc#r131851865
- removed the first snippet.
- made the second snippet copy&paste-able.
- applied the blog/readme and mentioned the OpenAPI swagger editor
This seems to be short and balanced description for the wiki. ^^^ @bajtos
@Setogit thanks, this is good enough, we can close this story as done. I'll make some further tweaks as part of my work on #393.
Let me use this opportunity for a bit of education:
describe('validate api spec', () => {
// ...
it('api spec is valid', async () => {
// ...
});
});
Mocha's describe and it interface is called BDD (for behaviour-driven development), because the tests should be describing application's behavior and read like English sentences. Your short test name says "it api spec is valid" and the long name says "validate api spec api spec is valid", that does not make sense to me.
Here are better names:
describe('Application api spec', () => {
// ...
it('is valid', async () => {
// ...
});
});
See http://loopback.io/doc/en/contrib/style-guide.html for more details (feel free to send a PR to capture some of my comment in that page for future readers).
describe('validate api spec', () => {
let app: Application = createApp();
let apiSpec: OpenApiSpec = app.getApiSpec();
it('api spec is valid', async () => {
// ...
In Mocha, it's a best practice to use before and beforeEach hooks to setup any values needed by the tests. In this particular case, your code will work, but there are situations where this approach will backfire. IMO, our documentation should be using best practices whenever possible.
(I can fix the wiki content myself as part of #393 to speed the process up.)
+1 thanks, @bajtos