Skip to content

Commit

Permalink
Normalize describe(). For #1606. For #1867
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jul 2, 2019
1 parent 3ba54e3 commit 62a0417
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 281 deletions.
1 change: 0 additions & 1 deletion lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ exports.symbols = {
arraySingle: Symbol('arraySingle'),
castFrom: Symbol('castFrom'),
deepDefault: Symbol('deepDefault'),
schema: Symbol('schema'), // Used by describe() to include a reference to the schema
prefs: Symbol('prefs'),
ref: Symbol('ref'),
template: Symbol('template')
Expand Down
1 change: 0 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ internals.methods = {

ValidationError: Errors.ValidationError,
version: Pkg.version,
schema: Common.symbols.schema,
cache: Cache.provider,

assert: function (value, schema, ...args/* [message], [options]*/) {
Expand Down
59 changes: 39 additions & 20 deletions lib/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ exports.describe = function (schema) {

const description = {
type: schema._type,
flags: {},
[Common.symbols.schema]: schema
flags: {}
};

// Flags
Expand Down Expand Up @@ -72,17 +71,29 @@ exports.describe = function (schema) {
delete description.flags;
}

// Preferences

if (schema._preferences) {
description.options = Hoek.clone(schema._preferences, { shallow: ['messages'] });
if (description.options.messages) {
description.options.messages = Messages.decompile(description.options.messages);
}
}

if (schema._baseType) {
description.base = schema._baseType.describe();
// Valids

const valids = schema._valids ? schema._valids.values() : [];
if (valids.length) {
description.valids = internals.values(valids);
}

const invalids = schema._invalids ? schema._invalids.values() : [];
if (invalids.length) {
description.invalids = internals.values(invalids);
}

// Meta properties

if (schema._inners.notes.length) {
description.notes = schema._inners.notes.slice();
}
Expand All @@ -99,30 +110,20 @@ exports.describe = function (schema) {
description.examples = schema._inners.examples.slice();
}

const valids = schema._valids ? schema._valids.values() : [];
if (valids.length) {
description.valids = valids.map((v) => {

return Common.isResolvable(v) ? v.describe() : v;
});
}

const invalids = schema._invalids ? schema._invalids.values() : [];
if (invalids.length) {
description.invalids = invalids.map((v) => {

return Common.isResolvable(v) ? v.describe() : v;
});
}
// Alterations

if (schema._inners.alterations) {
description.alterations = schema._inners.alterations.slice();
}

// Externals

if (schema._inners.externals) {
description.externals = schema._inners.externals.slice();
}

// Rules

description.rules = [];

for (const test of schema._tests) {
Expand Down Expand Up @@ -153,7 +154,7 @@ exports.describe = function (schema) {
}

if (arg !== undefined) {
item.arg = Common.isResolvable(arg) ? arg.describe() : arg;
item.arg = Common.isResolvable(arg) || Common.isSchema(arg) ? arg.describe() : arg;
}
}

Expand Down Expand Up @@ -183,5 +184,23 @@ exports.describe = function (schema) {
delete description.rules;
}

// Extension

if (schema._baseType) {
description.base = schema._baseType.describe();
}

return description;
};


internals.values = function (values) {

const normalized = [];

for (const value of values) {
normalized.push(Common.isResolvable(value) ? value.describe() : (value && typeof value === 'object' ? { value } : value));
}

return normalized;
};
4 changes: 2 additions & 2 deletions lib/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ exports.decompile = function (messages) {
}

if (Template.isTemplate(message)) {
target[code] = message.source;
target[code] = message.describe({ compact: true });
continue;
}

Expand All @@ -102,7 +102,7 @@ exports.decompile = function (messages) {
continue;
}

target[language][code] = localized.source;
target[language][code] = localized.describe({ compact: true });
}
}

Expand Down
17 changes: 14 additions & 3 deletions lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const internals = {

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

constructor(source, options = {}) {
constructor(source, options) {

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

describe() {
describe(options = {}) {

return { template: this.source, options: this._settings };
if (!this._settings &&
options.compact) {

return this.source;
}

const desc = { template: this.source };
if (this._settings) {
desc.options = this._settings;
}

return desc;
}

isDynamic() {
Expand Down
11 changes: 0 additions & 11 deletions lib/types/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,18 @@ internals.Array = class extends Any {

if (this._inners.ordereds.length) {
description.orderedItems = [];

for (let i = 0; i < this._inners.ordereds.length; ++i) {
description.orderedItems.push(this._inners.ordereds[i].describe());
}
}

if (this._inners.items.length) {
description.items = [];

for (let i = 0; i < this._inners.items.length; ++i) {
description.items.push(this._inners.items[i].describe());
}
}

if (description.rules) {
for (let i = 0; i < description.rules.length; ++i) {
const rule = description.rules[i];
if (rule.name === 'has') {
rule.arg = rule.arg.describe();
}
}
}

return description;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/types/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ internals.Object = class extends Any {
if (description.rules) {
for (const rule of description.rules) {

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

if (/* $lab:coverage:off$ */rule.arg &&
typeof rule.arg === 'object' &&
Expand Down
Loading

0 comments on commit 62a0417

Please sign in to comment.