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

fix: improve schema validation #1071

Merged
merged 15 commits into from
Apr 12, 2020
Merged
16 changes: 14 additions & 2 deletions src/__tests__/linter.jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,13 @@ describe('Linter', () => {

const rules = {
'strings-maxLength': testRuleset.rules['strings-maxLength'],
'oas3-schema': testRuleset.rules['oas3-schema'],
'oas3-schema': {
...testRuleset.rules['oas3-schema'],
then: {
...testRuleset.rules['oas3-schema'].then,
function: 'oasDocumentSchema',
},
},
};

spectral.setRuleset({ rules, exceptions: {}, functions: {} });
Expand Down Expand Up @@ -397,7 +403,13 @@ describe('Linter', () => {

const rules = {
'no-yaml-remote-reference': testRuleset.rules['no-yaml-remote-reference'],
'oas3-schema': testRuleset.rules['oas3-schema'],
'oas3-schema': {
...testRuleset.rules['oas3-schema'],
then: {
...testRuleset.rules['oas3-schema'].then,
function: 'oasDocumentSchema',
},
},
};

spectral.setRuleset({ rules, exceptions: {}, functions: {} });
Expand Down
157 changes: 156 additions & 1 deletion src/rulesets/oas/functions/__tests__/oasDocumentSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { isOpenApiv2, isOpenApiv3, RuleType, Spectral } from '../../../..';
import { functions } from '../../../../functions';
import { setFunctionContext } from '../../../evaluators';
import { rules } from '../../index.json';
import oasDocumentSchema from '../oasDocumentSchema';
import oasDocumentSchema, { prepareResults } from '../oasDocumentSchema';

import { ErrorObject } from 'ajv';
import * as oas2Schema from '../../schemas/schema.oas2.json';
import * as oas3Schema from '../../schemas/schema.oas3.json';

Expand Down Expand Up @@ -189,4 +190,158 @@ describe('oasDocumentSchema', () => {
]);
});
});

describe('prepareResults', () => {
test('given oneOf error one of which is required $ref property missing, picks only one error', () => {
const errors: ErrorObject[] = [
{
keyword: 'type',
dataPath: '/paths/test/post/parameters/0/schema/type',
schemaPath: '#/properties/type/type',
params: { type: 'string' },
message: 'should be string',
},
{
keyword: 'required',
dataPath: '/paths/test/post/parameters/0/schema',
schemaPath: '#/definitions/Reference/required',
params: { missingProperty: '$ref' },
message: "should have required property '$ref'",
},
{
keyword: 'oneOf',
dataPath: '/paths/test/post/parameters/0/schema',
schemaPath: '#/properties/schema/oneOf',
params: { passingSchemas: null },
message: 'should match exactly one schema in oneOf',
},
];

prepareResults(errors);

expect(errors).toStrictEqual([
P0lip marked this conversation as resolved.
Show resolved Hide resolved
{
keyword: 'type',
dataPath: '/paths/test/post/parameters/0/schema/type',
schemaPath: '#/properties/type/type',
params: { type: 'string' },
message: 'should be string',
},
]);
});

test('given oneOf error one without any $ref property missing, picks all errors', () => {
const errors: ErrorObject[] = [
{
keyword: 'type',
dataPath: '/paths/test/post/parameters/0/schema/type',
schemaPath: '#/properties/type/type',
params: { type: 'string' },
message: 'should be string',
},
{
keyword: 'type',
dataPath: '/paths/test/post/parameters/1/schema/type',
schemaPath: '#/properties/type/type',
params: { type: 'string' },
message: 'should be string',
},
{
keyword: 'oneOf',
dataPath: '/paths/test/post/parameters/0/schema',
schemaPath: '#/properties/schema/oneOf',
params: { passingSchemas: null },
message: 'should match exactly one schema in oneOf',
},
];

prepareResults(errors);

expect(errors).toStrictEqual([
{
keyword: 'type',
dataPath: '/paths/test/post/parameters/0/schema/type',
schemaPath: '#/properties/type/type',
params: { type: 'string' },
message: 'should be string',
},
{
dataPath: '/paths/test/post/parameters/1/schema/type',
keyword: 'type',
message: 'should be string',
params: {
type: 'string',
},
schemaPath: '#/properties/type/type',
},
{
dataPath: '/paths/test/post/parameters/0/schema',
keyword: 'oneOf',
message: 'should match exactly one schema in oneOf',
params: {
passingSchemas: null,
},
schemaPath: '#/properties/schema/oneOf',
},
]);
});

test('given errors with different data paths, picks all errors', () => {
const errors: ErrorObject[] = [
{
keyword: 'type',
dataPath: '/paths/test/post/parameters/0/schema/type',
schemaPath: '#/properties/type/type',
params: { type: 'string' },
message: 'should be string',
},
{
keyword: 'required',
dataPath: '/paths/foo/post/parameters/0/schema',
schemaPath: '#/definitions/Reference/required',
params: { missingProperty: '$ref' },
message: "should have required property '$ref'",
},
{
keyword: 'oneOf',
dataPath: '/paths/baz/post/parameters/0/schema',
schemaPath: '#/properties/schema/oneOf',
params: { passingSchemas: null },
message: 'should match exactly one schema in oneOf',
},
];

prepareResults(errors);

expect(errors).toStrictEqual([
{
dataPath: '/paths/test/post/parameters/0/schema/type',
keyword: 'type',
message: 'should be string',
params: {
type: 'string',
},
schemaPath: '#/properties/type/type',
},
{
dataPath: '/paths/foo/post/parameters/0/schema',
keyword: 'required',
message: "should have required property '$ref'",
params: {
missingProperty: '$ref',
},
schemaPath: '#/definitions/Reference/required',
},
{
dataPath: '/paths/baz/post/parameters/0/schema',
keyword: 'oneOf',
message: 'should match exactly one schema in oneOf',
params: {
passingSchemas: null,
},
schemaPath: '#/properties/schema/oneOf',
},
]);
});
});
});
2 changes: 1 addition & 1 deletion src/rulesets/oas/functions/oasDocumentSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const ERROR_MAP = [
];

// /shrug
P0lip marked this conversation as resolved.
Show resolved Hide resolved
function prepareResults(errors: AJV.ErrorObject[]) {
export function prepareResults(errors: AJV.ErrorObject[]) {
for (let i = 0; i < errors.length; i++) {
const error = errors[i];

Expand Down