diff --git a/package.json b/package.json index 910bf10..f8d4ba1 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "url": "https://github.com/MonoidDev/reform" }, "private": false, - "version": "1.0.1", + "version": "1.0.2", "license": "MIT", "publishConfig": { "access": "public" diff --git a/src/fields/stringField.ts b/src/fields/stringField.ts index 914ddfe..00783ce 100644 --- a/src/fields/stringField.ts +++ b/src/fields/stringField.ts @@ -1,9 +1,27 @@ -import { string } from '../types/BasicTypes'; +import { any } from '../types/BasicTypes'; import { Either, makeLeft, makeRight } from '../types/Either'; import { ErrorMessage } from '../types/ErrorMessage'; -export const stringField = () => { - const stringType = string(); +export interface StringFieldOptions { + message: string; +} + +export const stringField = ( + options: StringFieldOptions = { message: 'Invalid value supplied to string' }, +) => { + const { message } = options; + + const stringType = any().refine((input) => { + if (input == null) { + return makeRight(''); + } + + if (typeof input === 'string') { + return makeRight(input); + } + + return makeLeft({ message }); + }); return { ...stringType,