@naturalcycles/nodejs-lib
Advanced tools
Comparing version 5.2.0 to 5.3.0
@@ -0,1 +1,8 @@ | ||
# [5.3.0](https://github.com/NaturalCycles/nodejs-lib/compare/v5.2.0...v5.3.0) (2019-05-17) | ||
### Features | ||
* **validation:** allow undefined schema ([192e91b](https://github.com/NaturalCycles/nodejs-lib/commit/192e91b)) | ||
# [5.2.0](https://github.com/NaturalCycles/nodejs-lib/compare/v5.1.0...v5.2.0) (2019-05-12) | ||
@@ -2,0 +9,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { SchemaLike, ValidationOptions } from '@hapi/joi'; | ||
import { AnySchema, ValidationOptions } from '@hapi/joi'; | ||
import { JoiValidationError } from './joi.validation.error'; | ||
@@ -11,4 +11,6 @@ export interface JoiValidationResult<T = any> { | ||
* Returns *converted* value. | ||
* | ||
* If `schema` is undefined - returns value as is. | ||
*/ | ||
export declare function validate<T>(value: T, schema: SchemaLike, objectName?: string, options?: ValidationOptions): T; | ||
export declare function validate<T>(value: T, schema?: AnySchema, objectName?: string, options?: ValidationOptions): T; | ||
/** | ||
@@ -18,3 +20,5 @@ * Validates with Joi. | ||
* Does not throw. | ||
* | ||
* If `schema` is undefined - returns value as is. | ||
*/ | ||
export declare function getValidationResult<T>(value: T, schema: SchemaLike, objectName?: string, options?: ValidationOptions): JoiValidationResult<T>; | ||
export declare function getValidationResult<T>(value: T, schema?: AnySchema, objectName?: string, options?: ValidationOptions): JoiValidationResult<T>; |
@@ -33,10 +33,9 @@ "use strict"; | ||
* Returns *converted* value. | ||
* | ||
* If `schema` is undefined - returns value as is. | ||
*/ | ||
function validate(value, schema, objectName, options = {}) { | ||
const { value: returnValue, error } = joi_extensions_1.Joi.validate(value, schema, { | ||
...defaultOptions, | ||
...options, | ||
}); | ||
const { value: returnValue, error } = getValidationResult(value, schema, objectName, options); | ||
if (error) { | ||
throw createError(value, error, objectName); | ||
throw error; | ||
} | ||
@@ -50,4 +49,8 @@ return returnValue; | ||
* Does not throw. | ||
* | ||
* If `schema` is undefined - returns value as is. | ||
*/ | ||
function getValidationResult(value, schema, objectName, options = {}) { | ||
if (!schema) | ||
return { value }; | ||
const { value: returnValue, error } = joi_extensions_1.Joi.validate(value, schema, { | ||
@@ -54,0 +57,0 @@ ...defaultOptions, |
{ | ||
"name": "@naturalcycles/nodejs-lib", | ||
"version": "5.2.0", | ||
"version": "5.3.0", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "@naturalcycles/js-lib": "^6.0.0", |
@@ -9,3 +9,3 @@ /* | ||
import { SchemaLike, ValidationError, ValidationOptions } from '@hapi/joi' | ||
import { AnySchema, ValidationError, ValidationOptions } from '@hapi/joi' | ||
import { isObject } from '@naturalcycles/js-lib' | ||
@@ -41,16 +41,15 @@ import { Joi } from './joi.extensions' | ||
* Returns *converted* value. | ||
* | ||
* If `schema` is undefined - returns value as is. | ||
*/ | ||
export function validate<T> ( | ||
value: T, | ||
schema: SchemaLike, | ||
schema?: AnySchema, | ||
objectName?: string, | ||
options: ValidationOptions = {}, | ||
): T { | ||
const { value: returnValue, error } = Joi.validate(value, schema, { | ||
...defaultOptions, | ||
...options, | ||
}) | ||
const { value: returnValue, error } = getValidationResult(value, schema, objectName, options) | ||
if (error) { | ||
throw createError(value, error, objectName) | ||
throw error | ||
} | ||
@@ -65,9 +64,13 @@ | ||
* Does not throw. | ||
* | ||
* If `schema` is undefined - returns value as is. | ||
*/ | ||
export function getValidationResult<T> ( | ||
value: T, | ||
schema: SchemaLike, | ||
schema?: AnySchema, | ||
objectName?: string, | ||
options: ValidationOptions = {}, | ||
): JoiValidationResult<T> { | ||
if (!schema) return { value } | ||
const { value: returnValue, error } = Joi.validate(value, schema, { | ||
@@ -74,0 +77,0 @@ ...defaultOptions, |
Sorry, the diff of this file is not supported yet
61006
1165