Skip to content

Commit 62a0417

Browse files
committed
Normalize describe(). For #1606. For #1867
1 parent 3ba54e3 commit 62a0417

File tree

10 files changed

+301
-281
lines changed

10 files changed

+301
-281
lines changed

lib/common.js

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ exports.symbols = {
4343
arraySingle: Symbol('arraySingle'),
4444
castFrom: Symbol('castFrom'),
4545
deepDefault: Symbol('deepDefault'),
46-
schema: Symbol('schema'), // Used by describe() to include a reference to the schema
4746
prefs: Symbol('prefs'),
4847
ref: Symbol('ref'),
4948
template: Symbol('template')

lib/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ internals.methods = {
101101

102102
ValidationError: Errors.ValidationError,
103103
version: Pkg.version,
104-
schema: Common.symbols.schema,
105104
cache: Cache.provider,
106105

107106
assert: function (value, schema, ...args/* [message], [options]*/) {

lib/manifest.js

+39-20
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ exports.describe = function (schema) {
1515

1616
const description = {
1717
type: schema._type,
18-
flags: {},
19-
[Common.symbols.schema]: schema
18+
flags: {}
2019
};
2120

2221
// Flags
@@ -72,17 +71,29 @@ exports.describe = function (schema) {
7271
delete description.flags;
7372
}
7473

74+
// Preferences
75+
7576
if (schema._preferences) {
7677
description.options = Hoek.clone(schema._preferences, { shallow: ['messages'] });
7778
if (description.options.messages) {
7879
description.options.messages = Messages.decompile(description.options.messages);
7980
}
8081
}
8182

82-
if (schema._baseType) {
83-
description.base = schema._baseType.describe();
83+
// Valids
84+
85+
const valids = schema._valids ? schema._valids.values() : [];
86+
if (valids.length) {
87+
description.valids = internals.values(valids);
8488
}
8589

90+
const invalids = schema._invalids ? schema._invalids.values() : [];
91+
if (invalids.length) {
92+
description.invalids = internals.values(invalids);
93+
}
94+
95+
// Meta properties
96+
8697
if (schema._inners.notes.length) {
8798
description.notes = schema._inners.notes.slice();
8899
}
@@ -99,30 +110,20 @@ exports.describe = function (schema) {
99110
description.examples = schema._inners.examples.slice();
100111
}
101112

102-
const valids = schema._valids ? schema._valids.values() : [];
103-
if (valids.length) {
104-
description.valids = valids.map((v) => {
105-
106-
return Common.isResolvable(v) ? v.describe() : v;
107-
});
108-
}
109-
110-
const invalids = schema._invalids ? schema._invalids.values() : [];
111-
if (invalids.length) {
112-
description.invalids = invalids.map((v) => {
113-
114-
return Common.isResolvable(v) ? v.describe() : v;
115-
});
116-
}
113+
// Alterations
117114

118115
if (schema._inners.alterations) {
119116
description.alterations = schema._inners.alterations.slice();
120117
}
121118

119+
// Externals
120+
122121
if (schema._inners.externals) {
123122
description.externals = schema._inners.externals.slice();
124123
}
125124

125+
// Rules
126+
126127
description.rules = [];
127128

128129
for (const test of schema._tests) {
@@ -153,7 +154,7 @@ exports.describe = function (schema) {
153154
}
154155

155156
if (arg !== undefined) {
156-
item.arg = Common.isResolvable(arg) ? arg.describe() : arg;
157+
item.arg = Common.isResolvable(arg) || Common.isSchema(arg) ? arg.describe() : arg;
157158
}
158159
}
159160

@@ -183,5 +184,23 @@ exports.describe = function (schema) {
183184
delete description.rules;
184185
}
185186

187+
// Extension
188+
189+
if (schema._baseType) {
190+
description.base = schema._baseType.describe();
191+
}
192+
186193
return description;
187194
};
195+
196+
197+
internals.values = function (values) {
198+
199+
const normalized = [];
200+
201+
for (const value of values) {
202+
normalized.push(Common.isResolvable(value) ? value.describe() : (value && typeof value === 'object' ? { value } : value));
203+
}
204+
205+
return normalized;
206+
};

lib/messages.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ exports.decompile = function (messages) {
8585
}
8686

8787
if (Template.isTemplate(message)) {
88-
target[code] = message.source;
88+
target[code] = message.describe({ compact: true });
8989
continue;
9090
}
9191

@@ -102,7 +102,7 @@ exports.decompile = function (messages) {
102102
continue;
103103
}
104104

105-
target[language][code] = localized.source;
105+
target[language][code] = localized.describe({ compact: true });
106106
}
107107
}
108108

lib/template.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const internals = {
2525

2626
module.exports = exports = internals.Template = class {
2727

28-
constructor(source, options = {}) {
28+
constructor(source, options) {
2929

3030
Hoek.assert(typeof source === 'string', 'Template source must be a string');
3131
Hoek.assert(!source.includes('\u0000') && !source.includes('\u0001'), 'Template source cannot contain reserved control characters');
@@ -101,9 +101,20 @@ module.exports = exports = internals.Template = class {
101101
return internals.dateFormat[prefs.dateFormat].call(date);
102102
}
103103

104-
describe() {
104+
describe(options = {}) {
105105

106-
return { template: this.source, options: this._settings };
106+
if (!this._settings &&
107+
options.compact) {
108+
109+
return this.source;
110+
}
111+
112+
const desc = { template: this.source };
113+
if (this._settings) {
114+
desc.options = this._settings;
115+
}
116+
117+
return desc;
107118
}
108119

109120
isDynamic() {

lib/types/array.js

-11
Original file line numberDiff line numberDiff line change
@@ -110,29 +110,18 @@ internals.Array = class extends Any {
110110

111111
if (this._inners.ordereds.length) {
112112
description.orderedItems = [];
113-
114113
for (let i = 0; i < this._inners.ordereds.length; ++i) {
115114
description.orderedItems.push(this._inners.ordereds[i].describe());
116115
}
117116
}
118117

119118
if (this._inners.items.length) {
120119
description.items = [];
121-
122120
for (let i = 0; i < this._inners.items.length; ++i) {
123121
description.items.push(this._inners.items[i].describe());
124122
}
125123
}
126124

127-
if (description.rules) {
128-
for (let i = 0; i < description.rules.length; ++i) {
129-
const rule = description.rules[i];
130-
if (rule.name === 'has') {
131-
rule.arg = rule.arg.describe();
132-
}
133-
}
134-
}
135-
136125
return description;
137126
}
138127

lib/types/object.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ internals.Object = class extends Any {
319319
if (description.rules) {
320320
for (const rule of description.rules) {
321321

322-
// Coverage off for future-proof descriptions, only object().assert() is use right now
322+
// Coverage off for future-proof descriptions, only object().assert() is used right now
323323

324324
if (/* $lab:coverage:off$ */rule.arg &&
325325
typeof rule.arg === 'object' &&

0 commit comments

Comments
 (0)