2020-05-14 11:57:43 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { yup, formatYupErrors } = require('strapi-utils');
|
2020-05-18 19:54:43 +02:00
|
|
|
const validators = require('./common-validators');
|
2020-05-14 11:57:43 +02:00
|
|
|
|
|
|
|
const handleReject = error => Promise.reject(formatYupErrors(error));
|
|
|
|
|
2020-05-18 17:16:49 +02:00
|
|
|
const userCreationSchema = yup
|
|
|
|
.object()
|
|
|
|
.shape({
|
2020-05-18 20:39:39 +02:00
|
|
|
email: validators.email.required(),
|
|
|
|
firstname: validators.firstname.required(),
|
|
|
|
lastname: validators.lastname.required(),
|
2020-05-18 20:49:11 +02:00
|
|
|
roles: yup.array(), // FIXME: set min to 1 once the create role API is created,
|
2020-05-18 17:16:49 +02:00
|
|
|
})
|
|
|
|
.noUnknown();
|
2020-05-14 11:57:43 +02:00
|
|
|
|
|
|
|
const validateUserCreationInput = data => {
|
|
|
|
return userCreationSchema.validate(data, { strict: true, abortEarly: false }).catch(handleReject);
|
|
|
|
};
|
|
|
|
|
2020-05-18 19:54:43 +02:00
|
|
|
const profileUpdateSchema = yup
|
|
|
|
.object()
|
|
|
|
.shape({
|
2020-05-18 20:39:39 +02:00
|
|
|
email: validators.email,
|
2020-05-18 19:54:43 +02:00
|
|
|
firstname: validators.firstname,
|
|
|
|
lastname: validators.lastname,
|
2020-05-18 20:39:39 +02:00
|
|
|
username: yup
|
|
|
|
.string()
|
|
|
|
.min(1)
|
|
|
|
.nullable(),
|
2020-05-18 19:54:43 +02:00
|
|
|
password: validators.password,
|
|
|
|
})
|
|
|
|
.noUnknown();
|
|
|
|
|
|
|
|
const validateProfileUpdateInput = data => {
|
|
|
|
return profileUpdateSchema
|
|
|
|
.validate(data, { strict: true, abortEarly: false })
|
|
|
|
.catch(handleReject);
|
|
|
|
};
|
|
|
|
|
2020-05-14 11:57:43 +02:00
|
|
|
module.exports = {
|
|
|
|
validateUserCreationInput,
|
2020-05-18 19:54:43 +02:00
|
|
|
validateProfileUpdateInput,
|
2020-05-14 11:57:43 +02:00
|
|
|
};
|