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
16 changes: 13 additions & 3 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ declare namespace Joi {

type BasicType = boolean | number | string | any[] | object | null;

type LanguageMessages = Record<string, string | Record<string, string>>;
type LanguageMessages = Record<string, string | Template | Record<string, string | Template>>;

type PresenceMode = 'optional' | 'required' | 'forbidden';

Expand Down Expand Up @@ -650,6 +650,10 @@ declare namespace Joi {
render?: boolean;
}

interface ExpressionOptions extends ReferenceOptions {
functions?: Record<string, (...args: unknown[]) => unknown>
}

interface StringRegexOptions {
/**
* optional pattern name.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ validOpts = {
messages: {
'any.ref': str,
'string.email': str,
'any.custom': Joi.x('{{.value}}')
},
dateFormat: 'iso',
};
Expand Down Expand Up @@ -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<string>({} as ReturnType<ReturnType<typeof Joi.expression>['render']>)

expr = Joi.x('{{foo}}');
expr = Joi.x('{{foo}}', { adjust: (value) => value });
Expand All @@ -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<string>({} as ReturnType<ReturnType<typeof Joi.x>['render']>)

// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

Expand Down