strapi/packages/core/utils/src/validators.ts

194 lines
5.3 KiB
TypeScript
Raw Normal View History

2023-08-01 21:01:49 +02:00
/* eslint-disable @typescript-eslint/ban-ts-comment */
2023-04-27 23:18:48 +02:00
/* eslint-disable no-template-curly-in-string */
import * as yup from 'yup';
import _ from 'lodash';
2023-07-18 14:48:42 +02:00
import { defaults, isNumber, isInteger, get } from 'lodash/fp';
2023-04-27 23:18:48 +02:00
import * as utils from './string-formatting';
import { YupValidationError } from './errors';
import printValue from './print-value';
2023-08-01 21:01:49 +02:00
export * as yup from 'yup';
2022-06-01 14:04:22 +02:00
const MixedSchemaType = yup.MixedSchema;
2023-04-27 23:18:48 +02:00
const isNotNilTest = (value: unknown) => !_.isNil(value);
const isNotNullTest = (value: unknown) => !_.isNull(value);
2023-04-27 23:18:48 +02:00
yup.addMethod(yup.mixed, 'notNil', function isNotNill(msg = '${path} must be defined.') {
return this.test('defined', msg, isNotNilTest);
2023-04-27 23:18:48 +02:00
});
2023-04-27 23:18:48 +02:00
yup.addMethod(yup.mixed, 'notNull', function isNotNull(msg = '${path} cannot be null.') {
return this.test('defined', msg, isNotNullTest);
2023-04-27 23:18:48 +02: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) : true
2023-04-27 23:18:48 +02:00
);
}
);
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) : true
2023-04-27 23:18:48 +02:00
);
}
);
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))
);
}
);
2023-07-18 14:48:42 +02:00
yup.addMethod(
yup.array,
'uniqueProperty',
function uniqueProperty(propertyName: string, message: string) {
return this.test('unique', message, function unique(list) {
const errors: yup.ValidationError[] = [];
list?.forEach((element, index) => {
const sameElements = list.filter(
(e) => get(propertyName, e) === get(propertyName, element)
);
if (sameElements.length > 1) {
errors.push(
this.createError({
path: `${this.path}[${index}].${propertyName}`,
message,
})
);
}
});
if (errors.length) {
throw new yup.ValidationError(errors);
}
return true;
});
}
);
class StrapiIDSchema extends MixedSchemaType {
constructor() {
super({ type: 'strapiID' });
}
2023-06-05 14:01:39 +02:00
_typeCheck(value: unknown): value is string | number {
return typeof value === 'string' || (isNumber(value) && isInteger(value) && value >= 0);
}
}
2023-08-01 21:01:49 +02:00
// @ts-ignore
yup.strapiID = (): InstanceType<typeof StrapiIDSchema> => new StrapiIDSchema();
2023-06-05 14:01:39 +02:00
const handleYupError = (error: yup.ValidationError, errorMessage: string) => {
2021-11-03 19:31:57 +01:00
throw new YupValidationError(error, errorMessage);
};
const defaultValidationParam = { strict: true, abortEarly: false };
2022-08-08 23:33:39 +02:00
const validateYupSchema =
2023-06-05 14:01:39 +02:00
(schema: yup.AnySchema, options = {}) =>
async (body: unknown, errorMessage: string) => {
2022-08-08 23:33:39 +02:00
try {
const optionsWithDefaults = defaults(defaultValidationParam, options);
2023-04-28 18:20:31 +02:00
const result = await schema.validate(body, optionsWithDefaults);
return result;
2022-08-08 23:33:39 +02:00
} catch (e) {
2023-06-05 14:01:39 +02:00
if (e instanceof yup.ValidationError) {
handleYupError(e, errorMessage);
}
throw e;
2022-08-08 23:33:39 +02:00
}
};
const validateYupSchemaSync =
2023-06-05 14:01:39 +02:00
(schema: yup.AnySchema, options = {}) =>
(body: unknown, errorMessage: string) => {
2022-08-08 23:33:39 +02:00
try {
const optionsWithDefaults = defaults(defaultValidationParam, options);
return schema.validateSync(body, optionsWithDefaults);
} catch (e) {
2023-06-05 14:01:39 +02:00
if (e instanceof yup.ValidationError) {
handleYupError(e, errorMessage);
}
throw e;
2022-08-08 23:33:39 +02:00
}
};
2021-10-27 18:54:58 +02:00
2023-06-05 14:01:39 +02:00
interface NoTypeOptions {
path: string;
type: string;
value: unknown;
originalValue: unknown;
}
2021-11-04 10:54:13 +01:00
// Temporary fix of this issue : https://github.com/jquense/yup/issues/616
yup.setLocale({
mixed: {
2023-06-05 14:01:39 +02:00
notType(options: NoTypeOptions) {
const { path, type, value, originalValue } = options;
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-08-01 21:01:49 +02:00
declare module 'yup' {
const strapiID: () => InstanceType<typeof StrapiIDSchema>;
2023-06-06 10:58:36 +02:00
2023-08-01 21:01:49 +02:00
interface BaseSchema {
isFunction(message?: string): this;
notNil(message?: string): this;
notNull(message?: string): this;
}
interface StringSchema {
isCamelCase(message?: string): this;
isKebabCase(message?: string): this;
}
interface ObjectSchema<TShape> {
onlyContainsFunctions(message?: string): this;
}
}
export { StrapiIDSchema, handleYupError, validateYupSchema, validateYupSchemaSync };