Skip to content

Commit

Permalink
Refactor flags. Closes #1847. Closes #1848. Closes #1849. Closes #1850.
Browse files Browse the repository at this point in the history
Closes #1851. Closes #1852. Closes #1853
  • Loading branch information
hueniverse committed Jun 8, 2019
1 parent d3e33db commit a8c916e
Show file tree
Hide file tree
Showing 17 changed files with 323 additions and 576 deletions.
2 changes: 1 addition & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ const customJoi = Joi.extend((joi) => ({
name: 'round',
setup(params) {

this._flags.round = true; // Set a flag for later use
this._flags.round = true; // Set a flag for later use
},
validate(params, value, state, options) {

Expand Down
9 changes: 2 additions & 7 deletions lib/about.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ exports.describe = function (schema) {
[Settings.symbols.schema]: schema
};

const flags = Object.keys(schema._flags);
const flags = Object.keys(schema._flags).filter((name) => name[0] !== '_' && !internals.exclude.includes(name));
if (flags.length) {
description.flags = {};
for (const flag of flags) {
Expand Down Expand Up @@ -47,12 +47,7 @@ exports.describe = function (schema) {
break;

default:
if (!internals.exclude.includes(flag) &&
flag[0] !== '_') {

description.flags[flag] = value;
}

description.flags[flag] = value;
break;
}
}
Expand Down
21 changes: 14 additions & 7 deletions lib/types/alternatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ internals.Alternatives = class extends Any {
try(schemas) {

Hoek.assert(schemas, 'Missing alternative schemas');
Hoek.assert(!this._flags._endedSwitch, 'Unreachable condition');

if (!Array.isArray(schemas)) {
schemas = [schemas];
Expand All @@ -96,9 +97,12 @@ internals.Alternatives = class extends Any {
when(condition, options) {

let schemaCondition = false;
Hoek.assert(!this._baseType, 'Cannot chain multiple when conditions on non-alternatives root');
Hoek.assert(Ref.isRef(condition) || typeof condition === 'string' || (schemaCondition = condition instanceof Any), 'Invalid condition:', condition);
Hoek.assert(options, 'Missing options');
Hoek.assert(typeof options === 'object', 'Invalid options');
Hoek.assert(!this._flags._endedSwitch, 'Unreachable condition');

if (schemaCondition) {
Hoek.assert(!options.hasOwnProperty('is'), '"is" can not be used with a schema condition');
}
Expand All @@ -109,11 +113,19 @@ internals.Alternatives = class extends Any {
Hoek.assert(options.then !== undefined || options.otherwise !== undefined, 'options must have at least one of "then" or "otherwise"');

const obj = this.clone();

if (options.then !== undefined &&
options.otherwise !== undefined) {

obj._flag('_endedSwitch', true, { clone: false });
}

let is;
if (!schemaCondition) {
is = Cast.schema(this._currentJoi, options.is);

if (options.is === null || !(Ref.isRef(options.is) || options.is instanceof Any)) {
if (options.is === null ||
!(Ref.isRef(options.is) || options.is instanceof Any)) {

// Only apply required if this wasn't already a schema or a ref, we'll suppose people know what they're doing
is = is.required();
Expand All @@ -128,11 +140,6 @@ internals.Alternatives = class extends Any {
otherwise: options.otherwise !== undefined ? Cast.schema(this._currentJoi, options.otherwise) : undefined
};

if (obj._baseType) {
item.then = item.then && obj._baseType.concat(item.then);
item.otherwise = item.otherwise && obj._baseType.concat(item.otherwise);
}

if (!schemaCondition) {
obj._refs.register(item.ref, Ref.toSibling);
obj._refs.register(item.is, Ref.toSibling);
Expand Down Expand Up @@ -220,7 +227,7 @@ internals.Alternatives = class extends Any {
if (match[key] &&
match[key]._type === 'array') {

this._flags._arrayItems |= true;
this._flag('_arrayItems', true);
break;
}
}
Expand Down
Loading

0 comments on commit a8c916e

Please sign in to comment.