2021-01-27 12:35:24 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { prop } = require('lodash/fp');
|
2021-04-29 13:51:12 +02:00
|
|
|
const { yup, formatYupErrors } = require('@strapi/utils');
|
2021-01-27 12:35:24 +01:00
|
|
|
const { isoLocales } = require('../constants');
|
|
|
|
|
|
|
|
const allowedLocaleCodes = isoLocales.map(prop('code'));
|
|
|
|
|
|
|
|
const handleReject = error => Promise.reject(formatYupErrors(error));
|
|
|
|
|
|
|
|
const createLocaleSchema = yup
|
|
|
|
.object()
|
|
|
|
.shape({
|
|
|
|
name: yup
|
|
|
|
.string()
|
|
|
|
.max(50)
|
2021-03-02 18:32:07 +01:00
|
|
|
.nullable(),
|
2021-01-27 12:35:24 +01:00
|
|
|
code: yup
|
|
|
|
.string()
|
|
|
|
.oneOf(allowedLocaleCodes)
|
|
|
|
.required(),
|
2021-02-05 15:39:11 +01:00
|
|
|
isDefault: yup.boolean().required(),
|
2021-01-27 12:35:24 +01:00
|
|
|
})
|
|
|
|
.noUnknown();
|
|
|
|
|
|
|
|
const validateCreateLocaleInput = data => {
|
|
|
|
return createLocaleSchema.validate(data, { strict: true, abortEarly: false }).catch(handleReject);
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateLocaleSchema = yup
|
|
|
|
.object()
|
|
|
|
.shape({
|
|
|
|
name: yup
|
|
|
|
.string()
|
|
|
|
.min(1)
|
|
|
|
.max(50)
|
2021-02-05 15:39:11 +01:00
|
|
|
.nullable(),
|
|
|
|
isDefault: yup.boolean(),
|
2021-01-27 12:35:24 +01:00
|
|
|
})
|
|
|
|
.noUnknown();
|
|
|
|
|
|
|
|
const validateUpdateLocaleInput = data => {
|
|
|
|
return updateLocaleSchema.validate(data, { strict: true, abortEarly: false }).catch(handleReject);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
validateCreateLocaleInput,
|
|
|
|
validateUpdateLocaleInput,
|
|
|
|
};
|