diff --git a/types/hapi__joi/hapi__joi-tests.ts b/types/hapi__joi/hapi__joi-tests.ts index b40793edea..c7535d3c9f 100644 --- a/types/hapi__joi/hapi__joi-tests.ts +++ b/types/hapi__joi/hapi__joi-tests.ts @@ -955,6 +955,19 @@ schema = Joi.link(str); } } +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- +// validate result types +{ + const _boolTypeTest: boolean = Joi.boolean().validate(true).value; + const _numberTypeTest: number = Joi.number().validate(1337).value; + const _stringTypeTest: string = Joi.string().validate('hello world').value; + const _symbolTypeTest: symbol = Joi.symbol().validate(Symbol('hello world')).value; + const _arrayTypeTest: number[] = Joi.array().items(Joi.number()).validate([1, 2, 3]).value; + const _objectTypeTest: { key: string } = Joi.object<{ key: string }>({ key: Joi.string() }).validate({}).value; + const _dateTypeTest: Date = Joi.date().validate(new Date()).value; + const _alternativeType: number | string = Joi.alternatives(Joi.number(), Joi.string()).validate(1).value; +} + // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- schema = Joi.compile(obj); diff --git a/types/hapi__joi/index.d.ts b/types/hapi__joi/index.d.ts index dfa805f661..6444ce5b6b 100644 --- a/types/hapi__joi/index.d.ts +++ b/types/hapi__joi/index.d.ts @@ -20,6 +20,7 @@ // Anand Chowdhary // Miro Yovchev // David Recuenco +// Stefan-Gabriel Muscalu // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 @@ -614,11 +615,11 @@ declare namespace Joi { type ValidationErrorFunction = (errors: ValidationErrorItem[]) => string | ValidationErrorItem | Error; - interface ValidationResult { + interface ValidationResult { error: ValidationError; errors: ValidationError; warning: ValidationError; - value: any; + value: T; } interface CreateErrorOptions { @@ -752,10 +753,11 @@ declare namespace Joi { /** * Runs internal validations against given value. */ - $_validate(value: any, state: State, prefs: ValidationOptions): ValidationResult; + // tslint:disable-next-line:no-unnecessary-generics + $_validate(value: any, state: State, prefs: ValidationOptions): ValidationResult; } - interface AnySchema extends SchemaInternals { + interface AnySchema extends SchemaInternals { /** * Flags of current schema. */ @@ -1080,12 +1082,14 @@ declare namespace Joi { /** * Validates a value using the schema and options. */ - validate(value: any, options?: ValidationOptions): ValidationResult; + // tslint:disable-next-line:no-unnecessary-generics + validate(value: any, options?: ValidationOptions): ValidationResult; /** * Validates a value using the schema and options. */ - validateAsync(value: any, options?: AsyncValidationOptions): Promise; + // tslint:disable-next-line:no-unnecessary-generics + validateAsync(value: any, options?: AsyncValidationOptions): Promise; /** * Same as `rule({ warn: true })`. @@ -1145,7 +1149,7 @@ declare namespace Joi { localize?(...args: any[]): State; } - interface BooleanSchema extends AnySchema { + interface BooleanSchema extends AnySchema { /** * Allows for additional values to be considered valid booleans by converting them to false during validation. * String comparisons are by default case insensitive, @@ -1168,7 +1172,7 @@ declare namespace Joi { truthy(...values: Array): this; } - interface NumberSchema extends AnySchema { + interface NumberSchema extends AnySchema { /** * Specifies that the value must be greater than limit. * It can also be a reference to another field. @@ -1235,7 +1239,7 @@ declare namespace Joi { unsafe(enabled?: any): this; } - interface StringSchema extends AnySchema { + interface StringSchema extends AnySchema { /** * Requires the string value to only contain a-z, A-Z, and 0-9. */ @@ -1400,7 +1404,7 @@ declare namespace Joi { uuid(options?: GuidOptions): this; } - interface SymbolSchema extends AnySchema { + interface SymbolSchema extends AnySchema { // TODO: support number and symbol index map(iterable: Iterable<[string | number | boolean | symbol, symbol]> | { [key: string]: symbol }): this; } @@ -1424,7 +1428,7 @@ declare namespace Joi { type ComparatorFunction = (a: any, b: any) => boolean; - interface ArraySchema extends AnySchema { + interface ArraySchema extends AnySchema { /** * Verifies that an assertion passes for at least one item in the array, where: * `schema` - the validation rules required to satisfy the assertion. If the `schema` includes references, they are resolved against @@ -1500,7 +1504,7 @@ declare namespace Joi { matches: SchemaLike | Reference; } - interface ObjectSchema extends AnySchema { + interface ObjectSchema extends AnySchema { /** * Defines an all-or-nothing relationship between keys where if one of the peers is present, all of them are required as well. * @@ -1636,7 +1640,7 @@ declare namespace Joi { length(limit: number | Reference): this; } - interface DateSchema extends AnySchema { + interface DateSchema extends AnySchema { /** * Specifies that the value must be greater than date. * Notes: 'now' can be passed in lieu of date so as to always compare relatively to the current date, @@ -1706,7 +1710,7 @@ declare namespace Joi { maxArity(n: number): this; } - interface AlternativesSchema extends AnySchema { + interface AlternativesSchema extends AnySchema { /** * Adds a conditional alternative schema type, either based on another key value, or a schema peeking into the current value. */ @@ -1877,7 +1881,8 @@ declare namespace Joi { /** * Generates a schema object that matches an array data type. */ - array(): ArraySchema; + // tslint:disable-next-line:no-unnecessary-generics + array(): ArraySchema; /** * Generates a schema object that matches a boolean data type (as well as the strings 'true', 'false', 'yes', and 'no'). Can also be called via bool(). @@ -1933,8 +1938,10 @@ declare namespace Joi { /** * Generates a type that will match one of the provided alternative schemas */ - alternatives(types: SchemaLike[]): AlternativesSchema; - alternatives(...types: SchemaLike[]): AlternativesSchema; + // tslint:disable-next-line:no-unnecessary-generics + alternatives(types: SchemaLike[]): AlternativesSchema; + // tslint:disable-next-line:no-unnecessary-generics + alternatives(...types: SchemaLike[]): AlternativesSchema; /** * Alias for `alternatives`