Validation - nirrek/wallflyServer GitHub Wiki

Validating Requests

Hapi.js supports specifying validation in the routes. Please use this feature. This is important for data integrity, it also means we don't need to have validation logic in the handlers themselves, and it also is great documentation for understanding what data the endpoint needs to receive. Robust error messages are also returned to the client if you fail validation, so this is very handy when you are developing a feature, and you forget to send some data, or give it an incorrect name or something.

{
    method: 'POST',
    path: '/properties/{propertyId}/details',
    config: {
      handler: require('./handlers/propertyDetails.js'),
      auth: 'session',
      validate: {
        // This is for validating path parameters in the URL
        params: { propertyId: Joi.number().integer() },

        // This is for validating data in the payload (ie the body of the HTTP request)
        // If you are passing data via an AJAX call, it will be in the payload.
        payload: {
          propertyId: Joi.number().integer().required(),
          tenantEmail: Joi.string().email(),
          image: Joi.string(),
        }

        // This is for validating query parameters in the url. eg. `?limit=10`
        query: {
            limit: Joi.number().integer().min(1).max(100).default(10)
        }
      }
    }
  },

For more information read the documentation: http://hapijs.com/tutorials/validation

The Joi documentation (which is how the validation is specified) is also useful https://github.com/hapijs/joi