feat: add collection slug to schema validation errors

This commit is contained in:
Dan Ribbens
2021-07-21 13:25:04 -04:00
parent fe54837e73
commit ebfb72c8fa
2 changed files with 35 additions and 7 deletions

View File

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

View File

@@ -1,24 +1,54 @@
import schema from './schema';
import collectionSchema from '../collections/config/schema';
import Logger from '../utilities/logger';
import { PayloadConfig, Config } from './types';
import { PayloadCollectionConfig } from '../collections/config/types';
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}`);
});
}
});
return collectionErrors;
};
const validateSchema = (config: PayloadConfig): Config => {
const result = schema.validate(config, {
abortEarly: false,
});
if (result.error) {
logger.error(`There were ${result.error.details.length} errors validating your Payload config`);
const collectionErrors = validateCollection(config.collections);
result.error.details.forEach(({ message }, i) => {
logger.error(`${i + 1}: ${message}`);
if (result.error || collectionErrors.length > 0) {
logger.error(`There were ${(result.error?.details?.length || 0) + collectionErrors.length} errors validating your Payload config`);
let i = 0;
if (result.error) {
result.error.details.forEach(({ message }) => {
i += 1;
logger.error(`${i}: ${message}`);
});
}
collectionErrors.forEach((message) => {
i += 1;
logger.error(`${i}: ${message}`);
});
process.exit(1);
}
return result.value as Config;
};