Skip to content

Commit

Permalink
fix(validator): add schema restrictions for OAS3 servers URL (#74)
Browse files Browse the repository at this point in the history
fixes #73
  • Loading branch information
pmstss authored Dec 16, 2021
1 parent 1be34ed commit 931cc8b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/validator/src/schemas/openapi/v3.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,25 @@
}
}
},
"if": {
"required": ["variables"]
},
"then": {
"properties": {
"url": {
"type": "string",
"format": "uri-template"
}
}
},
"else": {
"properties": {
"url": {
"type": "string",
"format": "uri"
}
}
},
"patternProperties": {
"^x-": {}
},
Expand Down
2 changes: 1 addition & 1 deletion packages/validator/tests/error-humanizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('ErrorHumanizer', () => {

const getBaseOasDoc = (): OpenAPIV3.Document => ({
openapi: '3.0.1',
servers: [{ url: 'localhost' }],
servers: [{ url: 'http://localhost' }],
info: {
title: 'Invalid OpenAPI document',
version: '1.0.0'
Expand Down
77 changes: 77 additions & 0 deletions packages/validator/tests/oas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,82 @@ describe('OASValidator', () => {

result.should.deep.eq(expected);
});

it('should return error if `servers[].url` is empty', async () => {
const input: OpenAPIV3.Document = {
openapi: '3.0.0',
servers: [{ url: '' }],
info: {
title: 'Some valid API document',
version: '1.0.0'
},
paths: {}
} as unknown as OpenAPIV3.Document;

const expected: ErrorObject[] = [
{
instancePath: '/servers/0/url',
keyword: 'format',
message: 'must match format "uri"',
params: {
format: 'uri'
},
schemaPath: '#/else/properties/url/format'
}
];

const result = await validator.verify(input);

result.should.deep.eq(expected);
});

it('should return error if `servers[].url` uses template syntax w/o declared variables', async () => {
const input: OpenAPIV3.Document = {
openapi: '3.0.0',
servers: [{ url: 'http://example.com/search{?q,lang}' }],
info: {
title: 'Some valid API document',
version: '1.0.0'
},
paths: {}
} as unknown as OpenAPIV3.Document;

const expected: ErrorObject[] = [
{
instancePath: '/servers/0/url',
keyword: 'format',
message: 'must match format "uri"',
params: {
format: 'uri'
},
schemaPath: '#/else/properties/url/format'
}
];

const result = await validator.verify(input);

result.should.deep.eq(expected);
});

it('should successfully validate `servers[].url` with template syntax if `variables` presents', async () => {
const input: OpenAPIV3.Document = {
openapi: '3.0.0',
servers: [
{
url: '{protocol}://example.com/search{?q,lang}',
variables: {}
}
],
info: {
title: 'Some valid API document',
version: '1.0.0'
},
paths: {}
} as unknown as OpenAPIV3.Document;

const result = await validator.verify(input);

result.should.deep.eq([]);
});
});
});

0 comments on commit 931cc8b

Please sign in to comment.