Joi: Cannot read property 'presence' of undefined`,

Created on 18 Apr 2020  路  6Comments  路  Source: sideway/joi

Support plan

  • which support plan is this issue covered by? (e.g. Community, Core, Plus, or Enterprise): community
  • is this issue currently blocking your project? (yes/no):yes
  • is this issue affecting a production system? (yes/no):no

Context

  • node version: v12.16.2
  • module version: @hapi/[email protected]
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...):express
  • any other relevant information:

How can we help?

Error Details

[Error [ValidationError]: "name" failed custom validation because Cannot read property 'presence' of undefined] {
  _original: {
    name: 'John Doe',
    email: '[email protected]',
    password: '12324sgs4sfsf674yhlh'
  },
  details: [
    {
      message: `"name" failed custom validation because Cannot read property 'presence' of undefined`,
      path: [Array],
      type: 'any.custom',
      context: [Object]
    }
  ]
}

Schema

const Joi = require('@hapi/joi');



//Validation
const schema = Joi.object().keys({
    name: Joi.string().min(6).required,
    email: Joi.string().min(6).email().required,
    password: Joi.string().min(8).required,
});

Implementation

router.post('/register', async(req, res) => {
    try {
        const value = await schema.validateAsync({name: req.body.name, email: req.body.email, password: req.body.password});
        console.log(value);
    } catch (err) {
        console.log(err);
    }

    //  const {error} = schema.validate({name: req.body.name, email: req.body.email, password: req.body.password});
    //  console.log(error);
    const user = new User({
        name: req.body.name,
        email: req.body.email,
        password: req.body.password,
    });

    try {
        const savedUser = await user.save();
        res.send(savedUser);
    } catch (error) {
        res.send(error);
    };
});
support

Most helpful comment

@najdi123 using .required instead of .required() may cause this.

All 6 comments

Solved

hey man, do you remember how you solved the issue? im having the same error

@najdi123 using .required instead of .required() may cause this.

@najdi123 using .required instead of .required() may cause this.

Yea, that was my problem too, thanks for replying

@najdi123 using .required instead of .required() may cause this.

Same here.

Had the same issue using TypeScript / ES6. The solution was to use import * as Joi from 'joi'; instead of import Joi from 'joi'; as Joi doesn't seem to have a default Export

Was this page helpful?
0 / 5 - 0 ratings

Related issues

REBELinBLUE picture REBELinBLUE  路  3Comments

alekbarszczewski picture alekbarszczewski  路  3Comments

jamesdixon picture jamesdixon  路  4Comments

Taxi4you picture Taxi4you  路  3Comments

JbIPS picture JbIPS  路  4Comments