joi: Improve extend typings (#18760)

* joi: Improve `extend` typings

* fixup! joi: Improve `extend` typings

* fixup! joi: Improve `extend` typings

* use TS 2.4
This commit is contained in:
Simon Schick
2017-08-09 10:56:27 -07:00
committed by Mohamed Hegazy
parent 34aff41c75
commit 135ed17a23
2 changed files with 60 additions and 6 deletions
+31 -6
View File
@@ -2,11 +2,11 @@
// Project: https://github.com/hapijs/joi
// Definitions by: Bart van der Schoor <https://github.com/Bartvds>, Laurence Dougal Myers <https://github.com/laurence-myers>, Christopher Glantschnig <https://github.com/cglantschnig>, David Broder-Rodgers <https://github.com/DavidBR-SW>, Gael Magnan de Bornier <https://github.com/GaelMagnan>, Rytis Alekna <https://github.com/ralekna>, Pavel Ivanov <https://github.com/schfkt>
// 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<T extends AnySchema<Schema>> {
* 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<BooleanSchema> {
@@ -825,20 +844,26 @@ export interface Terms {
export interface Rules {
name: string;
params?: ObjectSchema | { [key: string]: Schema };
setup?: Function;
validate?: Function;
setup?: (this: AnySchema<AnySchema<Schema>>, description: { [key: string]: any }) => AnySchema<AnySchema<Schema>> | void;
validate?: (this: AnySchema<AnySchema<Schema>>, 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<AnySchema<Schema>>, params: { [key: string]: any }, value: any, state: State, options: ValidationOptions) => Err | void;
language?: {
[key: string]: string;
},
describe?: (this: AnySchema<AnySchema<Schema>>, description: any) => any;
rules?: Rules[];
}
export interface Err {
toString(): string;
}
// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
/**
+29
View File
@@ -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);