mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-23 00:22:19 +01:00
Make it possible to validate enterprise schema with respondWithValidation (#2781)
Now respondWithValidation<T, S = SchemaId> can be called in oss and enterprise to validate against needed schema.
This commit is contained in:
parent
d5e47ac352
commit
ec535ad7cc
@ -2,8 +2,8 @@ import Ajv, { ErrorObject } from 'ajv';
|
||||
import { SchemaId, schemas } from './index';
|
||||
import { omitKeys } from '../util/omit-keys';
|
||||
|
||||
interface ISchemaValidationErrors {
|
||||
schema: SchemaId;
|
||||
export interface ISchemaValidationErrors<S = SchemaId> {
|
||||
schema: S;
|
||||
errors: ErrorObject[];
|
||||
}
|
||||
|
||||
@ -21,10 +21,10 @@ const ajv = new Ajv({
|
||||
},
|
||||
});
|
||||
|
||||
export const validateSchema = (
|
||||
schema: SchemaId,
|
||||
export const validateSchema = <S = SchemaId>(
|
||||
schema: S,
|
||||
data: unknown,
|
||||
): ISchemaValidationErrors | undefined => {
|
||||
): ISchemaValidationErrors<S> | undefined => {
|
||||
if (!ajv.validate(schema, data)) {
|
||||
return {
|
||||
schema,
|
||||
|
@ -64,14 +64,14 @@ export class OpenApiService {
|
||||
});
|
||||
}
|
||||
|
||||
respondWithValidation<T>(
|
||||
respondWithValidation<T, S = SchemaId>(
|
||||
status: number,
|
||||
res: Response<T>,
|
||||
schema: SchemaId,
|
||||
schema: S,
|
||||
data: T,
|
||||
headers: { [header: string]: string } = {},
|
||||
): void {
|
||||
const errors = validateSchema(schema, data);
|
||||
const errors = validateSchema<S>(schema, data);
|
||||
|
||||
if (errors) {
|
||||
this.logger.debug('Invalid response:', errors);
|
||||
|
Loading…
Reference in New Issue
Block a user