-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoptions.js
More file actions
57 lines (50 loc) · 1.84 KB
/
options.js
File metadata and controls
57 lines (50 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { getErrorMessage } from '@defra/forms-model'
import Joi from 'joi'
import { createLogger } from '~/src/server/common/helpers/logging/logger.js'
import { CacheService } from '~/src/server/services/index.js'
const logger = createLogger()
const pluginRegistrationOptionsSchema = Joi.object({
model: Joi.object().optional(),
services: Joi.object().optional(),
controllers: Joi.object().pattern(Joi.string(), Joi.any()).optional(),
cache: Joi.alternatives().try(
Joi.object().instance(CacheService),
Joi.string()
),
globals: Joi.object().pattern(Joi.string(), Joi.any()).optional(),
filters: Joi.object().pattern(Joi.string(), Joi.any()).optional(),
pluginPath: Joi.string().optional(),
nunjucks: Joi.object({
baseLayoutPath: Joi.string().required(),
paths: Joi.array().items(Joi.string()).required()
}).required(),
viewContext: Joi.function().required(),
preparePageEventRequestOptions: Joi.function().optional(),
onRequest: Joi.function().optional(),
baseUrl: Joi.string().uri().required(),
saveAndExit: Joi.function().optional(),
ordnanceSurveyApiKey: Joi.string().optional(),
ordnanceSurveyApiSecret: Joi.string().optional()
})
/**
* Validates the plugin options against the schema and returns the validated value.
* @param {PluginOptions} options
* @returns {PluginOptions}
*/
export function validatePluginOptions(options) {
const result = pluginRegistrationOptionsSchema.validate(options, {
abortEarly: false
})
if (result.error) {
logger.error(
result.error,
`Missing required properties in plugin options: ${getErrorMessage(result.error)}`
)
throw new Error('Invalid plugin options', result.error)
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return result.value
}
/**
* @import { PluginOptions } from '~/src/server/plugins/engine/types.js'
*/