Created
December 15, 2016 22:17
-
-
Save sirgallifrey/85e0bb4d800f7692d8e6e5962565ad2a to your computer and use it in GitHub Desktop.
Hapi query validation of array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const Hapi = require('hapi'); | |
const Joi = require('joi'); | |
const server = new Hapi.Server(); | |
server.connection({ port: 3000 }); | |
server.route({ | |
method: 'GET', | |
path: '/', | |
config: { | |
handler: function(request, reply) { | |
reply('ok'); | |
}, | |
validate: { | |
query: Joi.object().keys({ | |
key: Joi.array().items(Joi.number().integer()).optional() | |
}) | |
} | |
} | |
}); | |
server.start((err) => { | |
if (err) { | |
throw err; | |
} | |
console.log('Server running'); | |
}); | |
exports = module.exports = server; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// Load modules | |
const Code = require('code'); | |
const Lab = require('lab'); | |
// require our server | |
const Server = require('./server'); | |
// Test shortcuts | |
const lab = exports.lab = Lab.script(); | |
const describe = lab.describe; | |
const it = lab.it; | |
const expect = Code.expect; | |
const querystring = require('querystring'); | |
describe('Query validation', () => { | |
it('query array', (done) => { | |
Server.inject({ method: 'GET', url:'/?key=[1,2,3]' }, (res) => { | |
expect(res.statusCode).to.be.equal(200); | |
done(); | |
}); | |
}); | |
it('query stringified array', (done) => { | |
const query = querystring.stringify({key: [1,2,3]}); | |
Server.inject({ method: 'GET', url:'/?' + query }, (res) => { | |
expect(res.statusCode).to.be.equal(200); | |
done(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment