Skip to content

Commit

Permalink
Move extension messages to constructor. Closes #1956
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jun 30, 2019
1 parent f85705f commit 6347a82
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
17 changes: 9 additions & 8 deletions lib/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,28 @@ exports.root = function (root, extensions) {

root.assert(extension, Schemas.extension);

const base = (extension.base || root.any()).clone(); // Cloning because we're going to override messages afterwards
const base = extension.base || root.any();
const ctor = base.constructor;
const type = class extends ctor { // eslint-disable-line no-loop-func
const type = class extends ctor { // eslint-disable-line no-loop-func

constructor() {

super();

if (extension.base) {
Object.assign(this, base);
Object.assign(this, extension.base);
}

this._type = extension.name;

if (extension.messages) {
const existing = base._preferences && Hoek.clone(base._preferences.messages);
this._preferences = Object.assign({}, this._preferences);
this._preferences.messages = Messages.compile(extension.messages, existing);
}
}
};

if (extension.messages) {
const existing = type.prototype._messages || base._preferences && base._preferences.messages;
type.prototype._messages = Messages.compile(extension.messages, existing);
}

if (extension.coerce) {
type.prototype._coerce = function (value, state, prefs) {

Expand Down
6 changes: 1 addition & 5 deletions lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,18 +358,14 @@ internals.prefs = function (schema, prefs) {
return schema._preferences[Common.symbols.prefs];
}

prefs = Common.preferences(schema._messages ? Common.preferences({ messages: schema._messages }, prefs) : prefs, schema._preferences);
prefs = Common.preferences(prefs, schema._preferences);
if (isDefaultOptions) {
schema._preferences[Common.symbols.prefs] = prefs;
}

return prefs;
}

if (schema._messages) {
return Common.preferences({ messages: schema._messages }, prefs);
}

return prefs;
};

Expand Down
4 changes: 2 additions & 2 deletions test/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ describe('extension', () => {
expect(schema.validate(3)).to.contain({ error: null, value: 6 });
});

it('does not override a predefined messages', () => {
it('overrides base messages', () => {

const base = Joi.any().prefs({
messages: {
Expand Down Expand Up @@ -566,7 +566,7 @@ describe('extension', () => {
const schema = customJoi.myType().foo();
const result = schema.validate({});
expect(result.error).to.be.an.instanceof(Error);
expect(result.error.toString()).to.equal('ValidationError: "value" original');
expect(result.error.toString()).to.equal('ValidationError: "value" modified');
});

it('does not change predefined options', () => {
Expand Down

0 comments on commit 6347a82

Please sign in to comment.