Skip to content

Commit

Permalink
[dsch] examples/either with fp-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
DScheglov committed Mar 18, 2021
1 parent 0fa6503 commit 3310411
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 0 deletions.
22 changes: 22 additions & 0 deletions examples/either/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# ts-cast: Basics (example)

## Install and Start

```sh
git clone https://github.com/DScheglov/ts-cast.git
cd ts-cast/examples/either
npm install
npm start
```

**Output**

```shell
Book {
title: 'My First Book',
annotation: undefined,
year: 2021,
authors: [ Person { name: 'John Smith', email: 'john@smith.com' } ],
coords: [ 1.2, 23.32 ]
}
```
81 changes: 81 additions & 0 deletions examples/either/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"private": true,
"scripts": {
"start": "ts-node ./src"
},
"dependencies": {
"fp-ts": "^3.0.0-alpha.15",
"ts-cast": "latest",
"validator": "^13.5.2"
},
"devDependencies": {
"@types/jest": "^24.9.1",
"@types/node": "^14.0.13",
"@types/validator": "^13.1.2",
"@typescript-eslint/eslint-plugin": "^2.34.0",
"@typescript-eslint/parser": "^2.34.0",
"eslint": "^6.7.2",
"eslint-config-airbnb": "^18.1.0",
"eslint-plugin-import": "^2.21.2",
"eslint-plugin-jest": "^23.13.2",
"ts-node": "^9.1.1",
"typescript": "^3.7.5"
},
"eslintConfig": {
"extends": [
"airbnb/base"
],
"plugins": [
"@typescript-eslint",
"jest"
],
"parser": "@typescript-eslint/parser",
"rules": {
"max-len": [
"error",
{
"code": 100,
"ignoreStrings": true
}
],
"no-param-reassign": "off",
"implicit-arrow-linebreak": "off",
"import/no-unresolved": 0,
"import/prefer-default-export": 0,
"indent": [
2,
2,
{
"flatTernaryExpressions": true
}
],
"no-unused-vars": "off",
"no-undef": "error",
"no-tabs": "error",
"no-nested-ternary": 0,
"import/extensions": 0,
"arrow-parens": [
"error",
"as-needed"
],
"operator-linebreak": 0,
"no-underscore-dangle": 0,
"@typescript-eslint/no-unused-vars": [
"error"
],
"jest/no-disabled-tests": "warn",
"jest/no-focused-tests": "error",
"jest/no-identical-title": "error",
"jest/prefer-to-have-length": "warn",
"jest/valid-expect": "error"
},
"env": {
"jest/globals": true
},
"ignorePatterns": [
"examples/**/*",
"lib/**/*",
"esm/**/*"
]
}
}
37 changes: 37 additions & 0 deletions examples/either/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as E from 'fp-ts/Either';
import { pipe } from 'fp-ts/function';
import { Book, eitherStr } from './schema';

const debugBook = (data: unknown) =>
pipe(
data,
eitherStr(Book),
E.orElseW(error => E.right({ error })),
E.map(result => console.dir(result, { depth: null })),
);

debugBook({
title: "My First Book",
year: 2021,
authors: [{ name: "John Smith", email: "john@smith.com" }],
coords: [1.2, 23.32]
});
/* Prints:
Book {
title: 'My First Book',
annotation: undefined,
year: 2021,
authors: [ Person { name: 'John Smith', email: 'john@smith.com' } ],
coords: [ 1.2, 23.32 ]
}
*/

debugBook(null);
/* Prints:
{ error: 'Book is expected but "null" received.' }
*/

debugBook({});
/* Prints:
{ error: 'string is expected in title but "undefined" received.' }
*/
23 changes: 23 additions & 0 deletions examples/either/src/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as E from 'fp-ts/Either';
import { integer, number, string, struct, tuple, array, toBe, Caster } from 'ts-cast';
import v from 'validator';

export const eitherStr = <T>(caster: Caster<T>) => caster.either(
({ message }) => E.left(message),
E.right
);

export const Person = struct({
name: string,
email: string.restrict(toBe(v.isEmail, "a valid email")),
}, 'Person');

export const Coords = tuple(number, number);

export const Book = struct({
title: string,
annotation: string.optional,
year: integer,
authors: array(Person),
coords: Coords.optional,
}, 'Book');
24 changes: 24 additions & 0 deletions examples/either/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"target": "ESNext",
"outDir": "./esm",
"baseUrl": "./src",
"alwaysStrict": true,
"noImplicitAny": true,
"experimentalDecorators": false,
"emitDecoratorMetadata": false,
"module": "CommonJS",
"moduleResolution": "node",
"allowSyntheticDefaultImports": false,
"esModuleInterop": true,
"resolveJsonModule": true,
"removeComments": true,
"declaration": true,
"importHelpers": false,
"strictNullChecks": true,
"sourceMap": true
},
"types": ["jest", "node"],
"lib": ["es6", "es7"],
"include": ["./src/**/*.ts"]
}

0 comments on commit 3310411

Please sign in to comment.