refactor(content-blog): improve error message of authors map validation (#6909)

This commit is contained in:
Joshua Chen 2022-03-14 08:16:12 +08:00 committed by GitHub
parent 8c1e518ba2
commit 009d87cbd9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 22 deletions

View file

@ -381,7 +381,9 @@ describe('validateAuthorsMap', () => {
validateAuthorsMap({ validateAuthorsMap({
slorber: undefined, 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', () => { it('reject null author', () => {
@ -390,7 +392,7 @@ describe('validateAuthorsMap', () => {
slorber: null, slorber: null,
}), }),
).toThrowErrorMatchingInlineSnapshot( ).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(() => expect(() =>
validateAuthorsMap({slorber: []}), validateAuthorsMap({slorber: []}),
).toThrowErrorMatchingInlineSnapshot( ).toThrowErrorMatchingInlineSnapshot(
`"\\"slorber\\" must be of type object"`, `"\\"slorber\\" should be an author object containing properties like name, title, and imageURL."`,
); );
}); });
it('reject array content', () => { it('reject array content', () => {
expect(() => validateAuthorsMap([])).toThrowErrorMatchingInlineSnapshot( expect(() => validateAuthorsMap([])).toThrowErrorMatchingInlineSnapshot(
// TODO improve this error message `"The authors map file should contain an object where each entry contains an author key and the corresponding author's data."`,
`"\\"value\\" must be of type object"`,
); );
}); });
@ -413,8 +414,7 @@ describe('validateAuthorsMap', () => {
expect(() => expect(() =>
validateAuthorsMap({name: 'Sébastien'}), validateAuthorsMap({name: 'Sébastien'}),
).toThrowErrorMatchingInlineSnapshot( ).toThrowErrorMatchingInlineSnapshot(
// TODO improve this error message `"\\"name\\" should be an author object containing properties like name, title, and imageURL."`,
`"\\"name\\" must be of type object"`,
); );
}); });
@ -426,7 +426,7 @@ describe('validateAuthorsMap', () => {
expect(() => expect(() =>
validateAuthorsMap(authorsMap), validateAuthorsMap(authorsMap),
).toThrowErrorMatchingInlineSnapshot( ).toThrowErrorMatchingInlineSnapshot(
`"\\"slorber\\" must be of type object"`, `"\\"slorber\\" should be an author object containing properties like name, title, and imageURL."`,
); );
}); });
}); });

View file

@ -17,20 +17,31 @@ import type {
export type AuthorsMap = Record<string, Author>; export type AuthorsMap = Record<string, Author>;
const AuthorsMapSchema = Joi.object<AuthorsMap>().pattern( const AuthorsMapSchema = Joi.object<AuthorsMap>()
Joi.string(), .pattern(
Joi.object({ Joi.string(),
name: Joi.string(), Joi.object({
url: URISchema, name: Joi.string(),
imageURL: URISchema, url: URISchema,
title: Joi.string(), imageURL: URISchema,
email: Joi.string(), title: Joi.string(),
}) email: Joi.string(),
.rename('image_url', 'imageURL') })
.or('name', 'imageURL') .rename('image_url', 'imageURL')
.unknown() .or('name', 'imageURL')
.required(), .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 { export function validateAuthorsMap(content: unknown): AuthorsMap {
return Joi.attempt(content, AuthorsMapSchema); return Joi.attempt(content, AuthorsMapSchema);