@faast/ts-common
Advanced tools
Comparing version 0.5.2 to 0.5.3
import { UnionType, IntersectionType } from 'io-ts'; | ||
import { stringify } from './string'; | ||
function isCodec(actual, expected) { | ||
return actual instanceof expected || actual._tag === expected.name; | ||
} | ||
function getContextPath(context) { | ||
@@ -9,3 +12,3 @@ return context | ||
const previousType = context[i - 1].type; | ||
return !(previousType instanceof UnionType || previousType instanceof IntersectionType); | ||
return !(isCodec(previousType, UnionType) || isCodec(previousType, IntersectionType)); | ||
}) | ||
@@ -15,4 +18,22 @@ .map(({ key, type }) => (key ? key : type.name)) | ||
} | ||
function stringifyNested(types, delim) { | ||
return types.map((type) => type.name).join(delim); | ||
} | ||
function getContextTypeName(context) { | ||
if (context.length <= 0) { | ||
return ''; | ||
} | ||
if (context.length > 1) { | ||
const parent = context[context.length - 2].type; | ||
if (isCodec(parent, UnionType)) { | ||
return stringifyNested(parent.types, ' | '); | ||
} | ||
else if (isCodec(parent, IntersectionType)) { | ||
return stringifyNested(parent.types, ' & '); | ||
} | ||
} | ||
return context[context.length - 1].type.name; | ||
} | ||
export function getMessage(e) { | ||
const expectedType = e.context.length > 0 ? e.context[e.context.length - 1].type.name : ''; | ||
const expectedType = getContextTypeName(e.context); | ||
const contextPath = getContextPath(e.context); | ||
@@ -19,0 +40,0 @@ const expectedMessage = expectedType !== contextPath ? `${expectedType} for ${contextPath}` : expectedType; |
{ | ||
"name": "@faast/ts-common", | ||
"version": "0.5.2", | ||
"version": "0.5.3", | ||
"description": "Common typescript types and utils used by faast", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -5,2 +5,9 @@ import { Context, ValidationError, UnionType, IntersectionType, Type } from 'io-ts' | ||
function isCodec<T extends { new (...args: any[]): Type<any> }>( | ||
actual: object, | ||
expected: T, | ||
): actual is InstanceType<T> { | ||
return actual instanceof expected || (actual as any)._tag === expected.name | ||
} | ||
function getContextPath(context: Context): string { | ||
@@ -10,4 +17,4 @@ return context | ||
if (i === 0) return true | ||
const previousType = context[i - 1].type | ||
return !(previousType instanceof UnionType || previousType instanceof IntersectionType) | ||
const previousType = context[i - 1].type as any | ||
return !(isCodec(previousType, UnionType) || isCodec(previousType, IntersectionType)) | ||
}) | ||
@@ -18,4 +25,23 @@ .map(({ key, type }) => (key ? key : type.name)) | ||
function stringifyNested(types: any[], delim: string): string { | ||
return types.map((type: Type<any>) => type.name).join(delim) | ||
} | ||
function getContextTypeName(context: Context): string { | ||
if (context.length <= 0) { | ||
return '' | ||
} | ||
if (context.length > 1) { | ||
const parent = context[context.length - 2].type | ||
if (isCodec(parent, UnionType)) { | ||
return stringifyNested(parent.types, ' | ') | ||
} else if (isCodec(parent, IntersectionType)) { | ||
return stringifyNested(parent.types, ' & ') | ||
} | ||
} | ||
return context[context.length - 1].type.name | ||
} | ||
export function getMessage(e: ValidationError): string { | ||
const expectedType = e.context.length > 0 ? e.context[e.context.length - 1].type.name : '' | ||
const expectedType = getContextTypeName(e.context) | ||
const contextPath = getContextPath(e.context) | ||
@@ -22,0 +48,0 @@ const expectedMessage = expectedType !== contextPath ? `${expectedType} for ${contextPath}` : expectedType |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
871702
10480