Files
cod-api/validate/error_code.schema.js

71 lines
2.0 KiB
JavaScript

const Joi = require("joi");
// ========================
// Error Code Validation
// ========================
const solutionSchema = Joi.object({
solution_name: Joi.string().max(100).required(),
type_solution: Joi.string()
.valid("text", "pdf", "image", "video", "link")
.required(),
text_solution: Joi.when("type_solution", {
is: "text",
then: Joi.string().required(),
otherwise: Joi.string().optional().allow(""),
}),
path_solution: Joi.when("type_solution", {
is: "text",
then: Joi.string().optional().allow(""),
otherwise: Joi.string().required(),
}),
is_active: Joi.boolean().default(true),
});
const insertErrorCodeSchema = Joi.object({
error_code: Joi.string().max(100).required(),
error_code_name: Joi.string().max(100).required(),
error_code_description: Joi.string().optional().allow(""),
error_code_color: Joi.string().optional().allow(""),
path_icon: Joi.string().optional().allow(""),
is_active: Joi.boolean().default(true),
solution: Joi.array()
.items(solutionSchema)
.min(1)
.required()
.messages({
"array.min": "Error code must have at least 1 solution",
}),
spareparts: Joi.array()
.items(Joi.number().integer())
.optional(),
}).messages({
"object.unknown": "{{#child}} is not allowed",
});
const updateErrorCodeSchema = Joi.object({
error_code_name: Joi.string().max(100).optional(),
error_code_description: Joi.string().optional().allow(""),
error_code_color: Joi.string().optional().allow(""),
path_icon: Joi.string().optional().allow(""),
is_active: Joi.boolean().optional(),
solution: Joi.array()
.items(solutionSchema)
.min(1)
.optional()
.messages({
"array.min": "Error code must have at least 1 solution",
}),
spareparts: Joi.array()
.items(Joi.number().integer())
.optional(),
}).min(1).messages({
"object.min": "At least one field must be provided for update",
"object.unknown": "{{#child}} is not allowed",
});
module.exports = {
insertErrorCodeSchema,
updateErrorCodeSchema,
solutionSchema,
};