Skip to content

Commit 754ff98

Browse files
authored
feat: allow no validation (#32)
* feat: allow no validation * fix windows tests * review comment
1 parent 0dc89bd commit 754ff98

2 files changed

Lines changed: 64 additions & 4 deletions

File tree

src/index.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,16 @@ const cloneDeep = require('lodash.clonedeep')
137137
* }
138138
*
139139
* @param {object} options options to load Config
140-
* @param {boolean} options.allowNoImpl do not throw if there is no implementation
141-
* @param {boolean} options.ignoreAioConfig do not load .aio config via aio-lib-core-config, which is loaded synchronously and blocks the main thread.
140+
* @param {boolean} [options.allowNoImpl=false] do not throw if there is no implementation
141+
* @param {boolean} [options.ignoreAioConfig=false] do not load .aio config via aio-lib-core-config, which is loaded synchronously and blocks the main thread.
142+
* @param {boolean} [options.validateAppConfig=true] set to false to not validate
142143
* @returns {object} the config
143144
*/
144145
async function load (options = {}) {
145146
const allowNoImpl = options.allowNoImpl === undefined ? false : options.allowNoImpl
146147
const ignoreAioConfig = options.ignoreAioConfig === undefined ? false : options.ignoreAioConfig
148+
const validateAppConfig = options.validateAppConfig === undefined ? true : options.validateAppConfig
149+
147150
// *NOTE* it would be nice to support an appFolder option to load config from a different folder.
148151
// However, this requires to update aio-lib-core-config to support loading
149152
// from a different folder aswell (or enforcing ignore).
@@ -164,7 +167,9 @@ async function load (options = {}) {
164167
// this will resolve $include directives and output the app config into a single object
165168
// paths config values in $included files will be rewritten
166169
const appConfigWithIndex = await coalesce(defaults.USER_CONFIG_FILE, { absolutePaths: true })
167-
await validate(appConfigWithIndex.config, { throws: true })
170+
if (validateAppConfig) {
171+
await validate(appConfigWithIndex.config, { throws: true })
172+
}
168173
const mergedAppConfig = await mergeLegacyAppConfig(appConfigWithIndex, legacyAppConfigWithIndex)
169174

170175
appConfig = mergedAppConfig.config
@@ -589,7 +594,13 @@ async function buildSingleConfig (configName, singleUserConfig, commonConfig, in
589594
// Let's search the config path that defines a key in the same config object level as 'web' or
590595
// 'action'
591596
const otherKeyInObject = Object.keys(singleUserConfig)[0]
592-
const configFilePath = includeIndex[`${fullKeyPrefix}.${otherKeyInObject}`].file
597+
let configFilePath
598+
if (otherKeyInObject === undefined) {
599+
// corner case if config is empty e.g. application: {}
600+
configFilePath = includeIndex[`${fullKeyPrefix}`].file
601+
} else {
602+
configFilePath = includeIndex[`${fullKeyPrefix}.${otherKeyInObject}`].file
603+
}
593604

594605
const defaultActionPath = resolveToRoot('actions/', configFilePath)
595606
const defaultWebPath = resolveToRoot('web-src/', configFilePath)

test/index.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,20 @@ extensions:
273273
await expect(appConfig.load({})).rejects.toThrow('must have required property \'packages\'')
274274
})
275275

276+
test('app config with empty application implementation', async () => {
277+
global.loadFixtureApp('app')
278+
global.fakeFileSystem.addJson({
279+
'/package.json': '{}',
280+
'app.config.yaml':
281+
`
282+
application: {}
283+
`
284+
})
285+
config = await appConfig.load()
286+
expect(config.all.application).toBeDefined()
287+
expect(config.implements).toEqual(['application'])
288+
})
289+
276290
// options
277291
test('standalone app config - ignoreAioConfig=true', async () => {
278292
global.loadFixtureApp('app')
@@ -281,6 +295,41 @@ extensions:
281295
expect(config).toEqual(mockConfig)
282296
})
283297

298+
test('invalid app config with validateAppConfig=true', async () => {
299+
global.loadFixtureApp('app')
300+
global.fakeFileSystem.addJson({
301+
'/package.json': '{}',
302+
'app.config.yaml':
303+
`
304+
application: {
305+
web: { notallowed: true }
306+
}
307+
`
308+
})
309+
await expect(appConfig.load({ validateAppConfig: true })).rejects.toThrow('Missing or invalid keys in app.config.yaml')
310+
})
311+
312+
test('invalid app config with validateAppConfig=false', async () => {
313+
global.loadFixtureApp('app')
314+
global.fakeFileSystem.addJson({
315+
'/package.json': '{}',
316+
'app.config.yaml':
317+
`
318+
application: {
319+
web: { notallowed: true }
320+
}
321+
`
322+
})
323+
config = await appConfig.load({ validateAppConfig: false })
324+
// the notallowed config is not picked up
325+
expect(config.all.application.web).toEqual({
326+
distDev: winCompat('/dist/application/web-dev'),
327+
distProd: winCompat('/dist/application/web-prod'),
328+
injectedConfig: winCompat('/web-src/src/config.json'),
329+
src: winCompat('/web-src')
330+
})
331+
})
332+
284333
test('no implementation - allowNoImpl=false', async () => {
285334
global.fakeFileSystem.addJson({ '/package.json': '{}' })
286335
await expect(appConfig.load({})).rejects.toThrow('Couldn\'t find configuration')

0 commit comments

Comments
 (0)