<commit_before>var schema = require('./schema')
  , validateConnection = require('app/lib/validate-connection')

module.exports = function (ServerModel) {
  return function (server, options, cb) {
    server.route(
      { method: 'POST'
      , path: '/'
      , handler: function (req, reply) {
          validateConnection(req.payload, req.payload.tables, function (error) {
            if (error) return reply.badRequest(error.message)

            ServerModel
              .forge(req.payload)
              .save()
              .then(function (server) {
                reply(server.toJSON())
              })
              .catch(reply)
          })
        }
      , config:
        { validate:
          { payload: schema.create
          }
        }
      })

    server.route(
      { method: 'GET'
      , path: '/'
      , handler: function (req, reply) {
          ServerModel.fetchAll().then(function (servers) {
            reply(servers.toJSON())
          }).catch(function (error) {
            reply(error)
          })
        }
      })

    server.route(
      { method: 'GET'
      , path: '/{id}'
      , handler: function (req, reply) {
          ServerModel
            .forge({ id: req.params.id })
            .fetch()
            .then(function (server) {
              if (!server) return reply.notFound()

              reply(server.toJSON())
            })
            .catch(reply)
        }
      })

    server.route(
      { method: 'PATCH'
      , path: '/{id}'
      , handler: function (req, reply) {
          ServerModel
            .forge({ id: req.params.id })
            .fetch()
            .then(function (server) {
              if (!server) return reply.notFound()
              if (!Object.keys(req.payload).length) return reply.badRequest('Missing payload')

              validateConnection(req.payload, req.payload.tables, function (error) {
                if (error) return reply.badRequest(error.message)

                server.save(req.payload, { patch: true })
                  .then(function (updatedServer) {
                  // updatedServer is sometimes boom error, huh?
                  if (updatedServer && !updatedServer.isBoom) reply(updatedServer.toJSON())
                })
                .catch(reply)
              })<commit_msg>Better validation for server update<commit_after>var _ = require('lodash')
  , schema = require('./schema')
  , validateConnection = require('app/lib/validate-connection')

module.exports = function (ServerModel) {
  return function (server, options, cb) {
    server.route(
      { method: 'POST'
      , path: '/'
      , handler: function (req, reply) {
          validateConnection(req.payload, req.payload.tables, function (error) {
            if (error) return reply.badRequest(error.message)

            ServerModel
              .forge(req.payload)
              .save()
              .then(function (server) {
                reply(server.toJSON())
              })
              .catch(reply)
          })
        }
      , config:
        { validate:
          { payload: schema.create
          }
        }
      })

    server.route(
      { method: 'GET'
      , path: '/'
      , handler: function (req, reply) {
          ServerModel.fetchAll().then(function (servers) {
            reply(servers.toJSON())
          }).catch(function (error) {
            reply(error)
          })
        }
      })

    server.route(
      { method: 'GET'
      , path: '/{id}'
      , handler: function (req, reply) {
          ServerModel
            .forge({ id: req.params.id })
            .fetch()
            .then(function (server) {
              if (!server) return reply.notFound()

              reply(server.toJSON())
            })
            .catch(reply)
        }
      })

    server.route(
      { method: 'PATCH'
      , path: '/{id}'
      , handler: function (req, reply) {
          ServerModel
            .forge({ id: req.params.id })
            .fetch()
            .then(function (server) {
              if (!server) return reply.notFound()
              if (!Object.keys(req.payload).length) return reply.badRequest('Missing payload')

              var merged = _.merge({}, server.toJSON(), req.payload)

              validateConnection(merged, merged.tables, function (error) {
                if (error) return reply.badRequest(error.message)

                server.save(req.payload, { patch: true })
                  .then(function (updatedServer) {
                  // updatedServer is sometimes boom error, huh?
                  if (updatedServer && !updatedServer.isBoom) reply(updatedServer.toJSON())
                })
                .catch(reply)
              })
{'fraction_non_alphanumeric': 0.13002964845404488, 'fraction_numerical': 0.0, 'mean_word_length': 1.5055702917771883, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6794022', 'n_tokens_mistral': 1277, 'n_tokens_neox': 1238, 'n_words': 260}