mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
[@types/hapi__joi]: implement validate value return types (#41788)
This commit is contained in:
parent
9c11476da2
commit
636c69bbd2
@ -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<number>().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<number | string>(Joi.number(), Joi.string()).validate(1).value;
|
||||
}
|
||||
|
||||
// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
|
||||
|
||||
schema = Joi.compile(obj);
|
||||
|
||||
41
types/hapi__joi/index.d.ts
vendored
41
types/hapi__joi/index.d.ts
vendored
@ -20,6 +20,7 @@
|
||||
// Anand Chowdhary <https://github.com/AnandChowdhary>
|
||||
// Miro Yovchev <https://github.com/myovchev>
|
||||
// David Recuenco <https://github.com/RecuencoJones>
|
||||
// Stefan-Gabriel Muscalu <https://github.com/legraphista>
|
||||
// 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<T = any> {
|
||||
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<T = any>(value: any, state: State, prefs: ValidationOptions): ValidationResult<T>;
|
||||
}
|
||||
|
||||
interface AnySchema extends SchemaInternals {
|
||||
interface AnySchema<TSchema = any> 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<T = TSchema>(value: any, options?: ValidationOptions): ValidationResult<T>;
|
||||
|
||||
/**
|
||||
* Validates a value using the schema and options.
|
||||
*/
|
||||
validateAsync(value: any, options?: AsyncValidationOptions): Promise<any>;
|
||||
// tslint:disable-next-line:no-unnecessary-generics
|
||||
validateAsync<T = TSchema>(value: any, options?: AsyncValidationOptions): Promise<T>;
|
||||
|
||||
/**
|
||||
* Same as `rule({ warn: true })`.
|
||||
@ -1145,7 +1149,7 @@ declare namespace Joi {
|
||||
localize?(...args: any[]): State;
|
||||
}
|
||||
|
||||
interface BooleanSchema extends AnySchema {
|
||||
interface BooleanSchema extends AnySchema<boolean> {
|
||||
/**
|
||||
* 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<string | number>): this;
|
||||
}
|
||||
|
||||
interface NumberSchema extends AnySchema {
|
||||
interface NumberSchema extends AnySchema<number> {
|
||||
/**
|
||||
* 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<string> {
|
||||
/**
|
||||
* 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<symbol> {
|
||||
// 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<TArrayValueSchema = any> extends AnySchema<TArrayValueSchema[]> {
|
||||
/**
|
||||
* 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<TSchema = any> extends AnySchema {
|
||||
interface ObjectSchema<TSchema = any> extends AnySchema<TSchema> {
|
||||
/**
|
||||
* 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<Date> {
|
||||
/**
|
||||
* 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<TAlternativesSchema = any> extends AnySchema<TAlternativesSchema> {
|
||||
/**
|
||||
* 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<T>(): ArraySchema<T>;
|
||||
|
||||
/**
|
||||
* 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<T = any>(types: SchemaLike[]): AlternativesSchema<T>;
|
||||
// tslint:disable-next-line:no-unnecessary-generics
|
||||
alternatives<T = any>(...types: SchemaLike[]): AlternativesSchema<T>;
|
||||
|
||||
/**
|
||||
* Alias for `alternatives`
|
||||
|
||||
Loading…
Reference in New Issue
Block a user