0.1.0 • Published 6 years ago
strictly-formed-io-ts v0.1.0
Strictly Formed Io-TS
io-ts wrapper for strictly-formed, a statically typed form library for typescript. Guarantee runtime type safety for custom static types.
API
Validate form input against io-ts runtime types, wrapping input changes in a fp-ts Either<Error, T>
type.
StringInput
type Props = {
type: t.Type<T>,
value: string | undefined
className?: string
placeholder?: string
forwardedRef?: Ref<HTMLInputElement>
onChange(validation: t.Validation<T>, value: string | undefined): void
onSubmit?(value: T): void
onClear?: () => void
}
import t as * from 'io-ts';
import { StringInput } from 'strictly-formed-io-ts';
// EX - validate trimmed input is not empty
const NonEmptyStringIoTs = t.refinement(t.string, (str) => str.trim() !== '');
type NonEmptyString = t.TypeOf<typeof NonEmptyString>;
<StringInput<NonEmptyString>
className="string-input"
type={NonEmptyStringIoTs}
value={copyDate}
onChange={(validation, value) => {
validation.fold(
(validationErrors) => console.error(validationErrors),
// ^^^^^^^^^^^^^^ - T.ValidationError[]
(validValue) => console.log(validValue)
// ^^^^^^^^ - NonEmptyString
)
}}
onSubmit={submitString}
>
Install
npm install strictly-formed