Files
shields/services/validate-example.js
Paul Melnikow 84a5be3946 Declare static examples using namedParams (#2308)
This continues the work from #2279, by allowing example badges to be specified using `namedParams`. Using an object makes it possible for us to display these in form fields down the line. (#701)

I've called this the "preferred" way, and labeled the other ways deprecated. I've also added some doc to the `examples` property in BaseService. Then I realized we had some doc in the tutorial, though I think it's fine to have a short version in the tutorial, and the gory detail in BaseService.

I've also added a `pattern` keyword, and made `urlPattern` an alias.

Closes #2050.
2018-11-17 09:47:25 -05:00

69 lines
1.4 KiB
JavaScript

'use strict'
module.exports = function validateExample(
{
title,
query,
namedParams,
exampleUrl,
previewUrl,
pattern,
urlPattern,
staticExample,
documentation,
keywords,
},
index,
ServiceClass
) {
pattern = pattern || urlPattern || ServiceClass.route.pattern
if (staticExample) {
if (!pattern) {
throw new Error(
`Static example for ${
ServiceClass.name
} at index ${index} does not declare a pattern`
)
}
if (namedParams && exampleUrl) {
throw new Error(
`Static example for ${
ServiceClass.name
} at index ${index} declares both namedParams and exampleUrl`
)
} else if (!namedParams && !exampleUrl) {
throw new Error(
`Static example for ${
ServiceClass.name
} at index ${index} does not declare namedParams nor exampleUrl`
)
}
if (previewUrl) {
throw new Error(
`Static example for ${
ServiceClass.name
} at index ${index} also declares a dynamic previewUrl, which is not allowed`
)
}
} else if (!previewUrl) {
throw Error(
`Example for ${
ServiceClass.name
} at index ${index} is missing required previewUrl or staticExample`
)
}
return {
title,
query,
namedParams,
exampleUrl,
previewUrl,
pattern,
staticExample,
documentation,
keywords,
}
}