Skip to content

Commit

Permalink
Normalize regex in describe(). For #1867
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jul 6, 2019
1 parent cd52685 commit 1756d6c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 56 deletions.
28 changes: 16 additions & 12 deletions lib/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ exports.describe = function (schema) {

// Inners

const inners = ['notes', 'tags', 'meta', 'examples', 'alterations', 'externals', 'items', 'ordered', 'matches', 'map', 'truthy', 'falsy'];
const inners = ['renames', 'patterns', 'notes', 'tags', 'meta', 'examples', 'alterations', 'externals', 'items', 'ordered', 'matches', 'map', 'truthy', 'falsy'];
for (const inner of inners) {
// for (const inner in schema._inners) {
const items = schema._inners[inner];
Expand Down Expand Up @@ -201,25 +201,29 @@ internals.inner = function (item) {
return item.describe();
}

if (typeof item !== 'object') {
return item;
}

if (Array.isArray(item)) {
return item;
}

if (typeof item === 'object') {
const normalized = {};
for (const key in item) {
const value = item[key];
if (value === undefined ||
value === null) {
if (item instanceof RegExp) {
return { regex: item.toString() };
}

continue;
}
const normalized = {};
for (const key in item) {
const value = item[key];
if (value === undefined ||
value === null) {

normalized[key] = internals.inner(value);
continue;
}

return normalized;
normalized[key] = internals.inner(value);
}

return item;
return normalized;
};
38 changes: 0 additions & 38 deletions lib/types/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,44 +331,6 @@ internals.Object = class extends Any {
}
}

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

for (const pattern of this._inners.patterns) {
const config = { rule: pattern.rule.describe() };

if (pattern.regex) {
config.regex = pattern.regex.toString();
}
else {
config.schema = pattern.schema.describe();
}

if (pattern.matches) {
config.matches = pattern.matches.describe();
}

if (pattern.exclusive) {
config.exclusive = true;
}

description.patterns.push(config);
}
}

if (this._inners.renames) {
description.renames = Hoek.clone(this._inners.renames);
for (const rename of description.renames) {
if (rename.from instanceof RegExp) {
rename.from = { regex: rename.from.toString() };
}

if (rename.to instanceof Template) {
rename.to = rename.to.describe();
}
}
}

return description;
}

Expand Down
12 changes: 6 additions & 6 deletions test/types/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ describe('object', () => {
},
patterns: [
{
regex: '/\\w\\d/i',
regex: { regex: '/\\w\\d/i' },
rule: {
type: 'boolean',
flags: {
Expand Down Expand Up @@ -1719,7 +1719,7 @@ describe('object', () => {
expect(schema.describe()).to.equal({
type: 'object',
patterns: [{
regex: '/^x\\d+x$/',
regex: { regex: '/^x\\d+x$/' },
rule: { type: 'any' }
}],
renames: [{
Expand Down Expand Up @@ -1760,7 +1760,7 @@ describe('object', () => {
expect(schema.describe()).to.equal({
type: 'object',
patterns: [{
regex: '/^x\\d+x$/',
regex: { regex: '/^x\\d+x$/' },
rule: { type: 'any' }
}],
renames: [{
Expand Down Expand Up @@ -2932,7 +2932,7 @@ describe('object', () => {
insensitive: true
}
},
regex: '/^x\\d+$/',
regex: { regex: '/^x\\d+$/' },
matches: {
type: 'array',
flags: {
Expand Down Expand Up @@ -2961,7 +2961,7 @@ describe('object', () => {
},
invalids: [Infinity, -Infinity]
},
regex: '/^z\\w+$/'
regex: { regex: '/^z\\w+$/' }
},
{
rule: {
Expand All @@ -2971,7 +2971,7 @@ describe('object', () => {
},
invalids: [Infinity, -Infinity]
},
regex: '/^x\\w+$/',
regex: { regex: '/^x\\w+$/' },
matches: {
type: 'array',
flags: {
Expand Down

0 comments on commit 1756d6c

Please sign in to comment.