validate(cube, errorReporter) { Joi.validate(cube, cubeSchema, (err) => { if (err) { errorReporter.error(err.message); } else { this.validCubes[cube.name] = true; } }); }
validateAndCompile(dashboardTemplate, errorReporter) { Joi.validate(dashboardTemplate, dashboardTemplateSchema, (err) => { if (err) { errorReporter.error(err.message); } else { this.compiledTemplates.push(this.compileTemplate(dashboardTemplate, errorReporter)); } }); }
const normalizeQuery = (query) => { const { error } = Joi.validate(query, querySchema); if (error) { throw new UserError(`Invalid query format: ${error.message || error.toString()}`);
_.curry((schema, payload) => { const { error, value } = Joi.validate(payload, schema, joiOptions); return { errors: parseJoiErrors(error), value, }; })
const registerValidation = data => { const schema = { name: Joi.string().min(6).required(), email: Joi.string().min(6).required().email(), password: Joi.string().min(6).required() }; return Joi.validate(data, schema) }
static validateWithSchema(value, schema) { const validatorOptions = { abortEarly: false, } const { error } = Joi.validate(value, schema, validatorOptions) if (error !== null && error.details) { return error.details.map(item => new ValidatorError(item.message, item.context.key)) } return null }
const loginValidation = data => { const schema = { email: Joi.string().min(6).required().email(), password: Joi.string().min(6).required() }; return Joi.validate(data, schema) }
router.post('/change-password', isAuthenticated, async (req, res) => { const requestSchema = Joi.object().keys({ oldPassword: Joi.string() .trim() .required(), newPassword: Joi.string() .trim() .required(), }); const { error: validationError, value: validatedRequestBody } = Joi.validate(req.body, requestSchema); if (validationError) { return res.status(400).json({ details: validationError.details[0].message }); } try { const isMatching = await req.user.comparePassword(validatedRequestBody.oldPassword); if (isMatching) { req.user.password = validatedRequestBody.newPassword; await req.user.save(); res.json({ status: 'Pasword updated.' }); } else { res.status(500).json({ details: 'Old password is invalid!' }); } } catch (error) { logger.error(error); res.status(500).json({ details: error }); } });
const request = (params) => { return new Promise((resolve, reject) => { const result = joi.validate(params, schema) if (!result) { const error = new Error('Invalid parameters') reject(error) } const defaultParams = { baseURL: '', method: 'post', url: '', headers: {}, params: {}, data: {}, timeout: 1000, responseType: 'json' } axios({ ...defaultParams, ...params }).then((response) => { resolve(response) }).catch((error) => { reject(error) }) }) }
route.post('/:id/update', (req, res) => { const schema = Joi.object().keys({ fruit: Joi.string().required(), stock: Joi.number().required() }) Joi.validate(req.body, schema, (err, result) => { if (err) { console.log(err); res.send("Error"); } else { fruitModel.updateById(req.params.id, { name: req.body.fruit, stock: req.body.stock }) .then(data => { console.log(data); res.redirect("/fruits"); }) .catch(err => { console.log(err); res.send("Unexcepted error occured"); }) } }) })
}); const { error: validationError, value: data } = Joi.validate(req.body, schema);
router.post('/admin/change-user-password/:id', isAuthenticated, isAdmin, async (req, res) => { const requestSchema = Joi.object().keys({ oldPassword: Joi.string() .trim() .required(), newPassword: Joi.string() .trim() .required(), }); const { error: validationError, value: validatedRequestBody } = Joi.validate(req.body, requestSchema); if (validationError) { return res.status(400).json({ details: validationError.details[0].message }); } try { const targetUser = await User.findById(req.params.id); const isMatching = await targetUser.comparePassword(validatedRequestBody.oldPassword); if (isMatching) { targetUser.password = validatedRequestBody.newPassword; await targetUser.save(); res.json({ status: 'Pasword updated.' }); } else { res.status(500).json({ details: 'Old password is invalid!' }); } } catch (error) { logger.error(error); res.status(500).json({ details: error }); } });
static validate(values, schema) { let errors const validatorOptions = { abortEarly: false, } const { error, value } = Joi.validate(values, schema, validatorOptions) if (error !== null && error.details) { errors = error.details.map(item => new ValidatorError(item.message, item.context.key)) } if (errors) { throw new MultiValidationError((values && values.id) || '', errors) } return value }