Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
function decode<T>(value: any, type: t.Type<T>): Promise<T> {
const validation = t.validate(value, type);
return validation.fold(
([e, ..._unused]) => {
const actualErr = _.last(e.context);
const errPath = e.context
.map(c => c.key)
.filter(key => key.length)
const actualValue = JSON.stringify(_.get(value, errPath));
const typeOf = (typedValue: any) =>
_.isArray(typedValue) ? "array" : typeof typedValue;
const error = new Error(
`[data].${errPath} expected ${}, but got ${typeOf(
)} (${actualValue})`
return Promise.reject(error);
validatedValue => Promise.resolve(validatedValue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment