From 009d87cbd940809d8d31c9837f81e78fa8d49a02 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Mon, 14 Mar 2022 08:16:12 +0800 Subject: [PATCH] refactor(content-blog): improve error message of authors map validation (#6909) --- .../src/__tests__/authors.test.ts | 16 ++++---- .../src/authors.ts | 39 ++++++++++++------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts b/packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts index 5c159b7bbd..0fde188e85 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts @@ -381,7 +381,9 @@ describe('validateAuthorsMap', () => { validateAuthorsMap({ slorber: undefined, }), - ).toThrowErrorMatchingInlineSnapshot(`"\\"slorber\\" is required"`); + ).toThrowErrorMatchingInlineSnapshot( + `"\\"slorber\\" cannot be undefined. It should be an author object containing properties like name, title, and imageURL."`, + ); }); it('reject null author', () => { @@ -390,7 +392,7 @@ describe('validateAuthorsMap', () => { slorber: null, }), ).toThrowErrorMatchingInlineSnapshot( - `"\\"slorber\\" must be of type object"`, + `"\\"slorber\\" should be an author object containing properties like name, title, and imageURL."`, ); }); @@ -398,14 +400,13 @@ describe('validateAuthorsMap', () => { expect(() => validateAuthorsMap({slorber: []}), ).toThrowErrorMatchingInlineSnapshot( - `"\\"slorber\\" must be of type object"`, + `"\\"slorber\\" should be an author object containing properties like name, title, and imageURL."`, ); }); it('reject array content', () => { expect(() => validateAuthorsMap([])).toThrowErrorMatchingInlineSnapshot( - // TODO improve this error message - `"\\"value\\" must be of type object"`, + `"The authors map file should contain an object where each entry contains an author key and the corresponding author's data."`, ); }); @@ -413,8 +414,7 @@ describe('validateAuthorsMap', () => { expect(() => validateAuthorsMap({name: 'Sébastien'}), ).toThrowErrorMatchingInlineSnapshot( - // TODO improve this error message - `"\\"name\\" must be of type object"`, + `"\\"name\\" should be an author object containing properties like name, title, and imageURL."`, ); }); @@ -426,7 +426,7 @@ describe('validateAuthorsMap', () => { expect(() => validateAuthorsMap(authorsMap), ).toThrowErrorMatchingInlineSnapshot( - `"\\"slorber\\" must be of type object"`, + `"\\"slorber\\" should be an author object containing properties like name, title, and imageURL."`, ); }); }); diff --git a/packages/docusaurus-plugin-content-blog/src/authors.ts b/packages/docusaurus-plugin-content-blog/src/authors.ts index 566da9a46a..9e234d86d9 100644 --- a/packages/docusaurus-plugin-content-blog/src/authors.ts +++ b/packages/docusaurus-plugin-content-blog/src/authors.ts @@ -17,20 +17,31 @@ import type { export type AuthorsMap = Record; -const AuthorsMapSchema = Joi.object().pattern( - Joi.string(), - Joi.object({ - name: Joi.string(), - url: URISchema, - imageURL: URISchema, - title: Joi.string(), - email: Joi.string(), - }) - .rename('image_url', 'imageURL') - .or('name', 'imageURL') - .unknown() - .required(), -); +const AuthorsMapSchema = Joi.object() + .pattern( + Joi.string(), + Joi.object({ + name: Joi.string(), + url: URISchema, + imageURL: URISchema, + title: Joi.string(), + email: Joi.string(), + }) + .rename('image_url', 'imageURL') + .or('name', 'imageURL') + .unknown() + .required() + .messages({ + 'object.base': + '{#label} should be an author object containing properties like name, title, and imageURL.', + 'any.required': + '{#label} cannot be undefined. It should be an author object containing properties like name, title, and imageURL.', + }), + ) + .messages({ + 'object.base': + "The authors map file should contain an object where each entry contains an author key and the corresponding author's data.", + }); export function validateAuthorsMap(content: unknown): AuthorsMap { return Joi.attempt(content, AuthorsMapSchema);