Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions packages/http/src/validator/validators/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,19 @@ describe('convertAjvErrors()', () => {
it('converts properly', () => {
expect(
convertAjvErrors(
[Object.assign({}, errorObjectFixture, {
params: { unevaluatedProperty: 'd' },
keyword: 'unevaluatedProperties',
message: 'must NOT have unevaluated propertes',
})],
[
Object.assign({}, errorObjectFixture, {
params: { unevaluatedProperty: 'd' },
keyword: 'unevaluatedProperties',
message: 'must NOT have unevaluated propertes',
}),
],
DiagnosticSeverity.Error,
ValidationContext.Input
)[0]
).toHaveProperty('message', "Request parameter a.b must NOT have unevaluated propertes: 'd'");
});
});
});
});

describe('validateAgainstSchema()', () => {
Expand All @@ -70,6 +72,22 @@ describe('validateAgainstSchema()', () => {
jest.spyOn(convertAjvErrorsModule, 'convertAjvErrors');
});

describe('when schema is invalid', () => {
it('returns an error with severity Error', () => {
assertSome(
validateAgainstSchema('test', { type: 'obj' } as unknown as JSONSchema7, true, ValidationContext.Input, 'pfx'),
error => {
expect(error).toContainEqual(
expect.objectContaining({
message: expect.stringContaining('Prism encountered an error processing the schema'),
severity: DiagnosticSeverity.Error,
})
);
}
);
});
});

describe('has no validation errors', () => {
it('returns no validation errors', () => {
assertNone(validateAgainstSchema('test', { type: 'string' }, true, ValidationContext.Input, 'pfx'));
Expand Down
34 changes: 30 additions & 4 deletions packages/http/src/validator/validators/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IPrismDiagnostic } from '@stoplight/prism-core';
import { DiagnosticSeverity } from '@stoplight/types';
import * as E from 'fp-ts/Either';
import * as O from 'fp-ts/Option';
import { pipe } from 'fp-ts/function';
import { NonEmptyArray, fromArray, map } from 'fp-ts/NonEmptyArray';
Expand Down Expand Up @@ -139,8 +140,33 @@ export const validateAgainstSchema = (
bundle?: unknown
): O.Option<NonEmptyArray<IPrismDiagnostic>> =>
pipe(
O.tryCatch(() => getValidationFunction(assignAjvInstance(String(schema.$schema), coerce), schema, bundle)),
O.chainFirst(validateFn => O.tryCatch(() => validateFn(value))),
O.chain(validateFn => pipe(O.fromNullable(validateFn.errors), O.chain(fromArray))),
O.map(errors => convertAjvErrors(errors, DiagnosticSeverity.Error, context, prefix))
E.tryCatch(
() => getValidationFunction(assignAjvInstance(String(schema.$schema), coerce), schema, bundle),
err => err
),
E.chain(validateFn =>
E.tryCatch(
() => {
validateFn(value);
return validateFn.errors;
},
err => err
)
),
E.fold(
err =>
O.some<NonEmptyArray<IPrismDiagnostic>>([
{
message: `Prism encountered an error processing the schema: ${err instanceof Error ? err.message : String(err)}`,
code: 500,
severity: DiagnosticSeverity.Error,
},
]),
errors =>
pipe(
O.fromNullable(errors),
O.chain(fromArray),
O.map(errors => convertAjvErrors(errors, DiagnosticSeverity.Error, context, prefix))
)
)
);