61 lines
1.7 KiB
JavaScript
Raw Normal View History

'use strict';
const { yup, formatYupErrors } = require('strapi-utils');
const validators = require('./common-validators');
const handleReject = error => Promise.reject(formatYupErrors(error));
const userCreationSchema = yup
.object()
.shape({
email: validators.email.required(),
firstname: validators.firstname.required(),
lastname: validators.lastname.required(),
2020-05-27 16:06:15 +02:00
roles: validators.roles, // FIXME: set min to 1 once the create role API is created,
})
.noUnknown();
const validateUserCreationInput = data => {
return userCreationSchema.validate(data, { strict: true, abortEarly: false }).catch(handleReject);
};
const profileUpdateSchema = yup
.object()
.shape({
email: validators.email.notNull(),
firstname: validators.firstname.notNull(),
lastname: validators.lastname.notNull(),
2020-05-27 16:06:15 +02:00
username: validators.username.nullable(),
password: validators.password.notNull(),
})
.noUnknown();
const validateProfileUpdateInput = data => {
return profileUpdateSchema
.validate(data, { strict: true, abortEarly: false })
.catch(handleReject);
};
2020-05-27 16:06:15 +02:00
const userUpdateSchema = yup
.object()
.shape({
email: validators.email.notNull(),
firstname: validators.firstname.notNull(),
lastname: validators.lastname.notNull(),
2020-05-27 16:06:15 +02:00
username: validators.username.nullable(),
password: validators.password.notNull(),
isActive: yup.bool().notNull(),
roles: validators.roles.min(1).notNull(),
2020-05-27 16:06:15 +02:00
})
.noUnknown();
const validateUserUpdateInput = data => {
return userUpdateSchema.validate(data, { strict: true, abortEarly: false }).catch(handleReject);
};
module.exports = {
validateUserCreationInput,
validateProfileUpdateInput,
2020-05-27 16:06:15 +02:00
validateUserUpdateInput,
};