Skip to content

Commit

Permalink
Move array and alternatives inner to manifest. For #1867
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jul 6, 2019
1 parent 715c5dd commit 83a93c9
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 144 deletions.
82 changes: 52 additions & 30 deletions lib/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,36 +92,6 @@ exports.describe = function (schema) {
description.invalids = internals.values(invalids);
}

// Meta properties

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

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

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

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

// 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 = [];
Expand Down Expand Up @@ -183,6 +153,26 @@ exports.describe = function (schema) {
description.base = schema._baseType.describe();
}

// Inners

const inners = ['notes', 'tags', 'meta', 'examples', 'alterations', 'externals', 'items', 'ordered', 'matches'];
for (const inner of inners) {
// for (const inner in schema._inners) {
const items = schema._inners[inner];
if (!items ||
!items.length) {

continue;
}

const normalized = [];
for (const item of items) {
normalized.push(internals.inner(item));
}

description[inner] = normalized;
}

return description;
};

Expand All @@ -197,3 +187,35 @@ internals.values = function (values) {

return normalized;
};


internals.inner = function (item) {

if (Common.isSchema(item) ||
Common.isResolvable(item)) {

return item.describe();
}

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) {

continue;
}

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

return normalized;
}

return item;
};
37 changes: 0 additions & 37 deletions lib/types/alternatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,43 +109,6 @@ internals.Alternatives = class extends Any {
}
}

// About

describe() {

const description = super.describe();

const alternatives = [];
for (let i = 0; i < this._inners.matches.length; ++i) {
const item = this._inners.matches[i];
if (item.schema) {

// try()

alternatives.push(item.schema.describe());
}
else {

// when()

const when = item.is ? { ref: item.ref.describe(), is: item.is.describe() } : { peek: item.peek.describe() };

if (item.then) {
when.then = item.then.describe();
}

if (item.otherwise) {
when.otherwise = item.otherwise.describe();
}

alternatives.push(when);
}
}

description.alternatives = alternatives;
return description;
}

// Rules

label(name) {
Expand Down
39 changes: 8 additions & 31 deletions lib/types/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internals.Array = class extends Any {

this._flags.sparse = false;
this._inners.items = [];
this._inners.ordereds = [];
this._inners.ordered = [];
this._inners.inclusions = [];
this._inners.exclusions = [];
this._inners.requireds = [];
Expand Down Expand Up @@ -79,7 +79,7 @@ internals.Array = class extends Any {

_override(id, schema) {

for (const set of ['items', 'ordereds']) {
for (const set of ['items', 'ordered']) {
for (let i = 0; i < this._inners[set].length; ++i) {
const existing = this._inners[set][i];
if (id === existing._flags.id) {
Expand All @@ -103,29 +103,6 @@ internals.Array = class extends Any {
}
}

// About

describe() {

const description = super.describe();

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());
}
}

return description;
}

// Rules

has(schema) {
Expand Down Expand Up @@ -176,7 +153,7 @@ internals.Array = class extends Any {
internals.validateSingle(type, obj);

obj._register(type);
obj._inners.ordereds.push(type);
obj._inners.ordered.push(type);
}

return obj;
Expand Down Expand Up @@ -322,7 +299,7 @@ internals.Array = class extends Any {
}
}

for (const type of this._inners.ordereds) {
for (const type of this._inners.ordered) {
internals.validateSingle(type, this);
this._register(type);
}
Expand Down Expand Up @@ -381,7 +358,7 @@ Common.extend(internals.Array, 'rules', {
items: function (value, { schema, error, state, prefs }) {

const requireds = schema._inners.requireds.slice();
const ordereds = schema._inners.ordereds.slice();
const ordereds = schema._inners.ordered.slice();
const inclusions = [...schema._inners.inclusions, ...requireds];

const wasArray = !value[Common.symbols.arraySingle];
Expand Down Expand Up @@ -436,8 +413,8 @@ Common.extend(internals.Array, 'rules', {

// Ordered

if (schema._inners.ordereds.length) {
if (ordereds.length > 0) {
if (schema._inners.ordered.length) {
if (ordereds.length) {
const ordered = ordereds.shift();
const res = ordered._validate(item, localState, prefs);
if (!res.errors) {
Expand Down Expand Up @@ -468,7 +445,7 @@ Common.extend(internals.Array, 'rules', {
continue;
}
else if (!schema._inners.items.length) {
errors.push(error('array.orderedLength', { pos: i, limit: schema._inners.ordereds.length }));
errors.push(error('array.orderedLength', { pos: i, limit: schema._inners.ordered.length }));
if (prefs.abortEarly) {
return errors;
}
Expand Down
18 changes: 11 additions & 7 deletions test/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,20 @@ describe('Manifest', () => {
},
min: {
type: 'alternatives',
alternatives: [
matches: [
{
type: 'number',
invalids: [Infinity, -Infinity],
flags: { unsafe: false }
schema: {
type: 'number',
invalids: [Infinity, -Infinity],
flags: { unsafe: false }
}
},
{
type: 'string',
invalids: [''],
rules: [{ name: 'min', args: { limit: 3 } }]
schema: {
type: 'string',
invalids: [''],
rules: [{ name: 'min', args: { limit: 3 } }]
}
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion test/ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ describe('ref', () => {
invalids: [{ ref: 'global', key: 'b.c', path: ['b', 'c'] }],
valids: [{ ref: 'value', key: 'a.b', path: ['a', 'b'] }]
},
alternatives: [{
matches: [{
ref: { ref: 'value', key: 'a.b', path: ['a', 'b'] },
is: {
type: 'date',
Expand Down
Loading

0 comments on commit 83a93c9

Please sign in to comment.