Skip to content

Commit

Permalink
Move interfaces to the top
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jun 29, 2019
1 parent 742d1e7 commit e81dfee
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 77 deletions.
84 changes: 42 additions & 42 deletions lib/types/alternatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ internals.Alternatives = class extends Any {
return { errors: this.createError('alternatives.types', value, { types }, state, prefs) };
}

_override(id, schema) {

let i = 0;
for (const match of this._inner.matches) {
for (const key of ['schema', 'peek', 'is', 'then', 'otherwise']) {
if (match[key] &&
id === match[key]._flags.id) {

const obj = this.clone();
obj._inner.matches[i] = Object.assign({}, match, { [key]: schema });
return obj._rebuild();
}
}

++i;
}
}

// About

describe() {
Expand Down Expand Up @@ -130,6 +148,30 @@ internals.Alternatives = class extends Any {

// Rules

label(name) {

const obj = super.label(name);
obj._inner.matches = obj._inner.matches.map((match) => {

if (match.schema) {
return { schema: match.schema.label(name) };
}

match = Object.assign({}, match);
if (match.then) {
match.then = match.then.label(name);
}

if (match.otherwise) {
match.otherwise = match.otherwise.label(name);
}

return match;
});

return obj;
}

try(schemas) {

Hoek.assert(schemas, 'Missing alternative schemas');
Expand Down Expand Up @@ -237,50 +279,8 @@ internals.Alternatives = class extends Any {
return obj._rebuild();
}

label(name) {

const obj = super.label(name);
obj._inner.matches = obj._inner.matches.map((match) => {

if (match.schema) {
return { schema: match.schema.label(name) };
}

match = Object.assign({}, match);
if (match.then) {
match.then = match.then.label(name);
}

if (match.otherwise) {
match.otherwise = match.otherwise.label(name);
}

return match;
});

return obj;
}

// Internals

_override(id, schema) {

let i = 0;
for (const match of this._inner.matches) {
for (const key of ['schema', 'peek', 'is', 'then', 'otherwise']) {
if (match[key] &&
id === match[key]._flags.id) {

const obj = this.clone();
obj._inner.matches[i] = Object.assign({}, match, { [key]: schema });
return obj._rebuild();
}
}

++i;
}
}

_rebuild() {

Hoek.assert(!this._inRuleset(), 'Cannot set alternative schemas inside a ruleset');
Expand Down
50 changes: 25 additions & 25 deletions lib/types/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,31 @@ internals.Array = class extends Any {
return { value: value.slice() }; // Clone the array so that we don't modify the original
}

_override(id, schema) {

for (const set of ['items', 'ordereds']) {
for (let i = 0; i < this._inner[set].length; ++i) {
const existing = this._inner[set][i];
if (id === existing._flags.id) {
const obj = this.clone();
obj._inner[set][i] = schema;
obj._rebuild();
return obj;
}
}
}

const hases = this._getRules('has');
for (const has of hases) {
if (id === has.args.schema._flags.id) {
const obj = this.clone();
has.args.schema = schema;
obj._rebuild();
return obj;
}
}
}

// About

describe() {
Expand Down Expand Up @@ -283,31 +308,6 @@ internals.Array = class extends Any {
return this._rule(name, { rule: 'length', refs, args: { limit }, operator });
}

_override(id, schema) {

for (const set of ['items', 'ordereds']) {
for (let i = 0; i < this._inner[set].length; ++i) {
const existing = this._inner[set][i];
if (id === existing._flags.id) {
const obj = this.clone();
obj._inner[set][i] = schema;
obj._rebuild();
return obj;
}
}
}

const hases = this._getRules('has');
for (const has of hases) {
if (id === has.args.schema._flags.id) {
const obj = this.clone();
has.args.schema = schema;
obj._rebuild();
return obj;
}
}
}

_rebuild() {

this._resetRegistrations();
Expand Down
20 changes: 10 additions & 10 deletions lib/types/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,16 @@ internals.Object = class extends Any {
return { value, errors };
}

_override(id, schema) {

for (const child of this._inner.children) {
const childId = child.schema._flags.id || child.key;
if (id === childId) {
return this.keys({ [child.key]: schema });
}
}
}

// About

describe(shallow) {
Expand Down Expand Up @@ -602,16 +612,6 @@ internals.Object = class extends Any {
return this._rule(name, { rule: 'length', refs, args: { limit }, operator });
}

_override(id, schema) {

for (const child of this._inner.children) {
const childId = child.schema._flags.id || child.key;
if (id === childId) {
return this.keys({ [child.key]: schema });
}
}
}

_rebuild() {

this._resetRegistrations();
Expand Down

0 comments on commit e81dfee

Please sign in to comment.