Skip to content
Open
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
36 changes: 23 additions & 13 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2503,7 +2503,7 @@ declare namespace Joi {
/**
* Creates a custom validation schema.
*/
custom(fn: CustomValidator, description?: string): Schema;
custom(fn: CustomValidator, description?: string): AnySchema;

/**
* Creates a new Joi instance that will apply defaults onto newly created schemas
Expand Down Expand Up @@ -2590,50 +2590,60 @@ declare namespace Joi {
/**
* Whitelists a value
*/
allow(...values: any[]): Schema;
allow(...values: any[]): AnySchema;

/**
* Adds the provided values into the allowed whitelist and marks them as the only valid values allowed.
*/
valid(...values: any[]): Schema;
equal(...values: any[]): Schema;
valid(...values: any[]): AnySchema;
equal(...values: any[]): AnySchema;

/**
* Blacklists a value
*/
invalid(...values: any[]): Schema;
disallow(...values: any[]): Schema;
not(...values: any[]): Schema;
invalid(...values: any[]): AnySchema;
disallow(...values: any[]): AnySchema;
not(...values: any[]): AnySchema;

/**
* Marks a key as required which will not allow undefined as value. All keys are optional by default.
*/
required(): Schema;
required(): AnySchema;

/**
* Alias of `required`.
*/
exist(): Schema;
exist(): AnySchema;

/**
* Marks a key as optional which will allow undefined as values. Used to annotate the schema for readability as all keys are optional by default.
*/
optional(): Schema;
optional(): AnySchema;

/**
* Marks a key as forbidden which will not allow any value except undefined. Used to explicitly forbid keys.
*/
forbidden(): Schema;
forbidden(): AnySchema;

/**
* Overrides the global validate() options for the current key and any sub-key.
*/
preferences(options: ValidationOptions): Schema;
preferences(options: ValidationOptions): AnySchema;

/**
* Overrides the global validate() options for the current key and any sub-key.
*/
prefs(options: ValidationOptions): Schema;
prefs(options: ValidationOptions): AnySchema;

/**
* Requires the validated value to match of the provided `any.allow()` values.
*/
only(): AnySchema;

/**
* Marks a key to be removed from a resulting object or array after validation. Used to sanitize output.
*/
strip(enabled?: boolean): AnySchema;

/**
* Converts the type into an alternatives type where the conditions are merged into the type definition where:
Expand Down
18 changes: 18 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,24 @@ schema = Joi.forbidden();

schema = Joi.preferences(validOpts);

// Root shorthand methods return AnySchema, enabling method chaining

anySchema = Joi.allow(x).required();
anySchema = Joi.valid(x).optional();
anySchema = Joi.equal(x).strip();
anySchema = Joi.invalid(x).required();
anySchema = Joi.disallow(x).forbidden();
anySchema = Joi.not(x).optional();
anySchema = Joi.required().allow(x);
anySchema = Joi.exist().valid(x);
anySchema = Joi.optional().invalid(x);
anySchema = Joi.forbidden().allow(x);
anySchema = Joi.preferences(validOpts).required();
anySchema = Joi.prefs(validOpts).required();
anySchema = Joi.only().valid(x);
anySchema = Joi.strip().allow(x);
anySchema = Joi.custom((value) => value).required();

schema = Joi.when(str, whenOpts);
schema = Joi.when(ref, whenOpts);
schema = Joi.when(schema, whenSchemaOpts);
Expand Down