Skip to content

Commit

Permalink
feat(core): custom keywords for stream and error
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Apr 26, 2021
1 parent 67a6a62 commit f4a3941
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/core/lib/plugins/tools/schema.js

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

6 changes: 6 additions & 0 deletions packages/core/src/plugins/tools/schema.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ schema. Thus, it mst be defined after every module which modify the config
object.
###

stream = require 'stream'
{merge} = require 'mixme'
Ajv = require('ajv').default
ajv_keywords = require 'ajv-keywords'
ajv_formats = require "ajv-formats"
utils = require '../../utils'

instanceofDef = require 'ajv-keywords/dist/definitions/instanceof'
instanceofDef.CONSTRUCTORS['Error'] = Error
instanceofDef.CONSTRUCTORS['stream.Writable'] = stream.Writable
instanceofDef.CONSTRUCTORS['stream.Readable'] = stream.Readable

parse = (uri) ->
matches = /^(\w+:)\/\/(.*)/.exec uri
throw utils.error 'NIKITA_SCHEMA_MALFORMED_URI', [
Expand Down
79 changes: 79 additions & 0 deletions packages/core/test/plugins/tools/schema.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

{tags} = require '../../test'
nikita = require '../../../src'
stream = require 'stream'

describe 'plugins.tools.schema', ->
return unless tags.api
Expand Down Expand Up @@ -63,6 +64,84 @@ describe 'plugins.tools.schema', ->
error = await action.tools.schema.validate action
error.code.should.eql 'NIKITA_SCHEMA_VALIDATION_CONFIG'

describe 'keywords', ->

it 'Error with valid property', ->
nikita ({registry}) ->
await registry.register ['test'],
metadata:
schema:
config:
type: 'object'
properties:
err:
instanceof: 'Error'
handler: ({config})-> config.err.message
@test err: new Error 'catchme'
.should.be.fulfilledWith 'catchme'

it 'Error with invalid property', ->
nikita ({registry}) ->
await registry.register ['test'],
metadata:
schema:
config:
type: 'object'
properties:
err:
instanceof: 'Error'
handler: ({config})-> config.err.message
@test err: 'catchme'
.should.be.rejectedWith
message: [
'NIKITA_SCHEMA_VALIDATION_CONFIG:'
'one error was found in the configuration of action `test`: #/definitions/config/properties/err/instanceof'
'config/err should pass "instanceof" keyword validation.'
].join ' '

it 'stream with valid property', ->
nikita ({registry}) ->
await registry.register ['test'],
metadata:
schema:
config:
type: 'object'
properties:
writable:
instanceof: 'stream.Writable'
readable:
instanceof: 'stream.Readable'
handler: ({config})-> 'ok'
@test
writable: new stream.Writable()
readable: new stream.Readable()
.should.be.fulfilledWith 'ok'

it 'stream with invalid property', ->
nikita ({registry}) ->
await registry.register ['test'],
metadata:
schema:
config:
type: 'object'
properties:
writable:
instanceof: 'stream.Writable'
readable:
instanceof: 'stream.Readable'
handler: ({config})-> config.err.message
@test
writable: 'invalid'
readable: 'invalid'
.should.be.rejectedWith
message: [
'NIKITA_SCHEMA_VALIDATION_CONFIG:'
'multiple errors where found in the configuration of action `test`:'
'#/definitions/config/properties/readable/instanceof config/readable should pass "instanceof" keyword validation;'
'#/definitions/config/properties/writable/instanceof'
'config/writable should pass "instanceof" keyword validation.'
].join ' '

describe '$ref', ->

it 'invalid ref definition', ->
Expand Down

0 comments on commit f4a3941

Please sign in to comment.