feat: add collection slug to schema validation errors
This commit is contained in:
@@ -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({
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user