diff --git a/types/hapi__joi/hapi__joi-tests.ts b/types/hapi__joi/hapi__joi-tests.ts index a7743e877e..0ccbe141ca 100644 --- a/types/hapi__joi/hapi__joi-tests.ts +++ b/types/hapi__joi/hapi__joi-tests.ts @@ -54,13 +54,17 @@ validOpts = { context: obj }; validOpts = { noDefaults: bool }; validOpts = { abortEarly: true, - errors: { wrapArrays: bool }, messages: { 'any.ref': str, 'string.email': str }, dateFormat: 'iso' }; +// Test various permutations of string, `false`, or `undefined` for both parameters: +validOpts = { errors: { wrap: { label: str, array: str }}}; +validOpts = { errors: { wrap: { label: false, array: false }}}; +validOpts = { errors: { wrap: { label: str }}}; +validOpts = { errors: { wrap: { array: str }}}; // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- @@ -162,7 +166,7 @@ stringRegexOpts = { invert: bool }; // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- -declare const validErr: Joi.ValidationError; +const validErr = new Joi.ValidationError("message", "details", "original"); let validErrItem: Joi.ValidationErrorItem; let validErrFunc: Joi.ValidationErrorFunction; @@ -205,6 +209,14 @@ validErrFunc = errors => { Joi.any().error(validErrFunc); +Joi.isError(validErr); + +const maybeValidErr = new Joi.ValidationError("message", "details", "original"); +if (Joi.isError(maybeValidErr)) { + // isError is a type guard that allows accessing these properties: + maybeValidErr.isJoi; +} + // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- schema = anySchema; @@ -291,6 +303,13 @@ anySchema = Joi.any(); anySchema = anySchema.default(); anySchema = anySchema.default(x); + anySchema = anySchema.default("string"); + anySchema = anySchema.default(3.14); + anySchema = anySchema.default(true); + anySchema = anySchema.default({ foo: "bar" }); + anySchema = anySchema.default((parent, helpers) => { + return helpers.state; + }); anySchema = anySchema.required(); anySchema = anySchema.optional(); @@ -743,6 +762,8 @@ objSchema = objSchema.instance(func, str); objSchema = objSchema.ref(); +objSchema = objSchema.regex(); + { // common objSchema = objSchema.allow(x); objSchema = objSchema.allow(x, x); diff --git a/types/hapi__joi/index.d.ts b/types/hapi__joi/index.d.ts index 144341f5df..a42c3a7e99 100644 --- a/types/hapi__joi/index.d.ts +++ b/types/hapi__joi/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for @hapi/joi 16.0 +// Type definitions for @hapi/joi 17.1 // Project: https://github.com/hapijs/joi // Definitions by: Bart van der Schoor // Laurence Dougal Myers @@ -23,6 +23,7 @@ // Frederic Reisenhauer // Stefan-Gabriel Muscalu // Simcha Wood +// Steven Barnett // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 @@ -43,6 +44,8 @@ declare namespace Joi { | 'string' | 'symbol'; + type BasicType = boolean|number|string|any[]|object|null; + type LanguageMessages = Record; type PresenceMode = 'optional' | 'required' | 'forbidden'; @@ -81,11 +84,25 @@ declare namespace Joi { */ stack?: boolean; /** - * if true, array values in error messages are wrapped in []. - * - * @default true + * overrides the way values are wrapped (e.g. `[]` arround arrays, `""` around labels). + * Each key can be set to a string with one (same character before and after the value) or two characters (first character + * before and second character after), or `false` to disable wrapping. */ - wrapArrays?: boolean; + wrap?: { + /** + * the characters used around `{#label}` references. Defaults to `'"'`. + * + * @default '"' + */ + label?: string|false, + + /** + * the characters used around array avlues. Defaults to `'[]'` + * + * @default '[]' + */ + array?: string|false + }; } interface BaseValidationOptions { @@ -619,6 +636,9 @@ declare namespace Joi { /** * function that returns a string with an annotated version of the object pointing at the places where errors occurred. + * + * NOTE: This method does not exist in browser builds of Joi + * * @param stripColors - if truthy, will strip the colors out of the output. */ annotate(stripColors?: boolean): string; @@ -850,24 +870,22 @@ declare namespace Joi { custom(fn: CustomValidator, description?: string): this; /** - * Sets a default value if the original value is undefined. - * @param value - the value. - * value supports references. - * value may also be a function which returns the default value. - * If value is specified as a function that accepts a single parameter, that parameter will be a context - * object that can be used to derive the resulting value. This clones the object however, which incurs some - * overhead so if you don't need access to the context define your method so that it does not accept any - * parameters. - * Without any value, default has no effect, except for object that will then create nested defaults - * (applying inner defaults of that object). + * Sets a default value if the original value is `undefined` where: + * @param value - the default value. One of: + * - a literal value (string, number, object, etc.) + * - a [references](#refkey-options) + * - a function which returns the default value using the signature `function(parent, helpers)` where: + * - `parent` - a clone of the object containing the value being validated. Note that since specifying a + * `parent` ragument performs cloning, do not declare format arguments if you are not using them. + * - `helpers` - same as thsoe described in [`any.custom()`](anycustomermethod_description) + * + * When called without any `value` on an object schema type, a default value will be automatically generated + * based on the default values of the object keys. * * Note that if value is an object, any changes to the object after `default()` is called will change the * reference and any future assignment. - * - * Additionally, when specifying a method you must either have a description property on your method or the - * second parameter is required. */ - default(value?: any): this; + default(value?: BasicType|Reference|((parent: any, helpers: CustomHelpers) => BasicType|Reference)): this; /** * Returns a plain object representing the schema's rules and properties @@ -1617,6 +1635,11 @@ declare namespace Joi { */ ref(): this; + /** + * Requires the object to be a `RegExp` object. + */ + regex(): this; + /** * Renames a key to another name (deletes the renamed key). */ @@ -1903,7 +1926,7 @@ declare namespace Joi { */ version: string; - ValidationError: ValidationError; + ValidationError: new (message: string, details: any, original: any) => ValidationError; /** * Generates a schema object that matches any data type. @@ -2054,6 +2077,11 @@ declare namespace Joi { */ in(ref: string, options?: ReferenceOptions): Reference; + /** + * Checks whether or not the provided argument is an instance of ValidationError + */ + isError(error: any): error is ValidationError; + /** * Checks whether or not the provided argument is an expression. */