strapi/packages/core/utils/lib/validators.js

112 lines
3.4 KiB
JavaScript
Raw Normal View History

2019-06-24 15:31:22 +02:00
'use strict';
2019-12-24 14:49:44 +01:00
const yup = require('yup');
const _ = require('lodash');
2021-11-03 19:31:57 +01:00
const { defaults } = require('lodash/fp');
2021-08-20 15:23:02 +02:00
const utils = require('./string-formatting');
2021-10-27 18:54:58 +02:00
const { YupValidationError } = require('./errors');
2021-11-04 10:54:13 +01:00
const printValue = require('./print-value');
const MixedSchemaType = yup.mixed;
const isNotNilTest = value => !_.isNil(value);
function isNotNill(msg = '${path} must be defined.') {
return this.test('defined', msg, isNotNilTest);
}
const isNotNullTest = value => !_.isNull(value);
function isNotNull(msg = '${path} cannot be null.') {
return this.test('defined', msg, isNotNullTest);
}
2021-05-31 18:28:42 +02:00
function isFunction(message = '${path} is not a function') {
2021-07-08 11:20:13 +02:00
return this.test('is a function', message, value => _.isUndefined(value) || _.isFunction(value));
2021-05-31 18:28:42 +02:00
}
2021-06-08 10:39:45 +02:00
function isCamelCase(message = '${path} is not in camel case (anExampleOfCamelCase)') {
2021-08-20 15:23:02 +02:00
return this.test('is in camelCase', message, value => utils.isCamelCase(value));
}
function isKebabCase(message = '${path} is not in kebab case (an-example-of-kebab-case)') {
return this.test('is in kebab-case', message, value => utils.isKebabCase(value));
2021-06-08 10:39:45 +02:00
}
2021-06-21 12:02:10 +02:00
function onlyContainsFunctions(message = '${path} contains values that are not functions') {
return this.test(
'only contains functions',
message,
2021-07-08 11:20:13 +02:00
value => _.isUndefined(value) || (value && Object.values(value).every(_.isFunction))
2021-06-21 12:02:10 +02:00
);
}
yup.addMethod(yup.mixed, 'notNil', isNotNill);
yup.addMethod(yup.mixed, 'notNull', isNotNull);
2021-05-31 18:28:42 +02:00
yup.addMethod(yup.mixed, 'isFunction', isFunction);
2021-06-08 10:39:45 +02:00
yup.addMethod(yup.string, 'isCamelCase', isCamelCase);
2021-08-20 15:23:02 +02:00
yup.addMethod(yup.string, 'isKebabCase', isKebabCase);
2021-06-21 12:02:10 +02:00
yup.addMethod(yup.object, 'onlyContainsFunctions', onlyContainsFunctions);
class StrapiIDSchema extends MixedSchemaType {
constructor() {
super({ type: 'strapiID' });
}
_typeCheck(value) {
2020-06-04 14:27:16 +02:00
return typeof value === 'string' || (Number.isInteger(value) && value >= 0);
}
}
yup.strapiID = () => new StrapiIDSchema();
2021-11-03 19:31:57 +01:00
const handleYupError = (error, errorMessage) => {
throw new YupValidationError(error, errorMessage);
};
const defaultValidationParam = { strict: true, abortEarly: false };
const validateYupSchema = (schema, options = {}) => async (body, errorMessage) => {
try {
const optionsWithDefaults = defaults(defaultValidationParam, options);
return await schema.validate(body, optionsWithDefaults);
} catch (e) {
handleYupError(e, errorMessage);
}
};
const validateYupSchemaSync = (schema, options = {}) => (body, errorMessage) => {
try {
const optionsWithDefaults = defaults(defaultValidationParam, options);
return schema.validateSync(body, optionsWithDefaults);
} catch (e) {
handleYupError(e, errorMessage);
}
2021-10-27 18:54:58 +02:00
};
2021-11-04 10:54:13 +01:00
// Temporary fix of this issue : https://github.com/jquense/yup/issues/616
yup.setLocale({
mixed: {
notType({ path, type, value, originalValue }) {
let isCast = originalValue != null && originalValue !== value;
let msg =
`${path} must be a \`${type}\` type, ` +
`but the final value was: \`${printValue(value, true)}\`` +
(isCast ? ` (cast from the value \`${printValue(originalValue, true)}\`).` : '.');
/* Remove comment that is not supposed to be seen by the enduser
if (value === null) {
msg += `\n If "null" is intended as an empty value be sure to mark the schema as \`.nullable()\``;
}
*/
return msg;
},
},
});
2019-12-24 14:49:44 +01:00
module.exports = {
yup,
2021-10-27 18:54:58 +02:00
handleYupError,
2021-11-03 19:31:57 +01:00
validateYupSchema,
validateYupSchemaSync,
2019-12-24 14:49:44 +01:00
};