Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
describe('oas3-valid-schema-example ', () => {
const s = new Spectral();
s.registerFormat('oas3', () => true);
s.setRules({
'oas3-valid-schema-example ': Object.assign(ruleset.rules['oas3-valid-schema-example '], {
recommended: true,
severity: DiagnosticSeverity.Error,
type: RuleType[ruleset.rules['oas3-valid-schema-example ']!.type],
}),
});
test('will pass when simple example is valid', async () => {
const results = await s.run({
openapi: '3.0.2',
components: {
schemas: {
xoxo: {
type: 'string',
example: 'doggie',
},
},
},
});
it('returns static example', () => {
jest.spyOn(helpers, 'negotiateOptionsForValidRequest');
jest.spyOn(helpers, 'negotiateOptionsForInvalidRequest');
mock({
config: { dynamic: false },
resource: mockResource,
input: Object.assign({}, mockInput, { validations: [{ severity: DiagnosticSeverity.Error }] }),
})(logger);
expect(helpers.negotiateOptionsForValidRequest).not.toHaveBeenCalled();
expect(helpers.negotiateOptionsForInvalidRequest).toHaveBeenCalled();
});
});
test('will fail when simple example is invalid', async () => {
const results = await s.run({
openapi: '3.0.2',
[path]: {
xoxo: {
schema: {
type: 'string',
example: 123,
},
},
},
});
expect(results).toEqual([
expect.objectContaining({
severity: DiagnosticSeverity.Error,
code: ruleName,
message: '"schema.example" property type should be string',
}),
]);
});
it('logs error', () => {
violationLogger(logger)({ severity: DiagnosticSeverity.Error, message: 'Test' });
expect(logger.error).toHaveBeenCalledWith({ name: 'VALIDATOR' }, 'Violation: Test');
expect(logger.warn).not.toHaveBeenCalled();
expect(logger.info).not.toHaveBeenCalled();
});
});
error =>
expect(error).toContainEqual({
code: 'pattern',
message: 'should match pattern "^(yes|no)$"',
path: ['query', 'overwrite'],
severity: DiagnosticSeverity.Error,
})
);
Array.reduce([], (diagnostics, encoding) => {
const allowReserved = get(encoding, 'allowReserved', false);
const property = encoding.property;
const value = encodedUriParams[property];
if (!allowReserved && typeof value === 'string' && /[/?#[\]@!$&'()*+,;=]/.test(value)) {
diagnostics.push({
path: [property],
message: 'Reserved characters used in request body',
severity: DiagnosticSeverity.Error,
});
}
return diagnostics;
}),
diagnostics => (Array.isNonEmpty(diagnostics) ? E.left(diagnostics) : E.right(encodedUriParams))
result => result.severity === DiagnosticSeverity.Error,
);
O.map(errors => convertAjvErrors(errors, DiagnosticSeverity.Error, prefix))
);
return (violation: IPrismDiagnostic) => {
const path = violation.path ? violation.path.join('.') + ' ' : '';
const message = `Violation: ${path}${violation.message}`;
if (violation.severity === DiagnosticSeverity.Error) {
logger.error({ name: 'VALIDATOR' }, message);
} else if (violation.severity === DiagnosticSeverity.Warning) {
logger.warn({ name: 'VALIDATOR' }, message);
} else {
logger.info({ name: 'VALIDATOR' }, message);
}
};
});
function negotiateResponse(
mockConfig: IHttpOperationConfig,
input: IPrismInput,
resource: IHttpOperation
) {
const { [DiagnosticSeverity.Error]: errors, [DiagnosticSeverity.Warning]: warnings } = groupBy(
input.validations,
validation => validation.severity
);
if (errors) {
return handleInputValidation(input, resource);
} else {
return pipe(
withLogger(logger => {
warnings && warnings.forEach(warn => logger.warn({ name: 'VALIDATOR' }, warn.message));
return logger.success(
{ name: 'VALIDATOR' },
'The request passed the validation rules. Looking for the best response'
);
}),
R.chain(() => helpers.negotiateOptionsForValidRequest(resource, mockConfig))