56 lines
2.1 KiB
TypeScript
56 lines
2.1 KiB
TypeScript
import { Request, Response } from 'express';
|
|
import validator from 'joi'; // DOCS: https://joi.dev/api
|
|
import { Prisma } from '@prisma/client';
|
|
|
|
// MARK: GET alertContact
|
|
const schema_get = validator.object({
|
|
//sort: validator.string().valid('id', 'name', 'phone', 'comment').default('id'),
|
|
sort: validator.string().valid(...Object.keys(Prisma.AlertContactsScalarFieldEnum)).default('id'),
|
|
order: validator.string().valid('asc', 'desc').default('asc'),
|
|
take: validator.number().min(1).max(512),
|
|
skip: validator.number().min(0),
|
|
// This regex ensures that the search string does not contain consecutive asterisks (**) and is at least 3 characters long.
|
|
search: validator.string().min(3).max(20).regex(new RegExp('^(?!.*\\*{2,}).*$')),
|
|
id: validator.number().positive().precision(0),
|
|
count: validator.boolean()
|
|
}).nand('id', 'search'); // Allow id or search. not both.
|
|
|
|
// MARK: CREATE alertContact
|
|
const schema_post = validator.object({
|
|
name: validator.string().min(1).max(32).required(),
|
|
phone: validator.string().pattern(new RegExp('^\\+(\\d{1,3})\\s*(?:\\(\\s*(\\d{2,5})\\s*\\)|\\s*(\\d{2,5})\\s*)\\s*(\\d{5,15})$')).required(),
|
|
comment: validator.string().max(64),
|
|
})
|
|
|
|
// MARK: UPDATE alertContact
|
|
const schema_patch = validator.object({
|
|
id: validator.number().positive().precision(0).required(),
|
|
name: validator.string().min(1).max(32),
|
|
phone: validator.string().pattern(new RegExp('^\\+(\\d{1,3})\\s*(?:\\(\\s*(\\d{2,5})\\s*\\)|\\s*(\\d{2,5})\\s*)\\s*(\\d{5,15})$')),
|
|
comment: validator.string().max(64),
|
|
}).or('name', 'phone', 'comment')
|
|
|
|
// MARK: DELETE alertContact
|
|
const schema_del = validator.object({
|
|
id: validator.number().positive().precision(0).required()
|
|
})
|
|
|
|
// Describe all schemas
|
|
const schema_get_desc = schema_get.describe();
|
|
const schema_post_desc = schema_post.describe();
|
|
const schema_patch_desc = schema_patch.describe();
|
|
const schema_del_desc = schema_del.describe();
|
|
|
|
|
|
// GET route
|
|
export default async function get(req: Request, res: Response) {
|
|
res.status(200).json({
|
|
GET: schema_get_desc,
|
|
POST: schema_post_desc,
|
|
PATCH: schema_patch_desc,
|
|
DELETE: schema_del_desc
|
|
});
|
|
}
|
|
|
|
export { schema_get, schema_post, schema_patch, schema_del }
|