[joi] Correct the validate function (#12306)

* Correct the validate function

* I lookup source code to correct validate func

* Find the correct type assert for validate

* According to new definition, to update tests

* Delete redundant function

* Enhance test completeness

* Fix the error
This commit is contained in:
TonyYang
2016-10-28 23:54:55 +08:00
committed by Masahiro Wakame
parent dc17e1a247
commit 0ad40bb042
2 changed files with 63 additions and 27 deletions

View File

@@ -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<typeof value>;
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);
schema = Joi.empty(anySchema);

15
joi/joi.d.ts vendored
View File

@@ -842,9 +842,18 @@ declare module 'joi' {
/**
* Validates a value using the given schema and options.
*/
export function validate<T>(value: T, schema: Schema, callback: (err: ValidationError, value: T) => void): void;
export function validate<T>(value: T, schema: Object, callback: (err: ValidationError, value: T) => void): void;
export function validate<T>(value: T, schema: Object, options?: ValidationOptions, callback?: (err: ValidationError, value: T) => void): ValidationResult<T>;
export function validate<T>(value: T): ValidationResult<T>;
export function validate<T, R>(value: T, callback: (err: ValidationError, value: T) => R): R;
export function validate<T>(value: T, schema: Schema): ValidationResult<T>;
export function validate<T>(value: T, schema: Object): ValidationResult<T>;
export function validate<T, R>(value: T, schema: Schema, callback: (err: ValidationError, value: T) => R): R;
export function validate<T, R>(value: T, schema: Object, callback: (err: ValidationError, value: T) => R): R;
export function validate<T>(value: T, schema: Schema, options: ValidationOptions): ValidationResult<T>;
export function validate<T>(value: T, schema: Object, options: ValidationOptions): ValidationResult<T>;
export function validate<T, R>(value: T, schema: Schema, options: ValidationOptions, callback: (err: ValidationError, value: T) => R): R;
export function validate<T, R>(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).