Skip to content

Commit

Permalink
fix(core): reject errors when $ref is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Feb 11, 2021
1 parent e426319 commit f8fd133
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
16 changes: 13 additions & 3 deletions packages/core/lib/plugins/schema.js

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

18 changes: 15 additions & 3 deletions packages/core/src/plugins/schema.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,24 @@ module.exports =
{protocol, pathname} = parse uri
switch protocol
when 'module:'
action = require.main.require pathname
accept action.metadata.schema
try
action = require.main.require pathname
accept action.metadata.schema
catch err
reject error 'NIKITA_SCHEMA_INVALID_MODULE', [
'the module location is not resolvable,'
"module name is #{JSON.stringify pathname}."
]
when 'registry:'
module = pathname.split '/'
action = await action.registry.get module
accept action.metadata.schema
if action
accept action.metadata.schema
else
reject error 'NIKITA_SCHEMA_UNREGISTERED_ACTION', [
'the action is not registered inside the Nikita registry,'
"action namespace is #{JSON.stringify module.join '.'}."
]
ajv_keywords ajv
ajv_formats ajv
action.tools.schema =
Expand Down
38 changes: 36 additions & 2 deletions packages/core/test/plugins/schema.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,25 @@ describe 'plugins.schema', ->
'got true in root action.'
].join ' '

describe '$ref', ->
describe '$ref module', ->

it 'invalid ref location', ->
nikita.call
an_object: an_integer: 'abc'
metadata: schema:
type: 'object'
properties:
'an_object': $ref: 'registry://invalid/action'
, (->)
.should.be.rejectedWith
code: 'NIKITA_SCHEMA_UNREGISTERED_ACTION'
message: [
'NIKITA_SCHEMA_UNREGISTERED_ACTION:'
'the action is not registered inside the Nikita registry,'
'action namespace is "invalid.action".'
].join ' '

describe '$ref registry', ->

it 'valid', ->
nikita
Expand All @@ -174,7 +192,23 @@ describe 'plugins.schema', ->
'an_object': $ref: 'registry://test/schema'
handler: (->)

it 'invalid', ->
it 'invalid ref location', ->
nikita.call
an_object: an_integer: 'abc'
metadata: schema:
type: 'object'
properties:
'an_object': $ref: 'registry://invalid/action'
, (->)
.should.be.rejectedWith
code: 'NIKITA_SCHEMA_UNREGISTERED_ACTION'
message: [
'NIKITA_SCHEMA_UNREGISTERED_ACTION:'
'the action is not registered inside the Nikita registry,'
'action namespace is "invalid.action".'
].join ' '

it 'invalid ref definition', ->
nikita
.registry.register ['test', 'schema'],
metadata: schema:
Expand Down

0 comments on commit f8fd133

Please sign in to comment.