@naturalcycles/nodejs-lib
Version:
Standard library for Node.js
35 lines (34 loc) • 1.39 kB
TypeScript
import { ValidationOptions } from 'joi';
import { AnySchemaTyped } from './joi.model';
import { JoiValidationError } from './joi.validation.error';
export interface JoiValidationResult<T = any> {
value: T;
error?: JoiValidationError;
}
/**
* Validates with Joi.
* Throws JoiValidationError if invalid.
* Returns *converted* value.
*
* If `schema` is undefined - returns value as is.
*/
export declare function validate<IN, OUT = IN>(value: IN, schema?: AnySchemaTyped<IN, OUT>, objectName?: string, options?: ValidationOptions): OUT;
/**
* Validates with Joi.
* Returns JoiValidationResult with converted value and error (if any).
* Does not throw.
*
* If `schema` is undefined - returns value as is.
*/
export declare function getValidationResult<IN, OUT = IN>(value: IN, schema?: AnySchemaTyped<IN, OUT>, objectName?: string, options?: ValidationOptions): JoiValidationResult<OUT>;
/**
* Convenience function that returns true if !error.
*/
export declare function isValid<IN, OUT = IN>(value: IN, schema?: AnySchemaTyped<IN, OUT>): boolean;
export declare function undefinedIfInvalid<IN, OUT = IN>(value: IN, schema?: AnySchemaTyped<IN, OUT>): OUT | undefined;
/**
* Will do joi-convertation, regardless of error/validity of value.
*
* @returns converted value
*/
export declare function convert<IN, OUT = IN>(value: IN, schema?: AnySchemaTyped<IN, OUT>): OUT;