Using Joi, require one of two fields to be non empty

Code below worked for me. I used alternatives because .or is really testing for the existence of keys and what you really wanted was an alternative where you would allow one key or the other to be empty. var console = require(“consoleit”); var Joi = require(‘joi’); var schema = Joi.alternatives().try( Joi.object().keys({ a: Joi.string().allow(”), b: Joi.string() … Read more

Joi validator conditional schema

I achieved the same in a little different manner. Posting the same here since this might be useful for someone in future. const schema = Joi.object({ type: Joi.number().required().valid(1, 2, 3), firstname: Joi.alternatives().conditional(‘type’, { is: 1, then: Joi.string().required() }), lastname: Joi.alternatives().conditional(‘type’, { is: 1, then: Joi.string().required() }), salary: Joi.alternatives().conditional(‘type’, { is: 2, then: Joi.number().required() }), pension: … Read more

Joi object validation: How to validate values with unknown key names?

Try this. It’ll basically accept any key within an object campaign and the value must validate against Joi.date().iso() campaign: Joi.object().pattern(/^/, Joi.date().iso()) This however will match any key. You can restrict this by padding out the regex a little. e.g. only word characters between 2 and 25 chars campaign: Joi.object().pattern(/\w{2,25}/, Joi.date().iso()) UPDATE Regarding the example in … Read more

Multipart HTTP response

You can serve the response as multipart/form-data and use Response.formData() to read response at client fetch(“/path/to/server”, {method:”POST”, body:formData}) .then(response => response.formData()) .then(fd => { for (let [key, prop] of fd) { console.log(key, prop) } }) let fd = new FormData(); fd.append(“json”, JSON.stringify({ file: “image” })); fetch(“data:image/gif;base64,R0lGODlhEAAQAPIAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkiH+GkNyZWF0ZWQgd2l0aCBhamF4bG9hZC5pbmZvACH5BAAKAAAAIf8LTkVUU0NBUEUyLjADAQAAACwAAAAAEAAQAAADMwi63P4wyklrE2MIOggZnAdOmGYJRbExwroUmcG2LmDEwnHQLVsYOd2mBzkYDAdKa+dIAAAh+QQACgABACwAAAAAEAAQAAADNAi63P5OjCEgG4QMu7DmikRxQlFUYDEZIGBMRVsaqHwctXXf7WEYB4Ag1xjihkMZsiUkKhIAIfkEAAoAAgAsAAAAABAAEAAAAzYIujIjK8pByJDMlFYvBoVjHA70GU7xSUJhmKtwHPAKzLO9HMaoKwJZ7Rf8AYPDDzKpZBqfvwQAIfkEAAoAAwAsAAAAABAAEAAAAzMIumIlK8oyhpHsnFZfhYumCYUhDAQxRIdhHBGqRoKw0R8DYlJd8z0fMDgsGo/IpHI5TAAAIfkEAAoABAAsAAAAABAAEAAAAzIIunInK0rnZBTwGPNMgQwmdsNgXGJUlIWEuR5oWUIpz8pAEAMe6TwfwyYsGo/IpFKSAAAh+QQACgAFACwAAAAAEAAQAAADMwi6IMKQORfjdOe82p4wGccc4CEuQradylesojEMBgsUc2G7sDX3lQGBMLAJibufbSlKAAAh+QQACgAGACwAAAAAEAAQAAADMgi63P7wCRHZnFVdmgHu2nFwlWCI3WGc3TSWhUFGxTAUkGCbtgENBMJAEJsxgMLWzpEAACH5BAAKAAcALAAAAAAQABAAAAMyCLrc/jDKSatlQtScKdceCAjDII7HcQ4EMTCpyrCuUBjCYRgHVtqlAiB1YhiCnlsRkAAAOwAAAAAAAAAAAA==”) .then(response => response.blob()) .then(blob => { fd.append(“file”, blob); new … Read more

Joi validation return only one error message

It happens because Joi aborts early by default. abortEarly – when true, stops validation on the first error, otherwise returns all the errors found. Defaults to true. *EDIT: Configuration has changed in hapi 8.0. You need to add abortEarly: false to the routes config: var server = new Hapi.Server(); server.connection({ host: ‘localhost’, port: 8000, routes: … Read more

How to store routes in separate files when using Hapi?

You can create a separate file for user routes (config/routes/user.js): module.exports = [ { method: ‘GET’, path: “https://stackoverflow.com/users”, handler: function () {} }, { method: ‘GET’, path: ‘/users/{id}’, handler: function () {} } ]; Similarly with cart. Then create an index file in config/routes (config/routes/index.js): var cart = require(‘./cart’); var user = require(‘./user’); module.exports = … Read more

Joi validation of array

Joi.array().items() accepts another Joi schema to use against the array elements. So an array of strings is this easy: Joi.array().items(Joi.string()) Same for an array of objects; just pass an object schema to items(): Joi.array().items(Joi.object({ // Object schema }))

How to destroy JWT Tokens on logout?

The JWT is stored on browser, so remove the token deleting the cookie at client side If you need also to invalidate the token from server side before its expiration time, for example account deleted/blocked/suspended, password changed, permissions changed, user logged out by admin, take a look at Invalidating JSON Web Tokens for some commons … Read more