mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-22 13:37:05 +02:00
refactor(content-blog): improve error message of authors map validation (#6909)
This commit is contained in:
parent
8c1e518ba2
commit
009d87cbd9
2 changed files with 33 additions and 22 deletions
|
@ -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."`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -17,20 +17,31 @@ import type {
|
|||
|
||||
export type AuthorsMap = Record<string, Author>;
|
||||
|
||||
const AuthorsMapSchema = Joi.object<AuthorsMap>().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<AuthorsMap>()
|
||||
.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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue