diff --git a/joi/joi-tests.ts b/joi/joi-tests.ts index c832b3eb05..7002a3872c 100644 --- a/joi/joi-tests.ts +++ b/joi/joi-tests.ts @@ -759,30 +759,57 @@ schema = Joi.lazy(() => schema) // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- -Joi.validate(value, obj); -Joi.validate(value, schema); -Joi.validate(value, schema, validOpts); -Joi.validate(value, schema, validOpts, (err, value) => { - x = value; - str = err.message; - str = err.details[0].path; - str = err.details[0].message; - str = err.details[0].type; -}); -Joi.validate(value, schema, (err, value) => { - x = value; - str = err.message; - str = err.details[0].path; - str = err.details[0].message; - str = err.details[0].type; -}); -// variant -Joi.validate(num, schema, validOpts, (err, value) => { - num = value; -}); +namespace validate_tests { + { + Joi.validate(value, obj); + Joi.validate(value, schema); + Joi.validate(value, schema, validOpts); + Joi.validate(value, schema, validOpts, (err, value) => { + x = value; + str = err.message; + str = err.details[0].path; + str = err.details[0].message; + str = err.details[0].type; + }); + Joi.validate(value, schema, (err, value) => { + x = value; + str = err.message; + str = err.details[0].path; + str = err.details[0].message; + str = err.details[0].type; + }); + // variant + Joi.validate(num, schema, validOpts, (err, value) => { + num = value; + }); + + // plain opts + Joi.validate(value, {}); + } + + { + let value = { username: 'example', password: 'example' }; + let schema = Joi.object().keys({ + username: Joi.string().max(255).required(), + password: Joi.string().regex(/^[a-zA-Z0-9]{3,255}$/).required(), + }); + let returnValue: Joi.ValidationResult; + + returnValue = Joi.validate(value); + value = Joi.validate(value, (err, value) => value); + + returnValue = Joi.validate(value, schema); + returnValue = Joi.validate(value, obj); + value = Joi.validate(value, obj, (err, value) => value); + value = Joi.validate(value, schema, (err, value) => value); + + returnValue = Joi.validate(value, schema, validOpts); + returnValue = Joi.validate(value, obj, validOpts); + value = Joi.validate(value, obj, validOpts, (err, value) => value); + value = Joi.validate(value, schema, validOpts, (err, value) => value); + } +} -// plain opts -Joi.validate(value, {}); // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- @@ -856,4 +883,4 @@ schema = Joi.raw(); schema = Joi.raw(bool); schema = Joi.empty(); schema = Joi.empty(str); -schema = Joi.empty(anySchema); \ No newline at end of file +schema = Joi.empty(anySchema); diff --git a/joi/joi.d.ts b/joi/joi.d.ts index a4a029f737..9a7d73cbbf 100644 --- a/joi/joi.d.ts +++ b/joi/joi.d.ts @@ -842,9 +842,18 @@ declare module 'joi' { /** * Validates a value using the given schema and options. */ - export function validate(value: T, schema: Schema, callback: (err: ValidationError, value: T) => void): void; - export function validate(value: T, schema: Object, callback: (err: ValidationError, value: T) => void): void; - export function validate(value: T, schema: Object, options?: ValidationOptions, callback?: (err: ValidationError, value: T) => void): ValidationResult; + export function validate(value: T): ValidationResult; + export function validate(value: T, callback: (err: ValidationError, value: T) => R): R; + + export function validate(value: T, schema: Schema): ValidationResult; + export function validate(value: T, schema: Object): ValidationResult; + export function validate(value: T, schema: Schema, callback: (err: ValidationError, value: T) => R): R; + export function validate(value: T, schema: Object, callback: (err: ValidationError, value: T) => R): R; + + export function validate(value: T, schema: Schema, options: ValidationOptions): ValidationResult; + export function validate(value: T, schema: Object, options: ValidationOptions): ValidationResult; + export function validate(value: T, schema: Schema, options: ValidationOptions, callback: (err: ValidationError, value: T) => R): R; + export function validate(value: T, schema: Object, options: ValidationOptions, callback: (err: ValidationError, value: T) => R): R; /** * Converts literal schema definition to joi schema object (or returns the same back if already a joi schema object).