From 1f8059002183c394231e5b956ac5f1729de07e33 Mon Sep 17 00:00:00 2001 From: David Worms Date: Tue, 30 Mar 2021 11:43:15 +0200 Subject: [PATCH] feat(core): schema validation for disabled --- docs/content/api/metadata/disabled.md | 2 +- .../core/lib/plugins/metadata/disabled.js | 20 ++++++- .../core/src/plugins/metadata/disabled.coffee | 17 +++++- .../test/plugins/metadata/disabled.coffee | 56 +++++-------------- 4 files changed, 49 insertions(+), 46 deletions(-) diff --git a/docs/content/api/metadata/disabled.md b/docs/content/api/metadata/disabled.md index 386053b94..072081501 100644 --- a/docs/content/api/metadata/disabled.md +++ b/docs/content/api/metadata/disabled.md @@ -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` diff --git a/packages/core/lib/plugins/metadata/disabled.js b/packages/core/lib/plugins/metadata/disabled.js index 75fec26cf..7b80c1f2e 100644 --- a/packages/core/lib/plugins/metadata/disabled.js +++ b/packages/core/lib/plugins/metadata/disabled.js @@ -1,13 +1,31 @@ // Generated by CoffeeScript 2.5.1 +var mutate; + +({mutate} = require('mixme')); + module.exports = { name: '@nikitajs/core/lib/plugins/metadata/disabled', hooks: { + 'nikita:schema': function({schema}) { + return 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': function(action, handler) { var base; + // 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 if ((base = action.metadata).disabled == null) { base.disabled = false; } - if (action.metadata.disabled) { + if (action.metadata.disabled === true) { return null; } else { return handler; diff --git a/packages/core/src/plugins/metadata/disabled.coffee b/packages/core/src/plugins/metadata/disabled.coffee index b98f8135c..63a088254 100644 --- a/packages/core/src/plugins/metadata/disabled.coffee +++ b/packages/core/src/plugins/metadata/disabled.coffee @@ -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 diff --git a/packages/core/test/plugins/metadata/disabled.coffee b/packages/core/test/plugins/metadata/disabled.coffee index 85202764a..ed8bc8470 100644 --- a/packages/core/test/plugins/metadata/disabled.coffee +++ b/packages/core/test/plugins/metadata/disabled.coffee @@ -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 @@ -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()