Comparing version 11.3.4 to 11.4.0
@@ -34,5 +34,6 @@ 'use strict'; | ||
const item = this._inner.matches[i]; | ||
const schema = item.schema; | ||
if (!schema) { | ||
const failed = item.is._validate(item.ref(state.reference || state.parent, options), null, options, state.parent).errors; | ||
if (!item.schema) { | ||
const schema = item.peek || item.is; | ||
const input = item.is ? item.ref(state.reference || state.parent, options) : value; | ||
const failed = schema._validate(input, null, options, state.parent).errors; | ||
@@ -55,3 +56,3 @@ if (failed) { | ||
const result = schema._validate(value, state, options); | ||
const result = item.schema._validate(value, state, options); | ||
if (!result.errors) { // Found a valid match | ||
@@ -89,21 +90,31 @@ return result; | ||
when(ref, options) { | ||
when(condition, options) { | ||
Hoek.assert(Ref.isRef(ref) || typeof ref === 'string', 'Invalid reference:', ref); | ||
let schemaCondition = false; | ||
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(options.hasOwnProperty('is'), 'Missing "is" directive'); | ||
if (schemaCondition) { | ||
Hoek.assert(!options.hasOwnProperty('is'), '"is" can not be used with a schema condition'); | ||
} | ||
else { | ||
Hoek.assert(options.hasOwnProperty('is'), 'Missing "is" directive'); | ||
} | ||
Hoek.assert(options.then !== undefined || options.otherwise !== undefined, 'options must have at least one of "then" or "otherwise"'); | ||
const obj = this.clone(); | ||
let is = Cast.schema(this._currentJoi, options.is); | ||
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(); | ||
// 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(); | ||
} | ||
} | ||
const item = { | ||
ref: Cast.ref(ref), | ||
ref: schemaCondition ? null : Cast.ref(condition), | ||
peek: schemaCondition ? condition : null, | ||
is, | ||
@@ -120,4 +131,6 @@ then: options.then !== undefined ? Cast.schema(this._currentJoi, options.then) : undefined, | ||
Ref.push(obj._refs, item.ref); | ||
obj._refs = obj._refs.concat(item.is._refs); | ||
if (!schemaCondition) { | ||
Ref.push(obj._refs, item.ref); | ||
obj._refs = obj._refs.concat(item.is._refs); | ||
} | ||
@@ -153,5 +166,7 @@ if (item.then && item.then._refs) { | ||
const when = { | ||
const when = item.is ? { | ||
ref: item.ref.toString(), | ||
is: item.is.describe() | ||
} : { | ||
peek: item.peek.describe() | ||
}; | ||
@@ -158,0 +173,0 @@ |
@@ -388,3 +388,3 @@ 'use strict'; | ||
when(ref, options) { | ||
when(condition, options) { | ||
@@ -398,3 +398,8 @@ Hoek.assert(options && typeof options === 'object', 'Invalid options'); | ||
Alternatives = Alternatives || require('../alternatives'); | ||
const obj = Alternatives.when(ref, { is: options.is, then, otherwise }); | ||
const alternativeOptions = { then, otherwise }; | ||
if (Object.prototype.hasOwnProperty.call(options, 'is')) { | ||
alternativeOptions.is = options.is; | ||
} | ||
const obj = Alternatives.when(condition, alternativeOptions); | ||
obj._flags.presence = 'ignore'; | ||
@@ -401,0 +406,0 @@ obj._baseType = this; |
{ | ||
"name": "joi", | ||
"description": "Object schema validation", | ||
"version": "11.3.4", | ||
"version": "11.4.0", | ||
"homepage": "https://github.com/hapijs/joi", | ||
@@ -6,0 +6,0 @@ "repository": "git://github.com/hapijs/joi", |
@@ -29,3 +29,3 @@ ![joi Logo](https://raw.github.com/hapijs/joi/master/images/joi.png) | ||
# API | ||
See the detailed [API Reference](https://github.com/hapijs/joi/blob/v11.3.4/API.md). | ||
See the detailed [API Reference](https://github.com/hapijs/joi/blob/v11.4.0/API.md). | ||
@@ -32,0 +32,0 @@ # Example |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
173706
4083