@prismicio/types-internal
Version:
Prismic types for Custom Types and Prismic Data
23 lines (20 loc) • 515 B
text/typescript
import { chain } from "fp-ts/Either"
import { pipe } from "fp-ts/function"
import { success, Type } from "io-ts"
import StringOrNull from "./StringOrNull"
export default new Type<string | null, string | null, unknown>(
"nonEmptyStringOrNull",
(u): u is string => typeof u === "string" || u === null,
(u, c) =>
pipe(
StringOrNull.validate(u, c),
chain((s) => {
if (typeof s === "string" && s.length > 0) {
return success(s)
} else {
return success(null)
}
}),
),
(s) => s,
)