validationSchema={ Yup.object().shape({ email: Yup.string().email('Must be a valid email').max(255).required('Email is required'), firstName: Yup.string().max(255).required('First name is required'), lastName: Yup.string().max(255).required('Last name is required'), password: Yup.string().max(255).required('password is required'), policy: Yup.boolean().oneOf([true], 'This field must be checked') })
server.post( RESOURCE_PATH, validation.validatePostBody({ tweet: yup.string().min(3).max(100).required(), user: yup.string().min(3).max(50).required() }), async (req, res, next) => { const tweet = await tweetService.createTweet(req.body); const locationHeader = httpHelper.createLocationHeaderString({req, id: tweet.id}); res.header('Location', locationHeader); res.send(201, tweet); next(); } );
forForm() { let yupChain = yup .array() .label(this.label) .of( yup .string() .email(i18n('user.validations.email')) .label(i18n('user.fields.email')) .max(255) .required(), ); if (this.required) { yupChain = yupChain.required(); } return yupChain; }
forForm() { let yupChain = yup .string() .nullable(true) .trim() .label(this.label); if (this.required) { yupChain = yupChain.required(); } if (this.min || this.min === 0) { yupChain = yupChain.min(this.min); } if (this.max) { yupChain = yupChain.max(this.max); } if (this.matches) { yupChain = yupChain.matches(/^[0-9]/); } return yupChain; }
const createDelivery = async (req, res, next) => { try { const schema = Yup.object(req.body).shape({ product: Yup.string() .min(2, 'Product must be at least 2 characters.') .max(100, 'Product has a maximum limit of 100 characters.') .required('Product is required.'), recipient_id: Yup.number().required('Recipient is required'), deliveryman_id: Yup.number().required('Deliveryman is required') }); await schema.validate(req.params, { abortEarly: false }); return next(); } catch (error) { return res .status(400) .json({ error: 'Validation fails', messages: error.inner }); } }
const updateDelivery = async (req, res, next) => { try { const schema = Yup.object(req.body).shape({ product: Yup.string() .min(2, 'Product must be at least 2 characters.') .max(100, 'Product has a maximum limit of 100 characters.'), recipient_id: Yup.number().nullable(false), deliveryman_id: Yup.number().nullable(false) }); await schema.validate(req.params, { abortEarly: false }); return next(); } catch (error) { return res .status(400) .json({ error: 'Validation fails', messages: error.inner }); } }
const updateDeliveryman = async (req, res, next) => { try { const schema = Yup.object().shape({ name: Yup.string() .min(2, 'Name must be at least 2 characters.') .max(100, 'Name has a maximum limit of 100 characters.'), email: Yup.string().max( 100, 'E-mail has a maximum limit of 100 characters' ), avatar_id: Yup.number().positive() }); await schema.validate(req.body, { abortEarly: false }); return next(); } catch (error) { return res .status(400) .json({ error: 'Validation fails.', messages: error.inner }); } }
const createDeliveryProblem = async (req, res, next) => { try { const params = Yup.object().shape({ id: Yup.number() .positive() .required('Id is required.') }); const body = Yup.object().shape({ description: Yup.string() .min(2, 'Description must be at least 5 characters.') .max(100, 'Description has a maximum limit of 100 characters.') .required('Description is required.') }); await params.validate(req.params, { abortEarly: false }); await body.validate(req.body, { abortEarly: false }); return next(); } catch (error) { return res .status(400) .json({ error: 'Validation fails.', messages: error.inner }); } }
const updateRecipient = async (req, res, next) => { try { const schema = Yup.object().shape({ name: Yup.string() .min(2, 'Name must be at least 2 characters.') .max(100, 'Name has a maximum limit of 100 characters.'), street: Yup.string() .min(2, 'Street must be at least 2 characters.') .max(100, 'Street has a maximum limit of 100 characters.'), number: Yup.number(), complement: Yup.number(), state: Yup.string() .min(2, 'State must be at least 2 characters.') .max(100, 'State has a maximum limit of 100 characters.'), city: Yup.string() .min(2, 'City must be at least 2 characters.') .max(100, 'City has a maximum limit of 100 characters.'), zip_code: Yup.string() }); await schema.validate(req.body, { abortEarly: false }); return next(); } catch (error) { return res .status(400) .json({ error: 'Validation fails.', messages: error.inner }); } }
const createSession = async (req, res, next) => { try { const schema = Yup.object().shape({ email: Yup.string() .email() .max(100, 'E-mail has a maximum limit of 100 characters.') .required(), password: Yup.string() .min(6, 'Password must be at least 6 characters.') .max(25, 'Password must be a maximum limit of 25 characters.') .required() }); await schema.validate(req.body, { abortEarly: false }); return next(); } catch (error) { return res .status(400) .json({ error: 'Validation fails.', messages: error.inner }); } }
const createDeliveryman = async (req, res, next) => { try { const schema = Yup.object().shape({ name: Yup.string() .min(2, 'Name must be at least 2 characters.') .max(100, 'Name has a maximum limit of 100 characters.') .required('Name is required.'), email: Yup.string() .max(100, 'E-mail has a maximum limit of 100 characters') .required('E-mail is required.'), avatar_id: Yup.number().required('Avatar_id is required') }); await schema.validate(req.body, { abortEarly: false }); return next(); } catch (error) { return res .status(400) .json({ error: 'Validation fails.', messages: error.inner }); } }
forFormRules() { let yupValidator = yup .array() .label(this.label) .of( yup .string() .email(i18n('user.validations.email')) .label(i18n('user.fields.email')) .max(255) .required(), ); if (this.required) { yupValidator = yupValidator.required(); } const validator = (rule, value, callback) => { try { yupValidator.validateSync(value); callback(); } catch (error) { callback(error); } }; return [{ validator }]; }
forImport() { let yupChain = yup .string() .nullable(true) .trim() .label(this.label); if (this.required) { yupChain = yupChain.required(); } if (this.min || this.min === 0) { yupChain = yupChain.min(this.min); } if (this.max) { yupChain = yupChain.max(this.max); } if (this.matches) { yupChain = yupChain.matches(/^[0-9]/); } return yupChain; }
forImport() { let yupChain = yup .string() .nullable(true) .trim() .label(this.label); if (this.required) { yupChain = yupChain.required(); } if (this.min || this.min === 0) { yupChain = yupChain.min(this.min); } if (this.max) { yupChain = yupChain.max(this.max); } if (this.matches) { yupChain = yupChain.matches(/^[0-9]/); } return yupChain; }