Improve static example validation message (#2246)

This commit is contained in:
Paul Melnikow
2018-11-01 19:36:25 -04:00
committed by GitHub
parent 52d642cf91
commit cdb4cb36a4

View File

@@ -119,29 +119,48 @@ class BaseService {
*/
static prepareExamples() {
return this.examples.map(
({
title,
query,
exampleUrl,
previewUrl,
urlPattern,
staticExample,
documentation,
keywords,
}) => {
if (!previewUrl && !staticExample) {
(
{
title,
query,
exampleUrl,
previewUrl,
urlPattern,
staticExample,
documentation,
keywords,
},
index
) => {
if (staticExample) {
if (!urlPattern) {
throw new Error(
`Static example for ${
this.name
} at index ${index} does not declare a urlPattern`
)
}
if (!exampleUrl) {
throw new Error(
`Static example for ${
this.name
} at index ${index} does not declare an exampleUrl`
)
}
if (previewUrl) {
throw new Error(
`Static example for ${
this.name
} at index ${index} also declares a dynamic previewUrl, which is not allowed`
)
}
} else if (!previewUrl) {
throw Error(
`Example for ${
this.name
} is missing required previewUrl or staticExample`
} at index ${index} is missing required previewUrl or staticExample`
)
}
if (staticExample && !urlPattern) {
throw new Error('Must declare a urlPattern if using staticExample')
}
if (staticExample && !exampleUrl) {
throw new Error('Must declare an exampleUrl if using staticExample')
}
const stringified = queryString.stringify(query)
const suffix = stringified ? `?${stringified}` : ''