Skip to content

Commit

Permalink
Replace symbol string format. For #1867
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jul 6, 2019
1 parent 83a93c9 commit c214fa0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
9 changes: 9 additions & 0 deletions lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,15 @@ internals.stringify = function (value, prefs, options) {
return internals.Template.date(value, prefs);
}

if (value instanceof Map) {
const pairs = [];
for (const [key, sym] of value.entries()) {
pairs.push(`${key.toString()} -> ${sym.toString()}`);
}

value = pairs;
}

if (!Array.isArray(value)) {
return value.toString();
}
Expand Down
1 change: 1 addition & 0 deletions lib/types/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = internals.Any = class {
constructor(type) {

this._type = type || 'any';
this._baseType = null;
this._ids = new Modify.Ids(this);
this._preferences = null;
this._refs = new Ref.Manager();
Expand Down
14 changes: 6 additions & 8 deletions lib/types/symbol.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const Util = require('util');

const Hoek = require('@hapi/hoek');

const Any = require('./any');
Expand Down Expand Up @@ -56,11 +54,15 @@ internals.Symbol = class extends Any {

map(iterable) {

if (iterable && !iterable[Symbol.iterator] && typeof iterable === 'object') {
if (iterable &&
!iterable[Symbol.iterator] &&
typeof iterable === 'object') {

iterable = Object.entries(iterable);
}

Hoek.assert(iterable && iterable[Symbol.iterator], 'Iterable must be an iterable or object');

const obj = this.clone();

const symbols = [];
Expand All @@ -70,6 +72,7 @@ internals.Symbol = class extends Any {

Hoek.assert(typeof key !== 'object' && typeof key !== 'function' && typeof key !== 'symbol', 'Key must not be an object, function, or Symbol');
Hoek.assert(typeof value === 'symbol', 'Value must be a Symbol');

obj._inners.map.set(key, value);
symbols.push(value);
}
Expand Down Expand Up @@ -108,11 +111,6 @@ internals.Map = class extends Map {

return new internals.Map(this);
}

toString() {

return Util.inspect(this);
}
};


Expand Down
19 changes: 15 additions & 4 deletions test/types/symbol.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ describe('symbol', () => {
expect(() => Joi.symbol('invalid argument.')).to.throw('The symbol type does not allow arguments');
});

describe('clone()', () => {

it('clones a symbols type', () => {

const schema = Joi.symbol();
const clone = schema.clone();
expect(schema).to.equal(clone);
expect(schema).to.not.shallow.equal(clone);
});
});

describe('validate()', () => {

it('handles plain symbols', () => {
Expand Down Expand Up @@ -80,9 +91,9 @@ describe('symbol', () => {
[1, true, null, symbols[0]],
[symbols[0], true, null, symbols[0]],
['1', false, null, {
message: `"value" must be one of Map { 1 => Symbol(1), 'two' => Symbol(2) }`,
message: `"value" must be one of [1 -> Symbol(1), two -> Symbol(2)]`,
details: [{
message: `"value" must be one of Map { 1 => Symbol(1), 'two' => Symbol(2) }`,
message: `"value" must be one of [1 -> Symbol(1), two -> Symbol(2)]`,
path: [],
type: 'symbol.map',
context: { label: 'value', value: '1', map }
Expand Down Expand Up @@ -120,9 +131,9 @@ describe('symbol', () => {
}]
}],
['toString', false, null, {
message: `"value" must be one of Map { 'one' => Symbol(one), 'two' => Symbol(two) }`,
message: `"value" must be one of [one -> Symbol(one), two -> Symbol(two)]`,
details: [{
message: `"value" must be one of Map { 'one' => Symbol(one), 'two' => Symbol(two) }`,
message: `"value" must be one of [one -> Symbol(one), two -> Symbol(two)]`,
path: [],
type: 'symbol.map',
context: { label: 'value', value: 'toString', map: new Map([['one', symbols[0]], ['two', symbols[1]]]) }
Expand Down

0 comments on commit c214fa0

Please sign in to comment.