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({
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."`,
);
});
});

View file

@ -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);