validators.tsā¢828 B
import Joi from 'joi';
import { ResourceQuery } from '../types';
export const resourceQuerySchema = Joi.object<ResourceQuery>({
resourceType: Joi.string()
.valid('ec2', 's3', 'dynamodb', 'lambda', 'iam-role')
.required(),
action: Joi.string()
.valid('create', 'delete')
.required(),
parameters: Joi.object()
.when('action', {
is: 'create',
then: Joi.required(),
otherwise: Joi.optional(),
}),
resourceId: Joi.string()
.when('action', {
is: 'delete',
then: Joi.required(),
otherwise: Joi.optional(),
}),
});
export const validateResourceQuery = (query: any): ResourceQuery => {
const { error, value } = resourceQuerySchema.validate(query);
if (error) {
throw new Error(`Validation error: ${error.details[0].message}`);
}
return value;
};