mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-07 00:07:07 +00:00
[Joi] - add schema option to .when (#25136)
This commit is contained in:
Vendored
+15
@@ -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
|
||||
|
||||
@@ -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<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.
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user