Skip to content

Commit

Permalink
ignore $schema definition (#1324)
Browse files Browse the repository at this point in the history
* ignore $schema definition

* added tests that verify $schema node is ignored

* reduced schema ignore tests (copied from other tests)

---------

Co-authored-by: Eric Newton <[email protected]>
  • Loading branch information
ericnewton76 and ericnewton-cst authored Mar 21, 2023
1 parent b9b2c49 commit 64f8698
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
28 changes: 28 additions & 0 deletions __tests__/server/schema-ignore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const request = require('supertest')
const jsonServer = require('../../src/server')

describe('$schema-ignore', () => {
let server
let router
let db

beforeEach(() => {
db = {
$schema: 'http://some.schema.somewhere/',
}

db.user = {
name: 'foo',
email: '[email protected]',
}

server = jsonServer.create()
router = jsonServer.router(db)
server.use(jsonServer.defaults())
server.use(router)
})

test('doesnt error with $schema node', () => {
return request(server).get('/user').expect(200, db.user)
})
})
3 changes: 3 additions & 0 deletions src/cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ function prettyPrint(argv, object, rules) {
console.log()
console.log(chalk.bold(' Resources'))
for (const prop in object) {
// skip printing $schema nodes
if (prop === '$schema') continue

console.log(` ${root}/${prop}`)
}

Expand Down
5 changes: 5 additions & 0 deletions src/server/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ module.exports = (db, opts) => {

// Create routes
db.forEach((value, key) => {
if (key === '$schema') {
// ignore $schema
return
}

if (_.isPlainObject(value)) {
router.use(`/${key}`, singular(db, key, opts))
return
Expand Down

0 comments on commit 64f8698

Please sign in to comment.