Skip to content

Commit

Permalink
Disable error rendering. Closes #2035
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Aug 9, 2019
1 parent 8664797 commit eb622c8
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,8 @@ Validates a value using the current schema and options where:
validation function. Note that references to the value are usually not what you want as they move
around the value structure relative to where the error happens. Instead, either use the global
context, or the absolute value using local context notation (e.g. `Joi.ref('#variable')`);
- `render` - when `false`, skips rendering error templates. Useful when error messages are generated
elsewhere to save processing time. Defaults to `true`.
- `stack` - when `true`, the main error will possess a stack trace, otherwise it will be disabled.
Defaults to `false` for performances reasons.
Has no effect on platforms other than V8/node.js as it uses the [Stack trace API](https://v8.dev/docs/stack-trace-api).
Expand Down
1 change: 1 addition & 0 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ exports.defaults = {
errors: {
escapeHtml: false,
language: null,
render: true,
stack: false,
wrapArrays: true
},
Expand Down
4 changes: 4 additions & 0 deletions lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ exports.Report = class {

const code = this.code;

if (!this.prefs.errors.render) {
return this.code;
}

const template = this._template(this.template) ||
this._template(this.prefs.messages) ||
this._template(this.messages);
Expand Down
1 change: 1 addition & 0 deletions lib/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ exports.preferences = Joi.object({
Joi.string(),
Joi.object().ref()
],
render: Joi.boolean(),
stack: Joi.boolean(),
wrapArrays: Joi.boolean()
},
Expand Down
5 changes: 5 additions & 0 deletions test/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ describe('errors', () => {
expect(schema.validate({ a: 1, lang: 'empty' }).error).to.be.an.error('"a" must be larger than or equal to 10');
});

it('supports render preference', () => {

expect(Joi.number().min(10).validate(1, { errors: { render: false } }).error).to.be.an.error('number.min');
});

it('does not prefix with key when messages uses context.key', () => {

const schema = Joi.valid('sad').prefs({ messages: { 'any.only': 'my hero "{{#label}}" is not {{#valids}}' } });
Expand Down

0 comments on commit eb622c8

Please sign in to comment.