diff --git a/lib/index.d.ts b/lib/index.d.ts index 39a1f855..2f62be6b 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -27,7 +27,7 @@ declare namespace Joi { type BasicType = boolean | number | string | any[] | object | null; - type LanguageMessages = Record>; + type LanguageMessages = Record>; type PresenceMode = 'optional' | 'required' | 'forbidden'; @@ -650,6 +650,10 @@ declare namespace Joi { render?: boolean; } + interface ExpressionOptions extends ReferenceOptions { + functions?: Record unknown> + } + interface StringRegexOptions { /** * optional pattern name. @@ -2003,6 +2007,11 @@ declare namespace Joi { toString(): string; } + interface Template { + render(value: any, state: State, prefs: any, local: any, options?: any): string + toString(): string; + } + type ExtensionBoundSchema = Schema & SchemaInternals; interface RuleArgs { @@ -2276,7 +2285,7 @@ declare namespace Joi { /** * Generates a dynamic expression using a template string. */ - expression(template: string, options?: ReferenceOptions): any; + expression(template: string, options?: ExpressionOptions): Template; /** * Creates a new Joi instance customized with the extension(s) you provide included. @@ -2340,8 +2349,9 @@ declare namespace Joi { /** * Generates a dynamic expression using a template string. + * @see {@link Root.expression} */ - x(template: string, options?: ReferenceOptions): any; + x(template: string, options?: ExpressionOptions): Template; // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- // Below are undocumented APIs. use at your own risk diff --git a/test/index.ts b/test/index.ts index eaac929b..48a93d11 100644 --- a/test/index.ts +++ b/test/index.ts @@ -69,6 +69,7 @@ validOpts = { messages: { 'any.ref': str, 'string.email': str, + 'any.custom': Joi.x('{{.value}}') }, dateFormat: 'iso', }; @@ -999,6 +1000,8 @@ expr = Joi.expression('{{foo}}', { iterables: true }); expr = Joi.expression('{{foo}}', { map: [['key', 'value']] }); expr = Joi.expression('{{foo}}', { prefix: { local: '%' } }); expr = Joi.expression('{{foo}}', { separator: '_' }); +expr = Joi.expression('{{foo}}', { functions: { foo: (...args) => 'return' } }); +expect.type({} as ReturnType['render']>) expr = Joi.x('{{foo}}'); expr = Joi.x('{{foo}}', { adjust: (value) => value }); @@ -1008,6 +1011,8 @@ expr = Joi.x('{{foo}}', { iterables: true }); expr = Joi.x('{{foo}}', { map: [['key', 'value']] }); expr = Joi.x('{{foo}}', { prefix: { local: '%' } }); expr = Joi.x('{{foo}}', { separator: '_' }); +expr = Joi.x('{{foo}}', { functions: { foo: (...args) => 'return' } }); +expect.type({} as ReturnType['render']>) // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---