feat: add global slug and field names to schema validation errors

This commit is contained in:
Dan Ribbens
2021-07-21 16:23:59 -04:00
parent ebfb72c8fa
commit bb63b4aad1
4 changed files with 53 additions and 26 deletions

View File

@@ -1,5 +1,4 @@
import joi from 'joi';
import fieldSchema from '../../fields/config/schema';
const component = joi.alternatives().try(
joi.object().unknown(),
@@ -34,8 +33,7 @@ const collectionSchema = joi.object().keys({
preview: joi.func(),
disableDuplicate: joi.bool(),
}),
fields: joi.array()
.items(fieldSchema),
fields: joi.array(),
hooks: joi.object({
beforeOperation: joi.array().items(joi.func()),
beforeValidate: joi.array().items(joi.func()),

View File

@@ -1,5 +1,4 @@
import joi from 'joi';
import globalSchema from '../globals/config/schema';
const component = joi.alternatives().try(
joi.object().unknown(),
@@ -31,8 +30,7 @@ export default joi.object({
graphQLPlayground: joi.string(),
}),
collections: joi.array(),
globals: joi.array()
.items(globalSchema),
globals: joi.array(),
admin: joi.object({
user: joi.string(),
meta: joi.object()

View File

@@ -3,24 +3,53 @@ import collectionSchema from '../collections/config/schema';
import Logger from '../utilities/logger';
import { PayloadConfig, Config } from './types';
import { PayloadCollectionConfig } from '../collections/config/types';
import fieldSchema from '../fields/config/schema';
import { PayloadGlobalConfig } from '../globals/config/types';
import globalSchema from '../globals/config/schema';
const logger = Logger();
const validateCollection = (collections: PayloadCollectionConfig[]): string[] => {
const collectionResults = {};
collections.forEach((collection) => {
collectionResults[collection.slug] = collectionSchema.validate(collection, { abortEarly: false });
});
const collectionErrors: string[] = [];
Object.keys(collectionResults).forEach((slug) => {
if (collectionResults[slug].error) {
collectionResults[slug].error.details.forEach(({ message }) => {
collectionErrors.push(`Collection "${slug}" > ${message}`);
const validateFields = (context: string, collection: PayloadCollectionConfig): string[] => {
const errors: string[] = [];
collection.fields.forEach((field) => {
const result = fieldSchema.validate(field, { abortEarly: false });
if (result.error) {
result.error.details.forEach(({ message }) => {
errors.push(`${context} "${collection.slug}" > Field "${field.name}" > ${message}`);
});
}
});
return collectionErrors;
return errors;
};
const validateCollections = (collections: PayloadCollectionConfig[]): string[] => {
const errors: string[] = [];
collections.forEach((collection) => {
const result = collectionSchema.validate(collection, { abortEarly: false });
if (result.error) {
result.error.details.forEach(({ message }) => {
errors.push(`Collection "${collection.slug}" > ${message}`);
});
}
errors.push(...validateFields('Collection', collection));
});
return errors;
};
const validateGlobals = (globals: PayloadGlobalConfig[]): string[] => {
const errors: string[] = [];
globals.forEach((global) => {
const result = globalSchema.validate(global, { abortEarly: false });
if (result.error) {
result.error.details.forEach(({ message }) => {
errors.push(`Globals "${global.slug}" > ${message}`);
});
}
errors.push(...validateFields('Global', global));
});
return errors;
};
const validateSchema = (config: PayloadConfig): Config => {
@@ -28,10 +57,13 @@ const validateSchema = (config: PayloadConfig): Config => {
abortEarly: false,
});
const collectionErrors = validateCollection(config.collections);
const nestedErrors = [
...validateCollections(config.collections),
...validateGlobals(config.globals),
];
if (result.error || collectionErrors.length > 0) {
logger.error(`There were ${(result.error?.details?.length || 0) + collectionErrors.length} errors validating your Payload config`);
if (result.error || nestedErrors.length > 0) {
logger.error(`There were ${(result.error?.details?.length || 0) + nestedErrors.length} errors validating your Payload config`);
let i = 0;
if (result.error) {
@@ -40,7 +72,7 @@ const validateSchema = (config: PayloadConfig): Config => {
logger.error(`${i}: ${message}`);
});
}
collectionErrors.forEach((message) => {
nestedErrors.forEach((message) => {
i += 1;
logger.error(`${i}: ${message}`);
});

View File

@@ -1,7 +1,6 @@
import joi from 'joi';
import fieldSchema from '../../fields/config/schema';
const schema = joi.object().keys({
const globalSchema = joi.object().keys({
slug: joi.string().required(),
label: joi.string(),
hooks: joi.object({
@@ -15,7 +14,7 @@ const schema = joi.object().keys({
read: joi.func(),
update: joi.func(),
}),
fields: joi.array().items(fieldSchema),
fields: joi.array(),
}).unknown();
export default schema;
export default globalSchema;