Skip to content

Commit

Permalink
refactor: io-ts to use decoder and throw errors
Browse files Browse the repository at this point in the history
  • Loading branch information
moltar committed Aug 26, 2020
1 parent 69f77ec commit 5501aa1
Show file tree
Hide file tree
Showing 2 changed files with 469 additions and 10 deletions.
18 changes: 11 additions & 7 deletions cases/io-ts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as t from 'io-ts';
import { pipe } from 'fp-ts/function';
import { fold } from 'fp-ts/Either';
import { Case } from './abstract';

const dataType = t.type({
Expand All @@ -19,12 +21,14 @@ export class IoTsCase extends Case implements Case {
name = 'io-ts';

validate() {
const { data } = this;

if (dataType.is(data)) {
return data;
}

throw new Error('Invalid');
return pipe(
dataType.decode(this.data),
fold(
errors => {
throw errors;
},
a => a
)
);
}
}

0 comments on commit 5501aa1

Please sign in to comment.