Skip to content

Commit

Permalink
Merge pull request #1723 from WesTyler/abortEarly
Browse files Browse the repository at this point in the history
Allow validate options to be passed through assert/attempt #1722
  • Loading branch information
hueniverse authored May 29, 2019
2 parents 2108950 + b27a105 commit a700b3a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
10 changes: 6 additions & 4 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
- [`ValidationError`](#ValidationError)
- [`compile(schema)`](#compileschema)
- [`describe(schema)`](#describeschema)
- [`assert(value, schema, [message])`](#assertvalue-schema-message)
- [`attempt(value, schema, [message])`](#attemptvalue-schema-message)
- [`assert(value, schema, [message], [options])`](#assertvalue-schema-message-options)
- [`attempt(value, schema, [message], [options])`](#attemptvalue-schema-message-options)
- [`ref(key, [options])`](#refkey-options)
- [`isRef(ref)`](#isrefref)
- [`reach(schema, path)`](#reachschema-path)
Expand Down Expand Up @@ -378,23 +378,25 @@ Results in:
valids: [ 'foo', 'bar' ] }
```

### `assert(value, schema, [message])`
### `assert(value, schema, [message], [options])`

Validates a value against a schema and [throws](#errors) if validation fails where:
- `value` - the value to validate.
- `schema` - the validation schema. Can be a **joi** type object or a plain object where every key is assigned a **joi** type object using [`Joi.compile`](#compileschema) (be careful of the cost of compiling repeatedly the same schemas).
- `message` - optional message string prefix added in front of the error message. may also be an Error object.
- `options` - optional options object, passed in to [`Joi.validate`](##validatevalue-schema-options-callback)

```js
Joi.assert('x', Joi.number());
```

### `attempt(value, schema, [message])`
### `attempt(value, schema, [message], [options])`

Validates a value against a schema, returns valid object, and [throws](#errors) if validation fails where:
- `value` - the value to validate.
- `schema` - the validation schema. Can be a **joi** type object or a plain object where every key is assigned a **joi** type object using [`Joi.compile`](#compileschema) (be careful of the cost of compiling repeatedly the same schemas).
- `message` - optional message string prefix added in front of the error message. may also be an Error object.
- `options` - optional options object, passed in to [`Joi.validate`](##validatevalue-schema-options-callback)

```js
Joi.attempt('x', Joi.number()); // throws error
Expand Down
15 changes: 11 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,21 @@ internals.root = function () {
return Cast.schema(this, schema, { appendPath: true });
};

root.assert = function (value, schema, message) {
root.assert = function (...args) {

this.attempt(value, schema, message);
this.attempt(...args);
};

root.attempt = function (value, schema, message) {
root.attempt = function (value, schema, ...args/* [message], [options]*/) {

const result = this.validate(value, schema);
const first = args[0];
const message = (
first instanceof Error ||
typeof first === 'string'
) ? first : null;

const options = message ? args[1] : args[0];
const result = this.validate(value, schema, options);
const error = result.error;

if (!error) {
Expand Down
27 changes: 25 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2633,6 +2633,17 @@ describe('Joi', () => {
}).to.not.throw();
expect(result).to.not.exist();
});

it('respects abortEarly option', () => {

try {
Joi.assert({}, Joi.object().keys({ a: Joi.required(), b: Joi.required() }), { abortEarly: false });
throw new Error('should not reach that');
}
catch (err) {
expect(err.details.length).to.equal(2);
}
});
});

describe('attempt()', () => {
Expand Down Expand Up @@ -2679,11 +2690,23 @@ describe('Joi', () => {
}).to.throw('the reason is "value" must be a number');
});

it('throws on invalid value with message as error', () => {
it('throws on invalid value with message and abortEarly: false', () => {

try {
Joi.attempt({}, Joi.object().keys({ a: Joi.required(), b: Joi.required() }), 'the reasons are', { abortEarly: false });
throw new Error('should not reach that');
}
catch (err) {
expect(err.message.match(/the reasons are/)).to.not.equal(null);
expect(err.details.length).to.equal(2);
}
});

it('throws on invalid value with message as error even with abortEarly: false', () => {

expect(() => {

Joi.attempt('x', Joi.number(), new Error('invalid value'));
Joi.attempt({}, Joi.object().keys({ a: Joi.required(), b: Joi.required() }), new Error('invalid value'), { abortEarly: false });
}).to.throw('invalid value');
});

Expand Down

0 comments on commit a700b3a

Please sign in to comment.