Skip to content

Commit

Permalink
Merge pull request #1115 from dwelle/master
Browse files Browse the repository at this point in the history
Added clarifying docs around `optional` behavior (fixes #1111).
  • Loading branch information
Marsup committed Feb 27, 2017
2 parents d09e342 + 90d488a commit 434df05
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,8 @@ Converts the type into an [`alternatives`](#alternatives) type where the conditi
- `then` - the alternative schema type if the condition is true. Required if `otherwise` is missing.
- `otherwise` - the alternative schema type if the condition is false. Required if `then` is missing.
Note: by default, the `is` condition schema allows for `undefined` values. Use `.required()` to override.
```js
const schema = {
a: Joi.any().valid('x').when('b', { is: 5, then: Joi.valid('y'), otherwise: Joi.valid('z') }),
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,20 @@ const schema = Joi.object().keys({

When validating a schema:

* Keys are optional by default.
* Values (or keys in case of objects) are optional by default.

```javascript
Joi.validate(undefined, Joi.string()); // validates fine
```

To disallow this behavior, you can either set the schema as `required()`, or set `presence` to `"required"` when passing `options`:

```javascript
Joi.validate(undefined, Joi.string().required());
// or
Joi.validate(undefined, Joi.string(), /* options */ { presence: "required" });
```

* Strings are utf-8 encoded by default.
* Rules are defined in an additive fashion and evaluated in order after whitelist and blacklist checks.

Expand Down

0 comments on commit 434df05

Please sign in to comment.