@prismicio/types-internal
Version:
Prismic types for Custom Types and Prismic Data
42 lines (36 loc) • 949 B
text/typescript
import { either } from "fp-ts/lib/Either"
import * as t from "io-ts"
import { IntFromString } from "io-ts-types/lib/IntFromString"
import { IntFromNumber, IntFromPixels } from "../../../validators"
const SideConstraint = new t.Type<number | null, unknown, unknown>(
"SideConstraints",
(u: unknown): u is number | null => {
return !u || typeof u === "number"
},
(u: unknown, context: t.Context) => {
return either.chain(
t
.union([
t.literal("auto"),
t.literal(""),
t.Int,
IntFromString,
IntFromNumber,
IntFromPixels,
t.null,
])
.validate(u, context),
(constraint) => {
if (constraint === "auto" || constraint === "") return t.success(null)
return t.success(constraint)
},
)
},
(res) => res,
)
const ImageConstraint = t.partial({
width: SideConstraint,
height: SideConstraint,
})
type ImageConstraint = t.TypeOf<typeof ImageConstraint>
export default ImageConstraint