2023-04-27 23:18:48 +02:00
|
|
|
/* eslint-disable no-template-curly-in-string */
|
|
|
|
import * as yup from 'yup';
|
|
|
|
import _ from 'lodash';
|
|
|
|
import { defaults } from 'lodash/fp';
|
|
|
|
import * as utils from './string-formatting';
|
|
|
|
import { YupValidationError } from './errors';
|
|
|
|
import printValue from './print-value';
|
2020-02-14 11:42:03 +01:00
|
|
|
|
2022-06-01 14:04:22 +02:00
|
|
|
const MixedSchemaType = yup.MixedSchema;
|
2020-05-28 16:56:44 +02:00
|
|
|
|
2023-04-27 23:18:48 +02:00
|
|
|
const isNotNilTest = (value: unknown) => !_.isNil(value);
|
|
|
|
|
|
|
|
const isNotNullTest = (value: unknown) => !_.isNull(value);
|
2020-02-17 14:51:21 +01:00
|
|
|
|
2023-04-27 23:18:48 +02:00
|
|
|
yup.addMethod(yup.mixed, 'notNil', function isNotNill(msg = '${path} must be defined.') {
|
2020-02-18 09:02:48 +01:00
|
|
|
return this.test('defined', msg, isNotNilTest);
|
2023-04-27 23:18:48 +02:00
|
|
|
});
|
2020-02-17 12:04:17 +01:00
|
|
|
|
2023-04-27 23:18:48 +02:00
|
|
|
yup.addMethod(yup.mixed, 'notNull', function isNotNull(msg = '${path} cannot be null.') {
|
2020-02-18 09:02:48 +01:00
|
|
|
return this.test('defined', msg, isNotNullTest);
|
2023-04-27 23:18:48 +02:00
|
|
|
});
|
2020-02-18 09:02:48 +01:00
|
|
|
|
2023-04-27 23:18:48 +02:00
|
|
|
yup.addMethod(yup.mixed, 'isFunction', function isFunction(message = '${path} is not a function') {
|
2022-08-08 23:33:39 +02:00
|
|
|
return this.test(
|
|
|
|
'is a function',
|
|
|
|
message,
|
|
|
|
(value) => _.isUndefined(value) || _.isFunction(value)
|
|
|
|
);
|
2023-04-27 23:18:48 +02:00
|
|
|
});
|
2021-06-21 12:02:10 +02:00
|
|
|
|
2023-04-27 23:18:48 +02:00
|
|
|
yup.addMethod(
|
|
|
|
yup.string,
|
|
|
|
'isCamelCase',
|
|
|
|
function isCamelCase(message = '${path} is not in camel case (anExampleOfCamelCase)') {
|
|
|
|
return this.test('is in camelCase', message, (value) =>
|
|
|
|
value ? utils.isCamelCase(value) : false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
yup.addMethod(
|
|
|
|
yup.string,
|
|
|
|
'isKebabCase',
|
|
|
|
function isKebabCase(message = '${path} is not in kebab case (an-example-of-kebab-case)') {
|
|
|
|
return this.test('is in kebab-case', message, (value) =>
|
|
|
|
value ? utils.isKebabCase(value) : false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
yup.addMethod(
|
|
|
|
yup.object,
|
|
|
|
'onlyContainsFunctions',
|
|
|
|
function onlyContainsFunctions(message = '${path} contains values that are not functions') {
|
|
|
|
return this.test(
|
|
|
|
'only contains functions',
|
|
|
|
message,
|
|
|
|
(value) => _.isUndefined(value) || (value && Object.values(value).every(_.isFunction))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2019-11-04 14:47:58 +01:00
|
|
|
|
2020-05-28 16:56:44 +02:00
|
|
|
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);
|
2020-05-28 16:56:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 };
|
|
|
|
|
2022-08-08 23:33:39 +02:00
|
|
|
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 }) {
|
2022-08-08 15:50:34 +02:00
|
|
|
const isCast = originalValue != null && originalValue !== value;
|
|
|
|
const msg =
|
2021-11-04 10:54:13 +01:00
|
|
|
`${path} must be a \`${type}\` type, ` +
|
2022-08-08 15:50:34 +02:00
|
|
|
`but the final value was: \`${printValue(value, true)}\`${
|
|
|
|
isCast ? ` (cast from the value \`${printValue(originalValue, true)}\`).` : '.'
|
|
|
|
}`;
|
2021-11-04 10:54:13 +01:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-04-27 23:18:48 +02:00
|
|
|
export { yup, handleYupError, validateYupSchema, validateYupSchemaSync };
|