Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow no validation #32

Merged
merged 4 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,14 @@
* @param {object} options options to load Config
* @param {boolean} options.allowNoImpl do not throw if there is no implementation
* @param {boolean} options.ignoreAioConfig do not load .aio config via aio-lib-core-config, which is loaded synchronously and blocks the main thread.
* @param {boolean} [options.validateAppConfig=true] set to false to not validate
* @returns {object} the config
*/
async function load (options = {}) {
const allowNoImpl = options.allowNoImpl === undefined ? false : options.allowNoImpl
const ignoreAioConfig = options.ignoreAioConfig === undefined ? false : options.ignoreAioConfig
const validateAppConfig = options.validateAppConfig === undefined ? true : options.validateAppConfig
shazron marked this conversation as resolved.
Show resolved Hide resolved

// *NOTE* it would be nice to support an appFolder option to load config from a different folder.
// However, this requires to update aio-lib-core-config to support loading
// from a different folder aswell (or enforcing ignore).
Expand All @@ -164,7 +167,9 @@
// this will resolve $include directives and output the app config into a single object
// paths config values in $included files will be rewritten
const appConfigWithIndex = await coalesce(defaults.USER_CONFIG_FILE, { absolutePaths: true })
await validate(appConfigWithIndex.config, { throws: true })
if (validateAppConfig) {
await validate(appConfigWithIndex.config, { throws: true })
}
const mergedAppConfig = await mergeLegacyAppConfig(appConfigWithIndex, legacyAppConfigWithIndex)

appConfig = mergedAppConfig.config
Expand Down Expand Up @@ -192,7 +197,7 @@
}
}

/**

Check warning on line 200 in src/index.js

View workflow job for this annotation

GitHub Actions / build (18.x, ubuntu-latest)

Missing JSDoc @returns declaration

Check warning on line 200 in src/index.js

View workflow job for this annotation

GitHub Actions / build (18.x, windows-latest)

Missing JSDoc @returns declaration

Check warning on line 200 in src/index.js

View workflow job for this annotation

GitHub Actions / build (20.x, ubuntu-latest)

Missing JSDoc @returns declaration

Check warning on line 200 in src/index.js

View workflow job for this annotation

GitHub Actions / build (20.x, windows-latest)

Missing JSDoc @returns declaration
* Validates the app configuration.
* To validate an app.config.yaml file, use `await validate(await coalesce('app.config.yaml'))`
*
Expand Down Expand Up @@ -589,7 +594,13 @@
// Let's search the config path that defines a key in the same config object level as 'web' or
// 'action'
const otherKeyInObject = Object.keys(singleUserConfig)[0]
const configFilePath = includeIndex[`${fullKeyPrefix}.${otherKeyInObject}`].file
let configFilePath
if (otherKeyInObject === undefined) {
// corner case if config is empty e.g. application: {}
configFilePath = includeIndex[`${fullKeyPrefix}`].file
} else {
configFilePath = includeIndex[`${fullKeyPrefix}.${otherKeyInObject}`].file
}

const defaultActionPath = resolveToRoot('actions/', configFilePath)
const defaultWebPath = resolveToRoot('web-src/', configFilePath)
Expand Down
49 changes: 49 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,20 @@ extensions:
await expect(appConfig.load({})).rejects.toThrow('must have required property \'packages\'')
})

test('app config with empty application implementation', async () => {
global.loadFixtureApp('app')
global.fakeFileSystem.addJson({
'/package.json': '{}',
'app.config.yaml':
`
application: {}
`
})
config = await appConfig.load()
expect(config.all.application).toBeDefined()
expect(config.implements).toEqual(['application'])
})

// options
test('standalone app config - ignoreAioConfig=true', async () => {
global.loadFixtureApp('app')
Expand All @@ -281,6 +295,41 @@ extensions:
expect(config).toEqual(mockConfig)
})

test('invalid app config with validateAppConfig=true', async () => {
global.loadFixtureApp('app')
global.fakeFileSystem.addJson({
'/package.json': '{}',
'app.config.yaml':
`
application: {
web: { notallowed: true }
}
`
})
await expect(appConfig.load({ validateAppConfig: true })).rejects.toThrow('Missing or invalid keys in app.config.yaml')
})

test('invalid app config with validateAppConfig=false', async () => {
global.loadFixtureApp('app')
global.fakeFileSystem.addJson({
'/package.json': '{}',
'app.config.yaml':
`
application: {
web: { notallowed: true }
}
`
})
config = await appConfig.load({ validateAppConfig: false })
// the notallowed config is not picked up
expect(config.all.application.web).toEqual({
distDev: winCompat('/dist/application/web-dev'),
distProd: winCompat('/dist/application/web-prod'),
injectedConfig: winCompat('/web-src/src/config.json'),
src: winCompat('/web-src')
})
})

test('no implementation - allowNoImpl=false', async () => {
global.fakeFileSystem.addJson({ '/package.json': '{}' })
await expect(appConfig.load({})).rejects.toThrow('Couldn\'t find configuration')
Expand Down
Loading