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 { SchemaId, schemas } from './index';
|
||||||
import { omitKeys } from '../util/omit-keys';
|
import { omitKeys } from '../util/omit-keys';
|
||||||
|
|
||||||
interface ISchemaValidationErrors {
|
export interface ISchemaValidationErrors<S = SchemaId> {
|
||||||
schema: SchemaId;
|
schema: S;
|
||||||
errors: ErrorObject[];
|
errors: ErrorObject[];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,10 +21,10 @@ const ajv = new Ajv({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export const validateSchema = (
|
export const validateSchema = <S = SchemaId>(
|
||||||
schema: SchemaId,
|
schema: S,
|
||||||
data: unknown,
|
data: unknown,
|
||||||
): ISchemaValidationErrors | undefined => {
|
): ISchemaValidationErrors<S> | undefined => {
|
||||||
if (!ajv.validate(schema, data)) {
|
if (!ajv.validate(schema, data)) {
|
||||||
return {
|
return {
|
||||||
schema,
|
schema,
|
||||||
|
@ -64,14 +64,14 @@ export class OpenApiService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
respondWithValidation<T>(
|
respondWithValidation<T, S = SchemaId>(
|
||||||
status: number,
|
status: number,
|
||||||
res: Response<T>,
|
res: Response<T>,
|
||||||
schema: SchemaId,
|
schema: S,
|
||||||
data: T,
|
data: T,
|
||||||
headers: { [header: string]: string } = {},
|
headers: { [header: string]: string } = {},
|
||||||
): void {
|
): void {
|
||||||
const errors = validateSchema(schema, data);
|
const errors = validateSchema<S>(schema, data);
|
||||||
|
|
||||||
if (errors) {
|
if (errors) {
|
||||||
this.logger.debug('Invalid response:', errors);
|
this.logger.debug('Invalid response:', errors);
|
||||||
|
Loading…
Reference in New Issue
Block a user