|
|
|
@ -2,6 +2,7 @@ import { NextFunction, Request, Response } from 'express';
|
|
|
|
|
import Ajv, { ErrorObject } from 'ajv'; |
|
|
|
|
// @ts-ignore
|
|
|
|
|
import swagger from '../../../../schema/swagger.json'; |
|
|
|
|
import { NcError } from '../../helpers/catchError' |
|
|
|
|
|
|
|
|
|
export function parseHrtimeToSeconds(hrtime) { |
|
|
|
|
const seconds = (hrtime[0] + hrtime[1] / 1e6).toFixed(3); |
|
|
|
@ -29,10 +30,29 @@ export const getAjvValidatorMw = (schema) => {
|
|
|
|
|
|
|
|
|
|
// If the request body is invalid, send a response with an error message
|
|
|
|
|
res.status(400).json({ |
|
|
|
|
status: 'error', |
|
|
|
|
message: 'Invalid request body', |
|
|
|
|
errors, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// a function to validate the payload against the schema
|
|
|
|
|
export const ajvValidator = (schema, payload) => { |
|
|
|
|
// Validate the request body against the schema
|
|
|
|
|
const valid = ajv.validate( |
|
|
|
|
typeof schema === 'string' ? { $ref: schema } : schema, |
|
|
|
|
payload |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
// If the request body is not valid, throw error
|
|
|
|
|
if (!valid) { |
|
|
|
|
const errors: ErrorObject[] | null | undefined = ajv.errors; |
|
|
|
|
|
|
|
|
|
// If the request body is invalid, throw error with error message and errors
|
|
|
|
|
NcError.ajvValidationError({ |
|
|
|
|
message: 'Invalid request body', |
|
|
|
|
errors, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|