Skip to content

Commit

Permalink
feat: joi typings with IN and OUT types
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed May 18, 2019
1 parent f177f71 commit 8ab6ccf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
24 changes: 14 additions & 10 deletions src/validation/joi/joi.model.ts
Expand Up @@ -12,20 +12,24 @@ import {
StringSchema, StringSchema,
} from '@hapi/joi' } from '@hapi/joi'


export type SchemaTyped<T> = export type SchemaTyped<IN, OUT = IN> =
| AnySchemaT<T> | AnySchemaT<IN, OUT>
| ArraySchemaTyped<T> | ArraySchemaTyped<IN>
| AlternativesSchemaTyped<T> | AlternativesSchemaTyped<IN>
| BinarySchemaTyped | BinarySchemaTyped
| BooleanSchemaTyped | BooleanSchemaTyped
| DateSchemaTyped<T> | DateSchemaTyped<IN>
| FunctionSchemaTyped<T> | FunctionSchemaTyped<IN>
| NumberSchemaTyped | NumberSchemaTyped
| ObjectSchemaTyped<T> | ObjectSchemaTyped<IN, OUT>
| StringSchemaTyped | StringSchemaTyped
| LazySchemaTyped<T> | LazySchemaTyped<IN>


export interface AnySchemaT<T> extends AnySchema {} /**
* IN - value before validation/conversion
* OUT - value after validation/conversion (can be different due to conversion, stripping, etc)
*/
export interface AnySchemaT<IN, OUT = IN> extends AnySchema {}


export interface ArraySchemaTyped<T> extends ArraySchema, AnySchemaT<T[]> {} export interface ArraySchemaTyped<T> extends ArraySchema, AnySchemaT<T[]> {}
export interface AlternativesSchemaTyped<T> extends AlternativesSchema {} export interface AlternativesSchemaTyped<T> extends AlternativesSchema {}
Expand All @@ -34,6 +38,6 @@ export interface BooleanSchemaTyped extends BooleanSchema, AnySchemaT<boolean> {
export interface DateSchemaTyped<T> extends DateSchema {} export interface DateSchemaTyped<T> extends DateSchema {}
export interface FunctionSchemaTyped<T> extends FunctionSchema {} export interface FunctionSchemaTyped<T> extends FunctionSchema {}
export interface NumberSchemaTyped extends NumberSchema, AnySchemaT<number> {} export interface NumberSchemaTyped extends NumberSchema, AnySchemaT<number> {}
export interface ObjectSchemaTyped<T> extends ObjectSchema, AnySchemaT<T> {} export interface ObjectSchemaTyped<IN, OUT> extends ObjectSchema, AnySchemaT<IN, OUT> {}
export interface StringSchemaTyped extends StringSchema, AnySchemaT<string> {} export interface StringSchemaTyped extends StringSchema, AnySchemaT<string> {}
export interface LazySchemaTyped<T> extends LazySchema {} export interface LazySchemaTyped<T> extends LazySchema {}
8 changes: 4 additions & 4 deletions src/validation/joi/joi.shared.schemas.ts
Expand Up @@ -9,13 +9,13 @@ export const integerSchema = Joi.number().integer()
export const dateStringSchema = stringSchema.dateString() export const dateStringSchema = stringSchema.dateString()
export const binarySchema = Joi.binary() export const binarySchema = Joi.binary()


export function arraySchema<T> (items?: AnySchemaT<T>): ArraySchemaTyped<T> { export function arraySchema<T> (items?: AnySchemaT<T, T>): ArraySchemaTyped<T> {
return items ? Joi.array().items(items) : Joi.array() return items ? Joi.array().items(items) : Joi.array()
} }


export function objectSchema<T> ( export function objectSchema<IN, OUT = IN> (
schema?: { [key in keyof T]: AnySchemaT<T[key]> }, schema?: { [key in keyof IN]: AnySchemaT<IN[key]> },
): ObjectSchemaTyped<T> { ): ObjectSchemaTyped<IN, OUT> {
return Joi.object(schema) return Joi.object(schema)
} }


Expand Down
29 changes: 17 additions & 12 deletions src/validation/joi/joi.validation.util.ts
Expand Up @@ -41,13 +41,18 @@ const defaultOptions: ValidationOptions = {
* *
* If `schema` is undefined - returns value as is. * If `schema` is undefined - returns value as is.
*/ */
export function validate<T> ( export function validate<IN, OUT = IN> (
value: T, value: IN,
schema?: AnySchemaT<T>, schema?: AnySchemaT<IN, OUT>,
objectName?: string, objectName?: string,
options: ValidationOptions = {}, options: ValidationOptions = {},
): T { ): OUT {
const { value: returnValue, error } = getValidationResult(value, schema, objectName, options) const { value: returnValue, error } = getValidationResult<IN, OUT>(
value,
schema,
objectName,
options,
)


if (error) { if (error) {
throw error throw error
Expand All @@ -63,21 +68,21 @@ export function validate<T> (
* *
* If `schema` is undefined - returns value as is. * If `schema` is undefined - returns value as is.
*/ */
export function getValidationResult<T> ( export function getValidationResult<IN, OUT = IN> (
value: T, value: IN,
schema?: AnySchemaT<T>, schema?: AnySchemaT<IN, OUT>,
objectName?: string, objectName?: string,
options: ValidationOptions = {}, options: ValidationOptions = {},
): JoiValidationResult<T> { ): JoiValidationResult<OUT> {
if (!schema) return { value } if (!schema) return { value } as any


const { value: returnValue, error } = Joi.validate(value, schema, { const { value: returnValue, error } = Joi.validate(value, schema, {
...defaultOptions, ...defaultOptions,
...options, ...options,
}) })


const vr: JoiValidationResult<T> = { const vr: JoiValidationResult<OUT> = {
value: returnValue, value: (returnValue as any) as OUT,
} }


if (error) { if (error) {
Expand Down

0 comments on commit 8ab6ccf

Please sign in to comment.