Skip to content

Commit

Permalink
implement swagger 2 validation
Browse files Browse the repository at this point in the history
  • Loading branch information
niclim committed Mar 4, 2024
1 parent 2043823 commit 53029cd
Show file tree
Hide file tree
Showing 7 changed files with 1,925 additions and 196 deletions.
24 changes: 24 additions & 0 deletions projects/openapi-io/inputs/swagger2/spec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
swagger: '2.0'
info:
version: '1.2.3'
title: my spec
paths:
/api/users:
get:
produces: ['application/json']
parameters:
- name: search
in: query
description: search for users
required: true
type: string
responses:
'200':
description: 'response'
schema:
type: object
properties:
id:
type: string


6 changes: 5 additions & 1 deletion projects/openapi-io/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {
import { ExternalRefHandler } from './parser/types';
import { JsonPath, JsonSchemaSourcemap } from './parser/sourcemap';
import { loadYaml, isYaml, isJson, writeYaml } from './write';
import { validateOpenApiV3Document } from './validation/validator';
import {
validateSwaggerV2Document,
validateOpenApiV3Document,
} from './validation/validator';
import { ValidationError, OpenAPIVersionError } from './validation/errors';
import {
checkOpenAPIVersion,
Expand Down Expand Up @@ -38,6 +41,7 @@ export {
writeYaml,
dereferenceOpenApi,
ResolverError,
validateSwaggerV2Document,
validateOpenApiV3Document,
ValidationError,
OpenAPIVersionError,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`non-strict validation should fail open api doc with invalid status code shape 1`] = `
exports[`2.x.x validation non-strict validation invalid swagger document should raise errors 1`] = `
"invalid openapi: paths > /api > get must have required property 'responses'
/paths/~1api/get
invalid openapi: swagger must be equal to one of the allowed values 2.0
/swagger
invalid openapi: info must have required property 'version'
/info"
`;

exports[`2.x.x validation strict validation invalid swagger document should raise errors 1`] = `
"invalid openapi: paths > /api > get > responses must NOT be valid
/paths/~1api/get/responses"
`;

exports[`3.x.x validation non-strict validation should fail open api doc with invalid status code shape 1`] = `
"invalid openapi: paths > /example > get > responses > 202 must be object
/paths/~1example/get/responses/202"
`;

exports[`non-strict validation should fail open api doc with no path should throw an error 1`] = `
exports[`3.x.x validation non-strict validation should fail open api doc with no path should throw an error 1`] = `
"invalid openapi:  must have required property 'paths'
"
`;

exports[`non-strict validation should fail open api doc without responses 1`] = `
exports[`3.x.x validation non-strict validation should fail open api doc without responses 1`] = `
"invalid openapi: paths > /example > get must have required property 'responses'
/paths/~1example/get"
`;

exports[`3.x.x validation strict validation advanced validators run and append their results 1`] = `
"invalid openapi: paths > /api/users/{userId} > get > responses > 200 > content > application/json > schema > oneOf must NOT have fewer than 1 items
/paths/~1api~1users~1{userId}/get/responses/200/content/application~1json/schema/oneOf
invalid openapi: paths > /api/users/{userId} > get > responses > 200 > content > application/json > schema > anyOf must NOT have fewer than 1 items
/paths/~1api~1users~1{userId}/get/responses/200/content/application~1json/schema/anyOf
invalid openapi: paths > /api/users/{userId} > get > responses > 200 > content > application/json > schema > items must be object
/paths/~1api~1users~1{userId}/get/responses/200/content/application~1json/schema/items
invalid openapi: paths > /api/users/{userId} > get > responses > 200 > content > application/json > schema schema with type "object" cannot also include keywords: items
/paths/~1api~1users~1{userId}/get/responses/200/content/application~1json/schema"
`;

exports[`3.x.x validation strict validation open api doc with no description in response should throw 1`] = `
"invalid openapi: paths > /example > get > responses > 200 must have required property 'description'
/paths/~1example/get/responses/200"
`;

exports[`processValidatorErrors 1`] = `
"invalid openapi: paths > /api/users/{userId} > get > responses > 200 > content > application/json > schema > properties > hello > items must be object
/paths/~1api~1users~1{userId}/get/responses/200/content/application~1json/schema/properties/hello/items"
Expand All @@ -38,19 +68,3 @@ exports[`processValidatorErrors attaches the sourcemap 1`] = `
40 | }
41 | }"
`;
exports[`strict validation advanced validators run and append their results 1`] = `
"invalid openapi: paths > /api/users/{userId} > get > responses > 200 > content > application/json > schema > oneOf must NOT have fewer than 1 items
/paths/~1api~1users~1{userId}/get/responses/200/content/application~1json/schema/oneOf
invalid openapi: paths > /api/users/{userId} > get > responses > 200 > content > application/json > schema > anyOf must NOT have fewer than 1 items
/paths/~1api~1users~1{userId}/get/responses/200/content/application~1json/schema/anyOf
invalid openapi: paths > /api/users/{userId} > get > responses > 200 > content > application/json > schema > items must be object
/paths/~1api~1users~1{userId}/get/responses/200/content/application~1json/schema/items
invalid openapi: paths > /api/users/{userId} > get > responses > 200 > content > application/json > schema schema with type "object" cannot also include keywords: items
/paths/~1api~1users~1{userId}/get/responses/200/content/application~1json/schema"
`;
exports[`strict validation open api doc with no description in response should throw 1`] = `
"invalid openapi: paths > /example > get > responses > 200 must have required property 'description'
/paths/~1example/get/responses/200"
`;
Loading

0 comments on commit 53029cd

Please sign in to comment.