@prismicio/types-internal
Advanced tools
Comparing version 2.2.0-alpha.13 to 2.2.0-alpha.14
@@ -7,3 +7,3 @@ export declare type Asset = { | ||
extension?: string; | ||
size?: string; | ||
size?: number; | ||
origin_url: string; | ||
@@ -10,0 +10,0 @@ url: string; |
@@ -6,4 +6,4 @@ "use strict"; | ||
const t = (0, tslib_1.__importStar)(require("io-ts")); | ||
const io_ts_types_1 = require("io-ts-types"); | ||
const uuid_1 = require("uuid"); | ||
exports.UUID = (0, io_ts_types_1.withMessage)(t.brand(t.string, (s) => (0, uuid_1.validate)(s), "UUID"), () => "The value must be a valid UUID"); | ||
const function_1 = require("../validators/function"); | ||
exports.UUID = (0, function_1.withCustomError)(t.brand(t.string, (s) => (0, uuid_1.validate)(s), "UUID"), () => "The value must be a valid UUID"); |
@@ -33,3 +33,3 @@ "use strict"; | ||
["alt", (imageField === null || imageField === void 0 ? void 0 : imageField.alt) || image.alt], | ||
["credits", (imageField === null || imageField === void 0 ? void 0 : imageField.credit) || image.credits], | ||
["credits", (imageField === null || imageField === void 0 ? void 0 : imageField.copyright) || image.credits], | ||
]); | ||
@@ -36,0 +36,0 @@ } |
@@ -21,3 +21,3 @@ "use strict"; | ||
const fileLinkConverter = (field, asset) => { | ||
var _a, _b; | ||
var _a; | ||
return ({ | ||
@@ -29,3 +29,3 @@ value: { | ||
kind: asset.kind, | ||
size: (_b = asset.size) !== null && _b !== void 0 ? _b : "0", | ||
size: asset.size ? String(asset.size) : "0", | ||
__TYPE__: "FileLink", | ||
@@ -37,3 +37,3 @@ }, | ||
const imageLinkConverter = (field, asset) => { | ||
var _a, _b, _c, _d; | ||
var _a, _b, _c; | ||
return ({ | ||
@@ -47,3 +47,3 @@ value: { | ||
kind: asset.kind, | ||
size: (_d = asset.size) !== null && _d !== void 0 ? _d : "0", | ||
size: asset.size ? String(asset.size) : "0", | ||
__TYPE__: "ImageLink", | ||
@@ -50,0 +50,0 @@ }, |
@@ -9,7 +9,7 @@ "use strict"; | ||
const t = (0, tslib_1.__importStar)(require("io-ts")); | ||
const io_ts_types_1 = require("io-ts-types"); | ||
const customtypes_1 = require("../../customtypes"); | ||
const Objects_1 = require("../../utils/Objects"); | ||
const function_2 = require("../../validators/function"); | ||
const ImportField_1 = require("./fields/ImportField"); | ||
const rawImportDocument = (0, io_ts_types_1.withMessage)(t.record(t.string, t.unknown), () => "document is not an object"); | ||
const rawImportDocument = (0, function_2.withCustomError)(t.UnknownRecord, () => "document is not an object"); | ||
function validateField(document, customType) { | ||
@@ -16,0 +16,0 @@ return (key, content, model) => { |
@@ -5,4 +5,4 @@ "use strict"; | ||
exports.SharedSliceErrors = { | ||
SliceTypeNotFound: (slice_type) => `Shared slice '${slice_type}' not found in document's custom type`, | ||
SliceTypeNotFound: (slice_type) => `Shared slice '${slice_type}' not found in this slice zone in the document's custom type`, | ||
VariationNotFound: (variation, slice_type) => `Variation '${variation}' not found for Slice '${slice_type}'`, | ||
}; |
@@ -10,3 +10,3 @@ "use strict"; | ||
const io_ts_1 = require("../../../../../../../utils/io-ts"); | ||
const validators_1 = require("../../../../../../../validators"); | ||
const function_2 = require("../../../../../../../validators/function"); | ||
const SharedSliceContentEntry_1 = require("./SharedSliceContentEntry"); | ||
@@ -23,3 +23,3 @@ /** | ||
// We first decode the raw content object in the shape of Record<WidgetKey, unknown> | ||
validators_1.AnyObject.validate(u, c), E.chain((rawContent) => (0, function_1.pipe)( | ||
(0, function_2.withCustomError)(t.UnknownRecord, () => "The value must be an object").validate(u, c), E.chain((rawContent) => (0, function_1.pipe)( | ||
// We split the object into entries because there is no easy other way to decode each value with a different codec | ||
@@ -26,0 +26,0 @@ Object.entries(rawContent).map((entry) => (0, SharedSliceContentEntry_1.SharedSliceContentEntry)(sliceName, sliceContentField, sliceFieldModels).validate(entry, c)), |
@@ -9,2 +9,3 @@ "use strict"; | ||
const validators_1 = require("../../../../../validators"); | ||
const function_2 = require("../../../../../validators/function"); | ||
const fields_1 = require("./fields"); | ||
@@ -48,3 +49,3 @@ const SharedSliceShape = t.type({ | ||
// We need to use this wrapper codec instead of using t.array(SharedSliceContent) directly in order to get proper custom error messages | ||
items: validators_1.AnyArray.pipe(t.array((0, fields_1.SharedSliceContent)(decoded.slice_type.slice_type, "items", (_b = decoded.variation.data.items) !== null && _b !== void 0 ? _b : {}))), | ||
items: (0, function_2.withCustomError)(t.array((0, fields_1.SharedSliceContent)(decoded.slice_type.slice_type, "items", (_b = decoded.variation.data.items) !== null && _b !== void 0 ? _b : {})), () => "The value must be an array"), | ||
}) | ||
@@ -51,0 +52,0 @@ .validate(u, c), E.map(({ primary, items }) => ({ ...decoded, primary, items }))); |
@@ -9,3 +9,3 @@ "use strict"; | ||
const ImportContent_1 = require("../ImportContent"); | ||
const GeooPoint = (0, function_1.withFallbackMessage)(t.strict({ | ||
const GeooPoint = (0, function_1.withCustomError)(t.strict({ | ||
latitude: (0, validators_1.NumberRange)(-90, 90, "latitude"), | ||
@@ -12,0 +12,0 @@ longitude: (0, validators_1.NumberRange)(-180, 180, "longitude"), |
@@ -13,3 +13,3 @@ "use strict"; | ||
const alt = image === null || image === void 0 ? void 0 : image.alt; | ||
const credit = image === null || image === void 0 ? void 0 : image.credit; | ||
const copyright = image === null || image === void 0 ? void 0 : image.copyright; | ||
return { | ||
@@ -28,3 +28,3 @@ id: (_k = image === null || image === void 0 ? void 0 : image.id) !== null && _k !== void 0 ? _k : mainImage === null || mainImage === void 0 ? void 0 : mainImage.id, | ||
...(alt !== undefined ? { alt } : {}), | ||
...(credit !== undefined ? { credit } : {}), | ||
...(copyright !== undefined ? { copyright } : {}), | ||
}; | ||
@@ -31,0 +31,0 @@ }; |
@@ -8,4 +8,4 @@ "use strict"; | ||
const t = (0, tslib_1.__importStar)(require("io-ts")); | ||
const io_ts_types_1 = require("io-ts-types"); | ||
const validators_1 = require("../../../../../validators"); | ||
const function_2 = require("../../../../../validators/function"); | ||
const ImportContent_1 = require("../../ImportContent"); | ||
@@ -15,9 +15,11 @@ const Decoder_1 = require("./Decoder"); | ||
const defaultImage = (field) => { | ||
var _a, _b; | ||
const thumbnailsNames = ((_b = (_a = field === null || field === void 0 ? void 0 : field.config) === null || _a === void 0 ? void 0 : _a.thumbnails) === null || _b === void 0 ? void 0 : _b.map((t) => t.name)) || []; | ||
const thumbnailsCodec = (0, io_ts_types_1.withMessage)(t.type(thumbnailsNames.reduce((acc, name) => ({ | ||
var _a; | ||
// If there is not thumbnails we expect an empty object | ||
if (!((_a = field === null || field === void 0 ? void 0 : field.config) === null || _a === void 0 ? void 0 : _a.thumbnails) || !field.config.thumbnails.length) | ||
return validators_1.EmptyObject; | ||
// If there are thumbnails, we expect an object with thumbnails as empty objects | ||
return (0, function_2.withCustomError)(t.strict(field.config.thumbnails.reduce((acc, thumbnail) => ({ | ||
...acc, | ||
[name]: validators_1.EmptyObject, | ||
}), {})), () => "The value must be an object with thumbnails as empty objects"); | ||
return validators_1.AnyObject.pipe(thumbnailsCodec); | ||
[thumbnail.name]: validators_1.EmptyObject, | ||
}), {})), () => "The value must be an object"); | ||
}; | ||
@@ -28,3 +30,3 @@ const ImageField = (field) => new t.Type("ImageField", (u) => Validator_1.ImageFieldValidator.is(u) && | ||
return (0, function_1.pipe)(Validator_1.ImageFieldValidator.validate(u, ctx), (0, Either_1.chain)((mainImage) => { | ||
const { id, dimensions, edit, credit, alt, ...maybeThumbnails } = mainImage; | ||
const { id, dimensions, edit, copyright, alt, ...maybeThumbnails } = mainImage; | ||
return (0, function_1.pipe)(Validator_1.ThumbnailsValidator.validate(maybeThumbnails, ctx), (0, Either_1.map)((thumbnails) => { | ||
@@ -39,5 +41,5 @@ var _a; | ||
}, t.identity); | ||
const ImageCodec = (field) => (0, validators_1.DefaultOrElse)(defaultImage(field))(ImageField(field)); | ||
const ImageCodec = (field) => (0, function_2.withCustomError)((0, validators_1.DefaultOrElse)(defaultImage(field))(ImageField(field)), () => "An image field must be an object"); | ||
const ImportImage = (field) => (0, ImportContent_1.ImportContent)("Image", ImageCodec(field)); | ||
exports.ImportImage = ImportImage; | ||
(0, tslib_1.__exportStar)(require("./model"), exports); |
@@ -13,3 +13,3 @@ export declare type ImageField = { | ||
}; | ||
credit?: string | null; | ||
copyright?: string | null; | ||
alt?: string | null; | ||
@@ -16,0 +16,0 @@ }; |
import * as t from "io-ts"; | ||
export declare const ImageFieldValidator: t.Type<{ | ||
id: string; | ||
} & { | ||
dimensions?: { | ||
width?: number; | ||
height?: number; | ||
}; | ||
edit?: { | ||
x?: number; | ||
y?: number; | ||
zoom?: number; | ||
background?: string; | ||
}; | ||
credit?: string | null; | ||
alt?: string | null; | ||
}, { | ||
[x: string]: unknown; | ||
}, unknown>; | ||
export declare const ThumbnailsValidator: t.RecordC<t.StringC, t.Type<{ | ||
id: string; | ||
} & { | ||
dimensions?: { | ||
width?: number; | ||
height?: number; | ||
}; | ||
edit?: { | ||
x?: number; | ||
y?: number; | ||
zoom?: number; | ||
background?: string; | ||
}; | ||
credit?: string | null; | ||
alt?: string | null; | ||
}, { | ||
[x: string]: unknown; | ||
}, unknown>>; | ||
export declare const ImageFieldValidator: t.IntersectionC<[t.TypeC<{ | ||
id: t.StringC; | ||
}>, t.PartialC<{ | ||
dimensions: t.PartialC<{ | ||
width: t.NumberC; | ||
height: t.NumberC; | ||
}>; | ||
edit: t.PartialC<{ | ||
x: t.NumberC; | ||
y: t.NumberC; | ||
zoom: t.NumberC; | ||
background: t.StringC; | ||
}>; | ||
copyright: t.UnionC<[t.StringC, t.NullC]>; | ||
alt: t.UnionC<[t.StringC, t.NullC]>; | ||
}>]>; | ||
export declare const ThumbnailsValidator: t.RecordC<t.StringC, t.IntersectionC<[t.TypeC<{ | ||
id: t.StringC; | ||
}>, t.PartialC<{ | ||
dimensions: t.PartialC<{ | ||
width: t.NumberC; | ||
height: t.NumberC; | ||
}>; | ||
edit: t.PartialC<{ | ||
x: t.NumberC; | ||
y: t.NumberC; | ||
zoom: t.NumberC; | ||
background: t.StringC; | ||
}>; | ||
copyright: t.UnionC<[t.StringC, t.NullC]>; | ||
alt: t.UnionC<[t.StringC, t.NullC]>; | ||
}>]>>; |
@@ -7,3 +7,4 @@ "use strict"; | ||
const BasicTypes_1 = require("../../../../../validators/BasicTypes"); | ||
exports.ImageFieldValidator = BasicTypes_1.AnyObject.pipe(t.intersection([ | ||
const function_1 = require("../../../../../validators/function"); | ||
exports.ImageFieldValidator = (0, function_1.withCustomError)(t.intersection([ | ||
t.type({ | ||
@@ -13,7 +14,7 @@ id: BasicTypes_1.String, | ||
t.partial({ | ||
dimensions: BasicTypes_1.AnyObject.pipe(t.partial({ | ||
dimensions: (0, function_1.withCustomError)(t.partial({ | ||
width: BasicTypes_1.Number, | ||
height: BasicTypes_1.Number, | ||
})), | ||
edit: BasicTypes_1.AnyObject.pipe(t.partial({ | ||
}), () => "The value must be an object"), | ||
edit: (0, function_1.withCustomError)(t.partial({ | ||
x: BasicTypes_1.Number, | ||
@@ -23,7 +24,7 @@ y: BasicTypes_1.Number, | ||
background: BasicTypes_1.String, | ||
})), | ||
credit: BasicTypes_1.StringOrNull, | ||
}), () => "The value must be an object"), | ||
copyright: BasicTypes_1.StringOrNull, | ||
alt: BasicTypes_1.StringOrNull, | ||
}), | ||
])); | ||
]), () => "The value must be an object"); | ||
exports.ThumbnailsValidator = t.record(t.string, exports.ImageFieldValidator); |
@@ -6,8 +6,8 @@ "use strict"; | ||
const t = (0, tslib_1.__importStar)(require("io-ts")); | ||
const io_ts_types_1 = require("io-ts-types"); | ||
const DocumentId_1 = require("../../../../utils/DocumentId"); | ||
const validators_1 = require("../../../../validators"); | ||
const function_1 = require("../../../../validators/function"); | ||
const ImportContent_1 = require("../ImportContent"); | ||
const LinkTypeValidator = t.type({ | ||
link_type: (0, io_ts_types_1.withMessage)(t.union([t.literal("Web"), t.literal("Document"), t.literal("Media")]), () => "The value must be `Web`, `Document` or `Media`"), | ||
link_type: (0, function_1.withCustomError)(t.union([t.literal("Web"), t.literal("Document"), t.literal("Media")]), () => "The value must be `Web`, `Document` or `Media`"), | ||
}); | ||
@@ -14,0 +14,0 @@ const WebLink = t.intersection([ |
@@ -1,1 +0,2 @@ | ||
export declare const DocumentId: import("io-ts").Type<string, string, unknown>; | ||
import * as t from "io-ts"; | ||
export declare const DocumentId: t.Type<string, string, unknown>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.DocumentId = void 0; | ||
const io_ts_types_1 = require("io-ts-types"); | ||
const validators_1 = require("../validators"); | ||
const tslib_1 = require("tslib"); | ||
const t = (0, tslib_1.__importStar)(require("io-ts")); | ||
const function_1 = require("../validators/function"); | ||
exports.DocumentId = (0, io_ts_types_1.withMessage)((0, function_1.refineType)(validators_1.String, "DocumentId", (s) => s.length === 16), () => "DocumentId must be a 16 character string"); | ||
exports.DocumentId = (0, function_1.withCustomError)((0, function_1.refineType)(t.string, "DocumentId", (s) => s.length === 16), () => "DocumentId must be a 16 character string"); |
@@ -11,3 +11,1 @@ import * as t from "io-ts"; | ||
export declare const EmptyArray: t.Type<never[], never[], unknown>; | ||
export declare const AnyObject: t.RecordC<t.StringC, t.UnknownC>; | ||
export declare const AnyArray: t.ArrayC<t.UnknownC>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AnyArray = exports.AnyObject = exports.EmptyArray = exports.EmptyObject = exports.Boolean = exports.NumberOrNull = exports.Number = exports.StringOrNull = exports.String = void 0; | ||
exports.EmptyArray = exports.EmptyObject = exports.Boolean = exports.NumberOrNull = exports.Number = exports.StringOrNull = exports.String = void 0; | ||
const tslib_1 = require("tslib"); | ||
@@ -8,8 +8,8 @@ const fp_ts_1 = require("fp-ts"); | ||
const t = (0, tslib_1.__importStar)(require("io-ts")); | ||
const io_ts_types_1 = require("io-ts-types"); | ||
exports.String = (0, io_ts_types_1.withMessage)(t.string, () => "The value must be a string"); | ||
exports.StringOrNull = (0, io_ts_types_1.withMessage)(t.union([t.string, t.null]), () => "The value must be a string or null"); | ||
exports.Number = (0, io_ts_types_1.withMessage)(t.number, () => "The value must be a number"); | ||
exports.NumberOrNull = (0, io_ts_types_1.withMessage)(t.union([t.number, t.null]), () => "The value must be a number or null"); | ||
exports.Boolean = (0, io_ts_types_1.withMessage)(t.boolean, () => "The value must be a boolean"); | ||
const function_2 = require("./function"); | ||
exports.String = (0, function_2.withCustomError)(t.string, () => "The value must be a string"); | ||
exports.StringOrNull = (0, function_2.withCustomError)(t.union([t.string, t.null]), () => "The value must be a string or null"); | ||
exports.Number = (0, function_2.withCustomError)(t.number, () => "The value must be a number"); | ||
exports.NumberOrNull = (0, function_2.withCustomError)(t.union([t.number, t.null]), () => "The value must be a number or null"); | ||
exports.Boolean = (0, function_2.withCustomError)(t.boolean, () => "The value must be a boolean"); | ||
exports.EmptyObject = t.UnknownRecord.pipe(new t.Type("emptyObject", (u) => t.UnknownRecord.is(u), (u, c) => { | ||
@@ -27,3 +27,1 @@ if (Object.keys(u).length > 0) | ||
}, () => []); | ||
exports.AnyObject = (0, io_ts_types_1.withMessage)(t.record(t.string, t.unknown), () => "The value must be an object"); | ||
exports.AnyArray = (0, io_ts_types_1.withMessage)(t.array(t.unknown), () => "The value must be an array"); |
@@ -9,3 +9,3 @@ "use strict"; | ||
const DefaultOrElse = (inputValidator) => { | ||
return (codec) => new t.Type("DefaultOrT", (u) => null === u || codec.is(u), (u) => { | ||
return (codec) => new t.Type("DefaultOrElse", (u) => null === u || codec.is(u), (u) => { | ||
if ((0, Either_1.isRight)(inputValidator.decode(u))) | ||
@@ -12,0 +12,0 @@ return t.success(null); |
@@ -22,4 +22,4 @@ import * as t from "io-ts"; | ||
* expect( | ||
* withFallbackMessage( | ||
* t.type({age: withMessage(t.number, () => 'Invalid child')}), | ||
* withCustomError( | ||
* t.type({age: withCustomError(t.number, () => 'Invalid child')}), | ||
* () => "Invalid parent" | ||
@@ -29,3 +29,3 @@ * ) | ||
* expect( | ||
* withFallbackMessage( | ||
* withCustomError( | ||
* t.type({age: t.number}), | ||
@@ -36,2 +36,2 @@ * () => "Invalid parent" | ||
*/ | ||
export declare function withFallbackMessage<C extends t.Any>(codec: C, message: (i: t.InputOf<C>, c: t.Context) => string): C; | ||
export declare function withCustomError<C extends t.Any>(codec: C, message: (i: t.InputOf<C>, c: t.Context) => string): C; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.withFallbackMessage = exports.addType = exports.isEmpty = exports.objectToMap = exports.grouped = exports.formatDate = exports.formatDateTime = exports.filterDouble = exports.refineType = exports.nullable = void 0; | ||
exports.withCustomError = exports.addType = exports.isEmpty = exports.objectToMap = exports.grouped = exports.formatDate = exports.formatDateTime = exports.filterDouble = exports.refineType = exports.nullable = void 0; | ||
const tslib_1 = require("tslib"); | ||
@@ -69,4 +69,4 @@ const fp_ts_1 = require("fp-ts"); | ||
* expect( | ||
* withFallbackMessage( | ||
* t.type({age: withMessage(t.number, () => 'Invalid child')}), | ||
* withCustomError( | ||
* t.type({age: withCustomError(t.number, () => 'Invalid child')}), | ||
* () => "Invalid parent" | ||
@@ -76,3 +76,3 @@ * ) | ||
* expect( | ||
* withFallbackMessage( | ||
* withCustomError( | ||
* t.type({age: t.number}), | ||
@@ -83,3 +83,3 @@ * () => "Invalid parent" | ||
*/ | ||
function withFallbackMessage(codec, message) { | ||
function withCustomError(codec, message) { | ||
return (0, io_ts_types_1.withValidate)(codec, (i, c) => { | ||
@@ -103,2 +103,2 @@ return fp_ts_1.either.mapLeft((errors) => { | ||
} | ||
exports.withFallbackMessage = withFallbackMessage; | ||
exports.withCustomError = withCustomError; |
@@ -5,4 +5,3 @@ "use strict"; | ||
const t = (0, tslib_1.__importStar)(require("io-ts")); | ||
const io_ts_types_1 = require("io-ts-types"); | ||
const function_1 = require("./function"); | ||
exports.default = (0, io_ts_types_1.withMessage)((0, function_1.refineType)(t.string, "nonEmptyString", (s) => s.trim().length > 0), () => "The value must be a non-empty string"); | ||
exports.default = (0, function_1.withCustomError)((0, function_1.refineType)(t.string, "nonEmptyString", (s) => s.trim().length > 0), () => "The value must be a non-empty string"); |
{ | ||
"name": "@prismicio/types-internal", | ||
"version": "2.2.0-alpha.13", | ||
"version": "2.2.0-alpha.14", | ||
"description": "Prismic types for Custom Types and Prismic Data", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -7,3 +7,3 @@ export type Asset = { | ||
extension?: string | ||
size?: string | ||
size?: number | ||
origin_url: string | ||
@@ -10,0 +10,0 @@ url: string |
import * as t from "io-ts" | ||
import { withMessage } from "io-ts-types" | ||
import { validate as validateUUID } from "uuid" | ||
import { withCustomError } from "../validators/function" | ||
interface UUIDBrand { | ||
@@ -9,3 +10,3 @@ readonly UUID: unique symbol | ||
export const UUID = withMessage( | ||
export const UUID = withCustomError( | ||
t.brand( | ||
@@ -12,0 +13,0 @@ t.string, |
@@ -37,3 +37,3 @@ import type { Asset } from "../../../../common" | ||
["alt", imageField?.alt || image.alt], | ||
["credits", imageField?.credit || image.credits], | ||
["credits", imageField?.copyright || image.credits], | ||
], | ||
@@ -40,0 +40,0 @@ ) |
@@ -34,3 +34,3 @@ import type { Asset } from "../../../../common" | ||
kind: asset.kind, | ||
size: asset.size ?? "0", | ||
size: asset.size ? String(asset.size) : "0", | ||
__TYPE__: "FileLink", | ||
@@ -49,3 +49,3 @@ }, | ||
kind: asset.kind, | ||
size: asset.size ?? "0", | ||
size: asset.size ? String(asset.size) : "0", | ||
__TYPE__: "ImageLink", | ||
@@ -52,0 +52,0 @@ }, |
@@ -5,3 +5,2 @@ import { either } from "fp-ts" | ||
import * as t from "io-ts" | ||
import { withMessage } from "io-ts-types" | ||
@@ -12,6 +11,7 @@ import type { WidgetKey } from "../../common" | ||
import { isObject } from "../../utils/Objects" | ||
import { withCustomError } from "../../validators/function" | ||
import { ImportField } from "./fields/ImportField" | ||
const rawImportDocument = withMessage( | ||
t.record(t.string, t.unknown), | ||
const rawImportDocument = withCustomError( | ||
t.UnknownRecord, | ||
() => "document is not an object", | ||
@@ -18,0 +18,0 @@ ) |
export const SharedSliceErrors = { | ||
SliceTypeNotFound: (slice_type: string) => | ||
`Shared slice '${slice_type}' not found in document's custom type`, | ||
`Shared slice '${slice_type}' not found in this slice zone in the document's custom type`, | ||
VariationNotFound: (variation: string, slice_type: string) => | ||
`Variation '${variation}' not found for Slice '${slice_type}'`, | ||
} as const |
@@ -8,3 +8,3 @@ import * as E from "fp-ts/Either" | ||
import { combineValidationResults } from "../../../../../../../utils/io-ts" | ||
import { AnyObject } from "../../../../../../../validators" | ||
import { withCustomError } from "../../../../../../../validators/function" | ||
import type { ImportNestable } from "../../../../nestable" | ||
@@ -37,3 +37,6 @@ import { SharedSliceContentEntry } from "./SharedSliceContentEntry" | ||
// We first decode the raw content object in the shape of Record<WidgetKey, unknown> | ||
AnyObject.validate(u, c), | ||
withCustomError( | ||
t.UnknownRecord, | ||
() => "The value must be an object", | ||
).validate(u, c), | ||
E.chain((rawContent) => | ||
@@ -40,0 +43,0 @@ pipe( |
@@ -6,3 +6,4 @@ import * as E from "fp-ts/Either" | ||
import type { SharedSlice as SharedSliceCustomType } from "../../../../../customtypes" | ||
import { AnyArray, NonEmptyString, String } from "../../../../../validators" | ||
import { NonEmptyString, String } from "../../../../../validators" | ||
import { withCustomError } from "../../../../../validators/function" | ||
import { | ||
@@ -79,3 +80,3 @@ OptionalSharedSliceId, | ||
// We need to use this wrapper codec instead of using t.array(SharedSliceContent) directly in order to get proper custom error messages | ||
items: AnyArray.pipe( | ||
items: withCustomError( | ||
t.array( | ||
@@ -88,2 +89,3 @@ SharedSliceContent( | ||
), | ||
() => "The value must be an array", | ||
), | ||
@@ -90,0 +92,0 @@ }) |
@@ -5,6 +5,6 @@ import type { TypeOf } from "io-ts" | ||
import { EmptyObjectOrElse, NumberRange } from "../../../../validators" | ||
import { withFallbackMessage } from "../../../../validators/function" | ||
import { withCustomError } from "../../../../validators/function" | ||
import { ImportContent } from "../ImportContent" | ||
const GeooPoint = withFallbackMessage( | ||
const GeooPoint = withCustomError( | ||
t.strict({ | ||
@@ -11,0 +11,0 @@ latitude: NumberRange(-90, 90, "latitude"), |
@@ -26,3 +26,3 @@ import type { TypeOf } from "io-ts" | ||
const alt = image?.alt | ||
const credit = image?.credit | ||
const copyright = image?.copyright | ||
@@ -42,3 +42,3 @@ return { | ||
...(alt !== undefined ? { alt } : {}), | ||
...(credit !== undefined ? { credit } : {}), | ||
...(copyright !== undefined ? { copyright } : {}), | ||
} | ||
@@ -45,0 +45,0 @@ } |
@@ -5,10 +5,6 @@ import { chain, map } from "fp-ts/Either" | ||
import * as t from "io-ts" | ||
import { withMessage } from "io-ts-types" | ||
import type { Image as ImageDefinition } from "../../../../../customtypes" | ||
import { | ||
AnyObject, | ||
DefaultOrElse, | ||
EmptyObject, | ||
} from "../../../../../validators" | ||
import { DefaultOrElse, EmptyObject } from "../../../../../validators" | ||
import { withCustomError } from "../../../../../validators/function" | ||
import { ImportContent } from "../../ImportContent" | ||
@@ -20,9 +16,13 @@ import { decodeImageField, decodeThumbnails } from "./Decoder" | ||
const defaultImage = (field?: ImageDefinition) => { | ||
const thumbnailsNames = field?.config?.thumbnails?.map((t) => t.name) || [] | ||
const thumbnailsCodec = withMessage( | ||
t.type( | ||
thumbnailsNames.reduce( | ||
(acc, name) => ({ | ||
// If there is not thumbnails we expect an empty object | ||
if (!field?.config?.thumbnails || !field.config.thumbnails.length) | ||
return EmptyObject | ||
// If there are thumbnails, we expect an object with thumbnails as empty objects | ||
return withCustomError( | ||
t.strict( | ||
field.config.thumbnails.reduce( | ||
(acc, thumbnail) => ({ | ||
...acc, | ||
[name]: EmptyObject, | ||
[thumbnail.name]: EmptyObject, | ||
}), | ||
@@ -32,6 +32,4 @@ {}, | ||
), | ||
() => "The value must be an object with thumbnails as empty objects", | ||
() => "The value must be an object", | ||
) | ||
return AnyObject.pipe(thumbnailsCodec) | ||
} | ||
@@ -50,3 +48,3 @@ | ||
chain((mainImage) => { | ||
const { id, dimensions, edit, credit, alt, ...maybeThumbnails } = | ||
const { id, dimensions, edit, copyright, alt, ...maybeThumbnails } = | ||
mainImage | ||
@@ -71,7 +69,8 @@ return pipe( | ||
const ImageCodec = (field?: ImageDefinition) => | ||
DefaultOrElse< | ||
Record<string, unknown>, | ||
ImageFieldWithThumbnails, | ||
ImageFieldWithThumbnails | ||
>(defaultImage(field))(ImageField(field)) | ||
withCustomError( | ||
DefaultOrElse<Record<never, never>, ImageFieldWithThumbnails>( | ||
defaultImage(field), | ||
)(ImageField(field)), | ||
() => "An image field must be an object", | ||
) | ||
@@ -78,0 +77,0 @@ export const ImportImage = (field?: ImageDefinition) => |
@@ -13,3 +13,3 @@ export type ImageField = { | ||
} | ||
credit?: string | null | ||
copyright?: string | null | ||
alt?: string | null | ||
@@ -16,0 +16,0 @@ } |
import * as t from "io-ts" | ||
import { | ||
AnyObject, | ||
Number, | ||
@@ -9,4 +8,5 @@ String, | ||
} from "../../../../../validators/BasicTypes" | ||
import { withCustomError } from "../../../../../validators/function" | ||
export const ImageFieldValidator = AnyObject.pipe( | ||
export const ImageFieldValidator = withCustomError( | ||
t.intersection([ | ||
@@ -17,3 +17,3 @@ t.type({ | ||
t.partial({ | ||
dimensions: AnyObject.pipe( | ||
dimensions: withCustomError( | ||
t.partial({ | ||
@@ -23,4 +23,5 @@ width: Number, | ||
}), | ||
() => "The value must be an object", | ||
), | ||
edit: AnyObject.pipe( | ||
edit: withCustomError( | ||
t.partial({ | ||
@@ -32,8 +33,10 @@ x: Number, | ||
}), | ||
() => "The value must be an object", | ||
), | ||
credit: StringOrNull, | ||
copyright: StringOrNull, | ||
alt: StringOrNull, | ||
}), | ||
]), | ||
() => "The value must be an object", | ||
) | ||
export const ThumbnailsValidator = t.record(t.string, ImageFieldValidator) |
import type { OutputOf, TypeOf } from "io-ts" | ||
import * as t from "io-ts" | ||
import { withMessage } from "io-ts-types" | ||
import { DocumentId } from "../../../../utils/DocumentId" | ||
import { DefaultOrElse, String } from "../../../../validators" | ||
import { withCustomError } from "../../../../validators/function" | ||
import { ImportContent } from "../ImportContent" | ||
const LinkTypeValidator = t.type({ | ||
link_type: withMessage( | ||
link_type: withCustomError( | ||
t.union([t.literal("Web"), t.literal("Document"), t.literal("Media")]), | ||
@@ -12,0 +12,0 @@ () => "The value must be `Web`, `Document` or `Media`", |
@@ -1,9 +0,8 @@ | ||
import { withMessage } from "io-ts-types" | ||
import * as t from "io-ts" | ||
import { String } from "../validators" | ||
import { refineType } from "../validators/function" | ||
import { refineType, withCustomError } from "../validators/function" | ||
export const DocumentId = withMessage( | ||
refineType(String, "DocumentId", (s) => s.length === 16), | ||
export const DocumentId = withCustomError( | ||
refineType(t.string, "DocumentId", (s) => s.length === 16), | ||
() => "DocumentId must be a 16 character string", | ||
) |
import { either } from "fp-ts" | ||
import { pipe } from "fp-ts/lib/function" | ||
import * as t from "io-ts" | ||
import { withMessage } from "io-ts-types" | ||
export const String = withMessage(t.string, () => "The value must be a string") | ||
export const StringOrNull = withMessage( | ||
import { withCustomError } from "./function" | ||
export const String = withCustomError( | ||
t.string, | ||
() => "The value must be a string", | ||
) | ||
export const StringOrNull = withCustomError( | ||
t.union([t.string, t.null]), | ||
() => "The value must be a string or null", | ||
) | ||
export const Number = withMessage(t.number, () => "The value must be a number") | ||
export const NumberOrNull = withMessage( | ||
export const Number = withCustomError( | ||
t.number, | ||
() => "The value must be a number", | ||
) | ||
export const NumberOrNull = withCustomError( | ||
t.union([t.number, t.null]), | ||
() => "The value must be a number or null", | ||
) | ||
export const Boolean = withMessage( | ||
export const Boolean = withCustomError( | ||
t.boolean, | ||
@@ -49,11 +56,1 @@ () => "The value must be a boolean", | ||
) | ||
export const AnyObject = withMessage( | ||
t.record(t.string, t.unknown), | ||
() => "The value must be an object", | ||
) | ||
export const AnyArray = withMessage( | ||
t.array(t.unknown), | ||
() => "The value must be an array", | ||
) |
@@ -9,3 +9,3 @@ import { isRight } from "fp-ts/lib/Either" | ||
new t.Type<A | null, O | undefined>( | ||
"DefaultOrT", | ||
"DefaultOrElse", | ||
(u: unknown): u is A | null => null === u || codec.is(u), | ||
@@ -12,0 +12,0 @@ (u: unknown) => { |
@@ -81,4 +81,4 @@ import { either } from "fp-ts" | ||
* expect( | ||
* withFallbackMessage( | ||
* t.type({age: withMessage(t.number, () => 'Invalid child')}), | ||
* withCustomError( | ||
* t.type({age: withCustomError(t.number, () => 'Invalid child')}), | ||
* () => "Invalid parent" | ||
@@ -88,3 +88,3 @@ * ) | ||
* expect( | ||
* withFallbackMessage( | ||
* withCustomError( | ||
* t.type({age: t.number}), | ||
@@ -95,3 +95,3 @@ * () => "Invalid parent" | ||
*/ | ||
export function withFallbackMessage<C extends t.Any>( | ||
export function withCustomError<C extends t.Any>( | ||
codec: C, | ||
@@ -98,0 +98,0 @@ message: (i: t.InputOf<C>, c: t.Context) => string, |
import * as t from "io-ts" | ||
import { withMessage } from "io-ts-types" | ||
import { refineType } from "./function" | ||
import { refineType, withCustomError } from "./function" | ||
export default withMessage( | ||
export default withCustomError( | ||
refineType(t.string, "nonEmptyString", (s) => s.trim().length > 0), | ||
() => "The value must be a non-empty string", | ||
) |
2525313
60672