UNPKG

smart-types-ts

Version:

A collection of _Smart Types_ and _Smart Constructors_ which enable you to be more strict when defining your application's important types/interfaces.

15 lines (12 loc) 440 B
import * as e from "fp-ts/lib/Either"; import { flow } from "fp-ts/lib/function"; import validator from "validator"; import { SmartConstructor, SmartType } from "../utilTypes"; import { string } from "./string"; export type URL = SmartType<string, "URL">; export const mkURL: SmartConstructor<URL> = flow( string, e.map(s => s.trim()), e.chain(e.fromPredicate(validator.isURL, () => "Not a valid URL")), e.map(id => id as URL) );