diff --git a/lib/index.d.ts b/lib/index.d.ts index 9904088f..3a4394df 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -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 @@ -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: diff --git a/test/index.ts b/test/index.ts index 15675d7d..5529bc4f 100644 --- a/test/index.ts +++ b/test/index.ts @@ -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);