Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions types/joi/v10/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// Rytis Alekna <https://github.com/ralekna>
// Pavel Ivanov <https://github.com/schfkt>
// Youngrok Kim <https://github.com/rokoroku>
// Conan Lai <https://github.com/aconanlai>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -1160,6 +1174,7 @@ export function concat<T>(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.
Expand Down
17 changes: 17 additions & 0 deletions types/joi/v10/joi-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -350,6 +359,7 @@ namespace common_copy_paste {

altSchema = arrSchema.when(str, whenOpts);
altSchema = arrSchema.when(ref, whenOpts);
altSchema = arrSchema.when(schema, whenSchemaOpts);
}

// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
Expand Down Expand Up @@ -420,6 +430,7 @@ namespace common_copy_paste {

altSchema = boolSchema.when(str, whenOpts);
altSchema = boolSchema.when(ref, whenOpts);
altSchema = boolSchema.when(schema, whenSchemaOpts);
}

// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
Expand Down Expand Up @@ -476,6 +487,7 @@ namespace common {

altSchema = binSchema.when(str, whenOpts);
altSchema = binSchema.when(ref, whenOpts);
altSchema = binSchema.when(schema, whenSchemaOpts);
}

// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
Expand Down Expand Up @@ -548,6 +560,7 @@ namespace common {

altSchema = dateSchema.when(str, whenOpts);
altSchema = dateSchema.when(ref, whenOpts);
altSchema = dateSchema.when(schema, whenSchemaOpts);
}

// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
Expand Down Expand Up @@ -622,6 +635,7 @@ namespace common {

altSchema = numSchema.when(str, whenOpts);
altSchema = numSchema.when(ref, whenOpts);
altSchema = numSchema.when(schema, whenSchemaOpts);
}

// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
Expand Down Expand Up @@ -732,6 +746,7 @@ namespace common {

altSchema = objSchema.when(str, whenOpts);
altSchema = objSchema.when(ref, whenOpts);
altSchema = objSchema.when(schema, whenSchemaOpts);
}

// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
Expand Down Expand Up @@ -823,6 +838,7 @@ namespace common {

altSchema = strSchema.when(str, whenOpts);
altSchema = strSchema.when(ref, whenOpts);
altSchema = strSchema.when(schema, whenSchemaOpts);
}

// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
Expand Down Expand Up @@ -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();
Expand Down