Skip to content

Instantly share code, notes, and snippets.

Embed
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)
.join(".");
const actualValue = JSON.stringify(_.get(value, errPath));
const typeOf = (typedValue: any) =>
_.isArray(typedValue) ? "array" : typeof typedValue;
const error = new Error(
`[data].${errPath} expected ${actualErr.type.name}, but got ${typeOf(
actualValue
)} (${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
You can’t perform that action at this time.