hapijs / hapijs/joi

Example from documentation is misleading

Open
#2,562 0 comments 0 reactions 1 assignee Claimed by @brianle1301 View on GitHub
documentation
Dominant language
JavaScript
Stars
21.2k
Forks
1.5k
Avg merge
4h 57m
Merged PRs (30d)
14

Description

#### Support plan

* *is this issue currently blocking your project?* (yes/no): no
* *is this issue affecting a production system?* (yes/no): no

#### Context

* *node version*: N/A
* *module version*: 17.4.0
* *environment* (e.g. node, browser, native): browser
* *used with* (e.g. hapi application, another framework, standalone, ...): N/A
* *any other relevant information*:

#### How can we help?

The [example from documentation](https://joi.dev/api/?v=17.4.0#example) implies that the validation should pass:
```js
schema.validate({ username: 'abc', birth_year: 1994 });
// -> { value: { username: 'abc', birth_year: 1994 } }
```

However, if the same schema and data above are tested against the [Schema Tester (v17.3.0)](https://joi.dev/tester/), the error below is thrown:
```js
Validation Error: "value" must contain at least one of [password, access_token]
```

I suggest the documentation should be updated to include the error in the validation result, so it won't mislead users into thinking that the example shouldn't throw an error. Also, it could include another example in the data is valid and an error isn't returned.

Just for the record, this is the schema used:
```js
Joi.object({
username: Joi.string()
.alphanum()
.min(3)
.max(30)
.required(),

password: Joi.string()
.pattern(new RegExp('^[a-zA-Z0-9]{3,30}$')),

repeat_password: Joi.ref('password'),

access_token: [
Joi.string(),
Joi.number()
],

birth_year: Joi.number()
.integer()
.min(1900)
.max(2013),

email: Joi.string()
.email({ minDomainSegments: 2, tlds: { allow: ['com', 'net'] } })
})
.with('username', 'birth_year')
.xor('password', 'access_token')
.with('password', 'repeat_password')
```

And this is the data:
```js
{ username: 'abc', birth_year: 1994 }
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.