Skip to content

Commit

Permalink
feat(core): schema validation for disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Apr 26, 2021
1 parent f4a3941 commit 1f80590
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 46 deletions.
2 changes: 1 addition & 1 deletion docs/content/api/metadata/disabled.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ navtitle: disabled

# Metadata "disabled"

The `disabled` metadata property disables the execution of the action and consequently the execution of its child actions.
The `disabled` metadata property disables the execution of the current action and consequently the execution of its child actions.

* Type: `boolean`
* Default: `false`
Expand Down
20 changes: 19 additions & 1 deletion packages/core/lib/plugins/metadata/disabled.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion packages/core/src/plugins/metadata/disabled.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@

{mutate} = require 'mixme'

module.exports =
name: '@nikitajs/core/src/plugins/metadata/disabled'
hooks:
'nikita:schema': ({schema}) ->
mutate schema.definitions.metadata.properties,
disabled:
type: 'boolean'
description: '''
Disable the execution of the current action and consequently the
execution of its child actions.
'''
default: false
'nikita:action': (action, handler) ->
# Note, we dont enforce schema validation before it because
# When a plugin a disabled, chances are that not all its property
# where passed correctly and we don't want schema validation
# to throw an error in such cases
action.metadata.disabled ?= false
if action.metadata.disabled then null else handler
if action.metadata.disabled is true then null else handler
56 changes: 13 additions & 43 deletions packages/core/test/plugins/metadata/disabled.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ nikita = require '../../../src'
describe 'plugins.metadata.disabled', ->
return unless tags.api

it 'validate schema', ->
nikita
$disabled: [false, false]
$handler: (->)
.should.be.rejectedWith
code: 'NIKITA_SCHEMA_VALIDATION_CONFIG'
message: [
'NIKITA_SCHEMA_VALIDATION_CONFIG:'
'one error was found in the configuration of root action:'
'nikita#/definitions/metadata/properties/disabled/type metadata/disabled'
'should be boolean, type is "boolean".'
].join ' '

it 'default', ->
nikita.call
$disabled: false
Expand All @@ -21,46 +34,3 @@ describe 'plugins.metadata.disabled', ->
$disabled: false
$handler: -> 'called'
.should.be.resolvedWith 'called'

it.skip 'emit lifecycle event when disabled', ->
nikita
.call
disabled: true
, (otions) ->
throw Error 'Achtung'
.on 'lifecycle', (log) ->
Object.keys(log).sort().should.eql [
'depth', 'file',
'index', 'level',
'line', 'message',
'metadata', 'module',
'config', 'parent',
'time', 'type'
]
log.depth.should.eql 1
log.index.should.eql 0
log.level.should.eql 'INFO'
log.message.should.eql 'disabled_true'
log.metadata.headers.should.eql []
log.metadata.shy.should.be.false()
(log.module is undefined).should.be.true()
log.type.should.eql 'lifecycle'
.promise()

it.skip 'emit lifecycle event when not disabled', ->
nikita
.call
disabled: false
, ->
throw Error 'Achtung'
.on 'lifecycle', (log) ->
return if log.message is 'conditions_passed'
log.file.should.eql 'session.coffee.md'
log.index.should.eql 0
log.level.should.eql 'DEBUG'
log.metadata.headers.should.eql []
log.message.should.eql 'disabled_false'
(log.module is undefined).should.be.true()
log.type.should.eql 'lifecycle'
.next (->)
.promise()

0 comments on commit 1f80590

Please sign in to comment.