# Joi-password-complexity

**URL:** https://forum.codewithmosh.com/t/joi-password-complexity/573
**Category:** Node
**Created:** [November 8, 2020, 10:11am UTC](https://forum.codewithmosh.com/t/joi-password-complexity/573 "2020-11-08T10:11:29Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![annadema](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/annadema/32/83_2.png) [@annadema](https://forum.codewithmosh.com/u/annadema)
#### Post date: [November 8, 2020, 10:11am UTC](https://forum.codewithmosh.com/t/joi-password-complexity/573/1 "2020-11-08T10:11:29Z")

</div>

I’m trying to use the package ‘joi-password-complexity’ to validate password in users.js for the vidly app. It’s not working. I tried some solutions but without success. There is no much documentation on how to use it with an old version of Joi.

I’d like to know if is there someone that has already successfully tried to use it. TIA

here is my validateUser function in the module vidly\models\user.js:

```
function validateUser(user) {
   const schema = {
      name: Joi.string().min(5).max(50).required(),
      email: Joi.string().min(5).max(255).required().email(),
      password: passwordComplexity().required()
   };
   return Joi.validate(user, schema);
}
```

---

<div class="post-metadata">

### Author: ![nekoiry](https://avatars.discourse-cdn.com/v4/letter/n/d26b3c/32.png) [@nekoiry](https://forum.codewithmosh.com/u/nekoiry)
#### Post date: [November 9, 2020, 1:50am UTC](https://forum.codewithmosh.com/t/joi-password-complexity/573/2 "2020-11-09T01:50:58Z")

</div>

By digging through his code for an hour, I figured out that the author didn’t export the extension correctly. See [source](https://github.com/kamronbatman/joi-password-complexity/blob/master/src/index.ts) (line 114)

However, you can still do this:

```
const method = (value, helpers) => {
    const { error } = passwordComplexity().validate(value);
    const errors = helpers.errorsArray();
    error?.details.forEach(err => errors.push(helpers.error(err.type)));
    return error ? errors : value;
}

```

So that you can do-

```
const schema = { 
    name: ...
    email: ...
    password: Joi.string().custom(method, 'Password Complexity').required()
}

```

or

```
Joi.string().custom(method, 'Password Complexity').validate(...);

```

By the way, that fella writes shit codes and my day is ruined x(.

---

<div class="post-metadata">

### Author: ![annadema](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/annadema/32/83_2.png) [@annadema](https://forum.codewithmosh.com/u/annadema)
#### Post date: [November 9, 2020, 6:53am UTC](https://forum.codewithmosh.com/t/joi-password-complexity/573/3 "2020-11-09T06:53:48Z")

</div>

Thank you for your reply.  
But I don’t agree with you: I don’t think that some code is bad only because I don’t know how to use it correctly 🙂

If you look in ‘node\_modules\joi-password-complexity’ you can see the actual code in use. Moreover in README.md are specified the Requirements 🙂 So it’s likely that this is not working only because I’m using an old version of Joi. When I will finish my course I will come back and update Joi. For the moment I thank you for you effort. sorry if I caused you a bad moment 😉

---

<div class="post-metadata">

### Author: ![nekoiry](https://avatars.discourse-cdn.com/v4/letter/n/d26b3c/32.png) [@nekoiry](https://forum.codewithmosh.com/u/nekoiry)
#### Post date: [November 9, 2020, 4:08pm UTC](https://forum.codewithmosh.com/t/joi-password-complexity/573/4 "2020-11-09T16:08:09Z")

</div>

The code exported the extension wrapped in a function that can be used in the way the read-me file shows. It supposed to export the extension and extend the Joi class.

And I installed the newest version of Joi and I also failed to use it as an extension, so it’s not the matter of Joi version.

And don’t worry, I’m just joking with memes. “and my day is ruined” is a meme phrase and I’m always happy to help people with questions ;).

---

<div class="post-metadata">

### Author: ![annadema](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/annadema/32/83_2.png) [@annadema](https://forum.codewithmosh.com/u/annadema)
#### Post date: [November 9, 2020, 5:52pm UTC](https://forum.codewithmosh.com/t/joi-password-complexity/573/5 "2020-11-09T17:52:53Z")

</div>

Ok 🙂 thank you for your precisation. thank you for your addon.  
For now I will freeze it in a vault. Otherwise I am going to lose other time struggling with it an entire day 😂  
If I solve it I will update this post.

P.S.  
By the way if you search on the web, there was an answer in the old forum but Mosh staff didn’t save the old forum 😭 😭 😭  
and so all those precious information were lost 😟  
I also searched in web-archive but no trace at all of that, last and unique saving was in August.

---

<div class="post-metadata">

### Author: ![SAM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sam/32/167_2.png) [@SAM](https://forum.codewithmosh.com/u/SAM)
#### Post date: [November 9, 2020, 6:25pm UTC](https://forum.codewithmosh.com/t/joi-password-complexity/573/6 "2020-11-09T18:25:51Z")

</div>

> [@annadema](#):
>
> and so all those precious information were lost

@annadema google results have a small down arrow ▼in the URL above the title. If you click it you can get the page from google’s cache.

---

<div class="post-metadata">

### Author: ![annadema](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/annadema/32/83_2.png) [@annadema](https://forum.codewithmosh.com/u/annadema)
#### Post date: [November 9, 2020, 7:25pm UTC](https://forum.codewithmosh.com/t/joi-password-complexity/573/7 "2020-11-09T19:25:14Z")

</div>

Wow 😲. Great discover!! Thank you very much  
Even if for this specific problem there are not any other solutions that I have not already tried ☹  
It will be useful for other issues.  
P.S.  
I’m downloading and saving for future all the old pages of the forum cached in Google Search 👍 👍 👍

---

<div class="post-metadata">

### Author: ![SAM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sam/32/167_2.png) [@SAM](https://forum.codewithmosh.com/u/SAM)
#### Post date: [November 9, 2020, 10:08pm UTC](https://forum.codewithmosh.com/t/joi-password-complexity/573/8 "2020-11-09T22:08:01Z")

</div>

👍 If you find anything especially valuable feel free to repost it here in the respective category.

Since you are processing _all_ post maybe you will find out what the FAQ are and repost them with the best answers? That may help new users and can be a good starting point for sticky or wiki posts as soon as anyone of us gains the privileges to make posts sticky or wiki posts.

---

<div class="post-metadata">

### Author: ![annadema](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/annadema/32/83_2.png) [@annadema](https://forum.codewithmosh.com/u/annadema)
#### Post date: [November 10, 2020, 10:35am UTC](https://forum.codewithmosh.com/t/joi-password-complexity/573/9 "2020-11-10T10:35:28Z")

</div>

It could be a good idea.  
I’d like to give my contribute to the community sharing those info but it would be a time-wasting activity without a specific request. And I don’t like post something that I didn’t checked before

If I experienced some issue that I can solve only in the old forum pages , then I will certainly post the solution here 🙂

For your other request, sadly I haven’t find yet any old page with the FAQ list.

---

<div class="post-metadata">

### Author: ![annadema](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/annadema/32/83_2.png) [@annadema](https://forum.codewithmosh.com/u/annadema)
#### Post date: [November 10, 2020, 7:38pm UTC](https://forum.codewithmosh.com/t/joi-password-complexity/573/10 "2020-11-10T19:38:00Z")

</div>

SOLVED.  
My code didn’t work because I didn’t install the right version of joi-password-complexity

With the version available at the time of recording video (with all the requirements currently installed on my environment, the same of the course) the package “Joi-password-complexity” can be used as in the following code.

First I installed  
` npm i joi-password-complexity@2.0.1`

In the user module on the top I imported the package:  
`const passwordComplexity = require("joi-password-complexity");`

Then the validateUser simply becomes :

```
 function validateUser(user) {
    const schema = Joi.object({
        name: Joi.string().min(5).max(50).required(),
        email: Joi.string().min(5).max(255).required().email(),
        // password: Joi.string().min(5).max(255).required()
        password: new passwordComplexity(),
    });
    return Joi.validate(user, schema);
}

```

A valid password could be for example: “A234567!”  
with the default complexityOptions
