diff --git a/types/joi/index.d.ts b/types/joi/index.d.ts index fb3547b3e4..0772c82fbe 100644 --- a/types/joi/index.d.ts +++ b/types/joi/index.d.ts @@ -2,11 +2,11 @@ // Project: https://github.com/hapijs/joi // Definitions by: Bart van der Schoor , Laurence Dougal Myers , Christopher Glantschnig , David Broder-Rodgers , Gael Magnan de Bornier , Rytis Alekna , Pavel Ivanov // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 // TODO express type of Schema in a type-parameter (.default, .valid, .example etc) - export interface ValidationOptions { /** * when true, stops validation on the first error, otherwise returns all the errors found. Defaults to true. @@ -337,6 +337,25 @@ export interface AnySchema> { * option has been set to `false`). */ error?(err: Error | ValidationErrorFunction): T; + + /** + * Creates a joi error object. + * Used in conjunction with custom rules. + * @param ruleName - the rule name to create the error for. + * @param context - must contain `v` eg. the value that was validated, all other properties + * are available in the `language` templates. + * @param state - should the context passed into the `validate` function in a + * custom rule + * @param options - should the context passed into the `validate` function in a + * custom rule + */ + createError(ruleName: string, context: { + v: any; + [key: string]: any; + }, state: State, options: ValidationOptions): Err; +} + +export interface State { } export interface BooleanSchema extends AnySchema { @@ -825,20 +844,26 @@ export interface Terms { export interface Rules { name: string; params?: ObjectSchema | { [key: string]: Schema }; - setup?: Function; - validate?: Function; + setup?: (this: AnySchema>, description: { [key: string]: any }) => AnySchema> | void; + validate?: (this: AnySchema>, params: { [key: string]: any }, value: any, state: State, options: ValidationOptions) => Err | undefined; description?: string | Function; } export interface Extension { name: string; base?: Schema; - pre?: Function; - language?: {}; - describe?: Function; + pre?: (this: AnySchema>, params: { [key: string]: any }, value: any, state: State, options: ValidationOptions) => Err | void; + language?: { + [key: string]: string; + }, + describe?: (this: AnySchema>, description: any) => any; rules?: Rules[]; } +export interface Err { + toString(): string; +} + // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- /** diff --git a/types/joi/joi-tests.ts b/types/joi/joi-tests.ts index a0684724b7..7bfb712247 100644 --- a/types/joi/joi-tests.ts +++ b/types/joi/joi-tests.ts @@ -886,6 +886,35 @@ schema = Joi.reach(schema, ''); const Joi2 = Joi.extend({ name: '', base: schema }); +const Joi3 = Joi.extend({ + base: Joi.string(), + name: 'string', + language: { + asd: 'must be exactly asd(f)', + }, + pre (value, state, options) { + }, + describe (description) { + }, + rules: [ + { + name: 'asd', + params: { + allowF: Joi.boolean().default(false), + }, + setup (params) { + const fIsAllowed = params.allowF; + }, + validate (params, value, state, options) { + if (value === 'asd' && (params.allowF && value === 'asdf')) { + return value; + } + return this.createError('asd', { v: value }, state, options); + }, + }, + ], +}); + // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- schema = Joi.allow(x, x);