From ee2f1b215e2b3833dbe8ef7d8878d893303a6548 Mon Sep 17 00:00:00 2001 From: Conan Date: Thu, 19 Apr 2018 19:42:37 -0400 Subject: [PATCH] [Joi] - add schema option to .when (#25136) --- types/joi/v10/index.d.ts | 15 +++++++++++++++ types/joi/v10/joi-tests.ts | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/types/joi/v10/index.d.ts b/types/joi/v10/index.d.ts index 9270c411aa..e21edda875 100644 --- a/types/joi/v10/index.d.ts +++ b/types/joi/v10/index.d.ts @@ -8,6 +8,7 @@ // Rytis Alekna // Pavel Ivanov // Youngrok Kim +// Conan Lai // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.4 @@ -142,6 +143,17 @@ export interface WhenOptions { otherwise?: SchemaLike; } +export interface WhenSchemaOptions { + /** + * the alternative schema type if the condition is true. Required if otherwise is missing. + */ + then?: SchemaLike; + /** + * the alternative schema type if the condition is false. Required if then is missing + */ + otherwise?: SchemaLike; +} + export interface ReferenceOptions { separator?: string; contextPrefix?: string; @@ -327,6 +339,7 @@ export interface AnySchema extends JoiObject { */ when(ref: string, options: WhenOptions): AlternativesSchema; when(ref: Reference, options: WhenOptions): AlternativesSchema; + when(ref: Schema, options: WhenSchemaOptions): AlternativesSchema; /** * Overrides the key name in error messages. @@ -878,6 +891,7 @@ export interface AlternativesSchema extends AnySchema { try(...types: SchemaLike[]): this; when(ref: string, options: WhenOptions): this; when(ref: Reference, options: WhenOptions): this; + when(ref: Schema, options: WhenSchemaOptions): this; } export interface LazySchema extends AnySchema { @@ -1160,6 +1174,7 @@ export function concat(schema: T): T; */ export function when(ref: string, options: WhenOptions): AlternativesSchema; export function when(ref: Reference, options: WhenOptions): AlternativesSchema; +export function when(ref: Schema, options: WhenSchemaOptions): AlternativesSchema; /** * Overrides the key name in error messages. diff --git a/types/joi/v10/joi-tests.ts b/types/joi/v10/joi-tests.ts index bfc6268ef7..b1f125a6ea 100644 --- a/types/joi/v10/joi-tests.ts +++ b/types/joi/v10/joi-tests.ts @@ -122,6 +122,14 @@ whenOpts = { is: schemaLike, then: schemaLike, otherwise: schemaLike }; // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- +var whenSchemaOpts: Joi.WhenSchemaOptions = null; + +whenSchemaOpts = { then: schema }; +whenSchemaOpts = { otherwise: schema }; +whenSchemaOpts = { then: schemaLike, otherwise: schemaLike }; + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + var refOpts: Joi.ReferenceOptions = null; refOpts = { separator: str }; @@ -263,6 +271,7 @@ namespace common { altSchema = anySchema.when(str, whenOpts); altSchema = anySchema.when(ref, whenOpts); + altSchema = anySchema.when(schema, whenSchemaOpts); anySchema = anySchema.label(str); anySchema = anySchema.raw(); @@ -350,6 +359,7 @@ namespace common_copy_paste { altSchema = arrSchema.when(str, whenOpts); altSchema = arrSchema.when(ref, whenOpts); + altSchema = arrSchema.when(schema, whenSchemaOpts); } // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- @@ -420,6 +430,7 @@ namespace common_copy_paste { altSchema = boolSchema.when(str, whenOpts); altSchema = boolSchema.when(ref, whenOpts); + altSchema = boolSchema.when(schema, whenSchemaOpts); } // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- @@ -476,6 +487,7 @@ namespace common { altSchema = binSchema.when(str, whenOpts); altSchema = binSchema.when(ref, whenOpts); + altSchema = binSchema.when(schema, whenSchemaOpts); } // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- @@ -548,6 +560,7 @@ namespace common { altSchema = dateSchema.when(str, whenOpts); altSchema = dateSchema.when(ref, whenOpts); + altSchema = dateSchema.when(schema, whenSchemaOpts); } // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- @@ -622,6 +635,7 @@ namespace common { altSchema = numSchema.when(str, whenOpts); altSchema = numSchema.when(ref, whenOpts); + altSchema = numSchema.when(schema, whenSchemaOpts); } // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- @@ -732,6 +746,7 @@ namespace common { altSchema = objSchema.when(str, whenOpts); altSchema = objSchema.when(ref, whenOpts); + altSchema = objSchema.when(schema, whenSchemaOpts); } // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- @@ -823,6 +838,7 @@ namespace common { altSchema = strSchema.when(str, whenOpts); altSchema = strSchema.when(ref, whenOpts); + altSchema = strSchema.when(schema, whenSchemaOpts); } // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- @@ -1007,6 +1023,7 @@ schema = Joi.concat(x); schema = Joi.when(str, whenOpts); schema = Joi.when(ref, whenOpts); +schema = Joi.when(schema, whenSchemaOpts); schema = Joi.label(str); schema = Joi.raw();