@prismicio/types-internal 2.2.0-alpha.13 → 2.2.0-alpha.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/lib/common/Asset.d.ts +1 -1
  2. package/lib/common/UUID.js +2 -2
  3. package/lib/import/converters/Document.js +0 -2
  4. package/lib/import/converters/fields/UID.js +0 -2
  5. package/lib/import/converters/fields/nestable/Image.js +1 -1
  6. package/lib/import/converters/fields/nestable/Link.js +6 -6
  7. package/lib/import/validators/Document.js +2 -2
  8. package/lib/import/validators/fields/ImportField.d.ts +2 -12
  9. package/lib/import/validators/fields/ImportField.js +2 -3
  10. package/lib/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.js +2 -1
  11. package/lib/import/validators/fields/ImportSlices/SharedSlice/errors.js +1 -1
  12. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.js +2 -2
  13. package/lib/import/validators/fields/UID.d.ts +2 -2
  14. package/lib/import/validators/fields/UID.js +1 -1
  15. package/lib/import/validators/fields/nestable/GeoPoint.js +1 -1
  16. package/lib/import/validators/fields/nestable/Image/Decoder.js +2 -2
  17. package/lib/import/validators/fields/nestable/Image/Validator.d.ts +32 -36
  18. package/lib/import/validators/fields/nestable/Image/Validator.js +8 -7
  19. package/lib/import/validators/fields/nestable/Image/index.js +11 -9
  20. package/lib/import/validators/fields/nestable/Image/model.d.ts +1 -1
  21. package/lib/import/validators/fields/nestable/Link.js +2 -2
  22. package/lib/utils/DocumentId.d.ts +2 -1
  23. package/lib/utils/DocumentId.js +3 -3
  24. package/lib/validators/BasicTypes.d.ts +0 -2
  25. package/lib/validators/BasicTypes.js +7 -9
  26. package/lib/validators/DefaultOrElse.js +1 -1
  27. package/lib/validators/NonEmptyString.js +1 -2
  28. package/lib/validators/function.d.ts +4 -4
  29. package/lib/validators/function.js +6 -6
  30. package/package.json +1 -1
  31. package/src/common/Asset.ts +1 -1
  32. package/src/common/UUID.ts +3 -2
  33. package/src/import/converters/Document.ts +1 -7
  34. package/src/import/converters/fields/UID.ts +0 -2
  35. package/src/import/converters/fields/nestable/Image.ts +1 -1
  36. package/src/import/converters/fields/nestable/Link.ts +4 -4
  37. package/src/import/validators/Document.ts +3 -3
  38. package/src/import/validators/fields/ImportField.ts +6 -5
  39. package/src/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.ts +4 -2
  40. package/src/import/validators/fields/ImportSlices/SharedSlice/errors.ts +1 -1
  41. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.ts +5 -2
  42. package/src/import/validators/fields/UID.ts +2 -2
  43. package/src/import/validators/fields/nestable/GeoPoint.ts +2 -2
  44. package/src/import/validators/fields/nestable/Image/Decoder.ts +2 -2
  45. package/src/import/validators/fields/nestable/Image/Validator.ts +8 -5
  46. package/src/import/validators/fields/nestable/Image/index.ts +20 -21
  47. package/src/import/validators/fields/nestable/Image/model.ts +1 -1
  48. package/src/import/validators/fields/nestable/Link.ts +2 -2
  49. package/src/utils/DocumentId.ts +4 -5
  50. package/src/validators/BasicTypes.ts +13 -16
  51. package/src/validators/DefaultOrElse.ts +1 -1
  52. package/src/validators/NonEmptyString.ts +2 -3
  53. package/src/validators/function.ts +4 -4
@@ -4,7 +4,7 @@ export declare type Asset = {
4
4
  kind: "image" | "all";
5
5
  filename?: string;
6
6
  extension?: string;
7
- size?: string;
7
+ size?: number;
8
8
  origin_url: string;
9
9
  url: string;
10
10
  width?: number;
@@ -3,6 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.UUID = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const t = (0, tslib_1.__importStar)(require("io-ts"));
6
- const io_ts_types_1 = require("io-ts-types");
7
6
  const uuid_1 = require("uuid");
8
- 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");
7
+ const function_1 = require("../validators/function");
8
+ exports.UUID = (0, function_1.withCustomError)(t.brand(t.string, (s) => (0, uuid_1.validate)(s), "UUID"), () => "The value must be a valid UUID");
@@ -11,8 +11,6 @@ function convertImportToContent(document, assets, embeds) {
11
11
  exports.convertImportToContent = convertImportToContent;
12
12
  function convertWidget(field, assets, embeds) {
13
13
  switch (field.type) {
14
- case "UID":
15
- return (0, fields_1.uidConverter)(field.value);
16
14
  case "Slices":
17
15
  return (0, fields_1.importSlicesConverter)(field.value, assets, embeds);
18
16
  default:
@@ -2,8 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.uidConverter = void 0;
4
4
  const uidConverter = (field) => {
5
- if (field === null)
6
- return;
7
5
  return {
8
6
  value: field,
9
7
  __TYPE__: "UIDContent",
@@ -30,7 +30,7 @@ function convertImage(imageField, image) {
30
30
  url: image.url,
31
31
  }, [
32
32
  ["alt", (imageField === null || imageField === void 0 ? void 0 : imageField.alt) || image.alt],
33
- ["credits", (imageField === null || imageField === void 0 ? void 0 : imageField.credit) || image.credits],
33
+ ["credits", (imageField === null || imageField === void 0 ? void 0 : imageField.copyright) || image.credits],
34
34
  ]);
35
35
  }
36
36
  function convertThumbnails(imageField, assets) {
@@ -18,30 +18,30 @@ const documentLinkConverter = (field) => ({
18
18
  __TYPE__: "LinkContent",
19
19
  });
20
20
  const fileLinkConverter = (field, asset) => {
21
- var _a, _b;
21
+ var _a;
22
22
  return ({
23
23
  value: {
24
24
  id: field.id,
25
- url: asset.url,
25
+ url: asset.origin_url,
26
26
  name: (_a = asset.filename) !== null && _a !== void 0 ? _a : "unknown.jpg",
27
27
  kind: asset.kind,
28
- size: (_b = asset.size) !== null && _b !== void 0 ? _b : "0",
28
+ size: asset.size ? String(asset.size) : "0",
29
29
  __TYPE__: "FileLink",
30
30
  },
31
31
  __TYPE__: "LinkContent",
32
32
  });
33
33
  };
34
34
  const imageLinkConverter = (field, asset) => {
35
- var _a, _b, _c, _d;
35
+ var _a, _b, _c;
36
36
  return ({
37
37
  value: {
38
38
  id: field.id,
39
- url: asset.url,
39
+ url: asset.origin_url,
40
40
  height: String((_a = asset.height) !== null && _a !== void 0 ? _a : 1),
41
41
  width: String((_b = asset.width) !== null && _b !== void 0 ? _b : 1),
42
42
  name: (_c = asset.filename) !== null && _c !== void 0 ? _c : "unknown.jpg",
43
43
  kind: asset.kind,
44
- size: (_d = asset.size) !== null && _d !== void 0 ? _d : "0",
44
+ size: asset.size ? String(asset.size) : "0",
45
45
  __TYPE__: "ImageLink",
46
46
  },
47
47
  __TYPE__: "LinkContent",
@@ -6,11 +6,11 @@ const fp_ts_1 = require("fp-ts");
6
6
  const Either_1 = require("fp-ts/Either");
7
7
  const function_1 = require("fp-ts/lib/function");
8
8
  const t = (0, tslib_1.__importStar)(require("io-ts"));
9
- const io_ts_types_1 = require("io-ts-types");
10
9
  const customtypes_1 = require("../../customtypes");
11
10
  const Objects_1 = require("../../utils/Objects");
11
+ const function_2 = require("../../validators/function");
12
12
  const ImportField_1 = require("./fields/ImportField");
13
- const rawImportDocument = (0, io_ts_types_1.withMessage)(t.record(t.string, t.unknown), () => "document is not an object");
13
+ const rawImportDocument = (0, function_2.withCustomError)(t.UnknownRecord, () => "document is not an object");
14
14
  function validateField(document, customType) {
15
15
  return (key, content, model) => {
16
16
  // the field is not defined in the custom type -> extra field or typo
@@ -1,10 +1,9 @@
1
1
  import type { StaticWidget } from "../../../customtypes";
2
2
  import { ImportSlices } from "./ImportSlices";
3
3
  import { ImportNestable } from "./nestable";
4
- import { ImportUID } from "./UID";
5
- export declare type ImportField = ImportUID | ImportSlices | ImportNestable;
4
+ export declare type ImportField = ImportSlices | ImportNestable;
6
5
  export declare const ImportField: {
7
- is(u: unknown): u is ImportNestable;
6
+ is(u: unknown): u is ImportField;
8
7
  decode: (field: StaticWidget) => (content: unknown) => {
9
8
  codec: import("io-ts").Type<{
10
9
  type: "Boolean";
@@ -113,15 +112,6 @@ export declare const ImportField: {
113
112
  type: "Boolean";
114
113
  value: boolean | null;
115
114
  }>;
116
- } | {
117
- codec: import("io-ts").Type<{
118
- type: "UID";
119
- value: string | null;
120
- }, string | undefined, unknown>;
121
- result: import("io-ts").Validation<{
122
- type: "UID";
123
- value: string | null;
124
- }>;
125
115
  } | {
126
116
  codec: import("io-ts").Type<{
127
117
  type: "Slices";
@@ -3,16 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ImportField = void 0;
4
4
  const ImportSlices_1 = require("./ImportSlices");
5
5
  const nestable_1 = require("./nestable");
6
- const UID_1 = require("./UID");
7
6
  exports.ImportField = {
8
7
  is(u) {
9
- return UID_1.ImportUID.is(u) || nestable_1.ImportNestable.is(u);
8
+ return exports.ImportField.is(u);
10
9
  },
11
10
  decode: (field) => {
12
11
  return (content) => {
13
12
  switch (field.type) {
14
13
  case "UID":
15
- return { codec: UID_1.ImportUID, result: UID_1.ImportUID.decode(content) };
14
+ throw new Error(`The field '${field.type}' should be put at the root level of the document`);
16
15
  case "Slices":
17
16
  return {
18
17
  codec: (0, ImportSlices_1.ImportSlices)(field),
@@ -6,6 +6,7 @@ const E = (0, tslib_1.__importStar)(require("fp-ts/Either"));
6
6
  const function_1 = require("fp-ts/function");
7
7
  const t = (0, tslib_1.__importStar)(require("io-ts"));
8
8
  const validators_1 = require("../../../../../validators");
9
+ const function_2 = require("../../../../../validators/function");
9
10
  const fields_1 = require("./fields");
10
11
  const SharedSliceShape = t.type({
11
12
  id: fields_1.OptionalSharedSliceId,
@@ -44,7 +45,7 @@ t
44
45
  .partial({
45
46
  primary: (0, fields_1.SharedSliceContent)(decoded.slice_type.slice_type, "primary", (_a = decoded.variation.data.primary) !== null && _a !== void 0 ? _a : {}),
46
47
  // We need to use this wrapper codec instead of using t.array(SharedSliceContent) directly in order to get proper custom error messages
47
- 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 : {}))),
48
+ 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"),
48
49
  })
49
50
  .validate(u, c), E.map(({ primary, items }) => ({ ...decoded, primary, items })));
50
51
  }), E.map(({ id, slice_type, variation, primary, items, slice_label, version, }) => ({
@@ -2,6 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SharedSliceErrors = void 0;
4
4
  exports.SharedSliceErrors = {
5
- SliceTypeNotFound: (slice_type) => `Shared slice '${slice_type}' not found in document's custom type`,
5
+ SliceTypeNotFound: (slice_type) => `Shared slice '${slice_type}' not found in this slice zone in the document's custom type`,
6
6
  VariationNotFound: (variation, slice_type) => `Variation '${variation}' not found for Slice '${slice_type}'`,
7
7
  };
@@ -7,7 +7,7 @@ const function_1 = require("fp-ts/lib/function");
7
7
  const t = (0, tslib_1.__importStar)(require("io-ts"));
8
8
  const common_1 = require("../../../../../../../common");
9
9
  const io_ts_1 = require("../../../../../../../utils/io-ts");
10
- const validators_1 = require("../../../../../../../validators");
10
+ const function_2 = require("../../../../../../../validators/function");
11
11
  const SharedSliceContentEntry_1 = require("./SharedSliceContentEntry");
12
12
  /**
13
13
  * Builds the decoder for a single entry in the 'primary' field or a single item in the 'items' array in the SharedSlice.
@@ -19,7 +19,7 @@ const SharedSliceContentEntry_1 = require("./SharedSliceContentEntry");
19
19
  */
20
20
  const SharedSliceContent = (sliceName, sliceContentField, sliceFieldModels) => new t.Type("SharedSliceContent", (u) => t.record(common_1.WidgetKey, t.unknown).is(u), (u, c) => (0, function_1.pipe)(
21
21
  // We first decode the raw content object in the shape of Record<WidgetKey, unknown>
22
- validators_1.AnyObject.validate(u, c), E.chain((rawContent) => (0, function_1.pipe)(
22
+ (0, function_2.withCustomError)(t.UnknownRecord, () => "The value must be an object").validate(u, c), E.chain((rawContent) => (0, function_1.pipe)(
23
23
  // We split the object into entries because there is no easy other way to decode each value with a different codec
24
24
  Object.entries(rawContent).map((entry) => (0, SharedSliceContentEntry_1.SharedSliceContentEntry)(sliceName, sliceContentField, sliceFieldModels).validate(entry, c)),
25
25
  // We get a validation result for each entry in the content object (t.Validation<SharedSliceContentEntry>[]),
@@ -1,6 +1,6 @@
1
1
  import * as t from "io-ts";
2
2
  export declare const ImportUID: t.Type<{
3
3
  type: "UID";
4
- value: string | null;
5
- }, string | undefined, unknown>;
4
+ value: string;
5
+ }, string, unknown>;
6
6
  export declare type ImportUID = t.TypeOf<typeof ImportUID>;
@@ -12,4 +12,4 @@ const uidValue = validators_1.String.pipe(new t.Type("ImportUIDField", (u) => va
12
12
  }
13
13
  return t.failure(n, c, "UID can only contain alphanumeric characters, dashes, underscores, and dots");
14
14
  }, t.identity));
15
- exports.ImportUID = (0, ImportContent_1.ImportContent)("UID", (0, validators_1.NullOrElse)(uidValue));
15
+ exports.ImportUID = (0, ImportContent_1.ImportContent)("UID", uidValue);
@@ -6,7 +6,7 @@ const t = (0, tslib_1.__importStar)(require("io-ts"));
6
6
  const validators_1 = require("../../../../validators");
7
7
  const function_1 = require("../../../../validators/function");
8
8
  const ImportContent_1 = require("../ImportContent");
9
- const GeooPoint = (0, function_1.withFallbackMessage)(t.strict({
9
+ const GeooPoint = (0, function_1.withCustomError)(t.strict({
10
10
  latitude: (0, validators_1.NumberRange)(-90, 90, "latitude"),
11
11
  longitude: (0, validators_1.NumberRange)(-180, 180, "longitude"),
12
12
  }), () => "GeoPoint must be an object with the properties `latitude` and `longitude` between the given ranges");
@@ -10,7 +10,7 @@ const decodeImageField = (image, mainImage, constraints) => {
10
10
  const width = (_d = (_b = constraints === null || constraints === void 0 ? void 0 : constraints.width) !== null && _b !== void 0 ? _b : (_c = image === null || image === void 0 ? void 0 : image.dimensions) === null || _c === void 0 ? void 0 : _c.width) !== null && _d !== void 0 ? _d : (_e = mainImage.dimensions) === null || _e === void 0 ? void 0 : _e.width;
11
11
  const height = (_h = (_f = constraints === null || constraints === void 0 ? void 0 : constraints.height) !== null && _f !== void 0 ? _f : (_g = image === null || image === void 0 ? void 0 : image.dimensions) === null || _g === void 0 ? void 0 : _g.height) !== null && _h !== void 0 ? _h : (_j = mainImage.dimensions) === null || _j === void 0 ? void 0 : _j.height;
12
12
  const alt = image === null || image === void 0 ? void 0 : image.alt;
13
- const credit = image === null || image === void 0 ? void 0 : image.credit;
13
+ const copyright = image === null || image === void 0 ? void 0 : image.copyright;
14
14
  return {
15
15
  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,
16
16
  edit: {
@@ -24,7 +24,7 @@ const decodeImageField = (image, mainImage, constraints) => {
24
24
  ...(height !== undefined ? { height } : {}),
25
25
  },
26
26
  ...(alt !== undefined ? { alt } : {}),
27
- ...(credit !== undefined ? { credit } : {}),
27
+ ...(copyright !== undefined ? { copyright } : {}),
28
28
  };
29
29
  };
30
30
  exports.decodeImageField = decodeImageField;
@@ -1,37 +1,33 @@
1
1
  import * as t from "io-ts";
2
- export declare const ImageFieldValidator: t.Type<{
3
- id: string;
4
- } & {
5
- dimensions?: {
6
- width?: number;
7
- height?: number;
8
- };
9
- edit?: {
10
- x?: number;
11
- y?: number;
12
- zoom?: number;
13
- background?: string;
14
- };
15
- credit?: string | null;
16
- alt?: string | null;
17
- }, {
18
- [x: string]: unknown;
19
- }, unknown>;
20
- export declare const ThumbnailsValidator: t.RecordC<t.StringC, t.Type<{
21
- id: string;
22
- } & {
23
- dimensions?: {
24
- width?: number;
25
- height?: number;
26
- };
27
- edit?: {
28
- x?: number;
29
- y?: number;
30
- zoom?: number;
31
- background?: string;
32
- };
33
- credit?: string | null;
34
- alt?: string | null;
35
- }, {
36
- [x: string]: unknown;
37
- }, unknown>>;
2
+ export declare const ImageFieldValidator: t.IntersectionC<[t.TypeC<{
3
+ id: t.StringC;
4
+ }>, t.PartialC<{
5
+ dimensions: t.PartialC<{
6
+ width: t.NumberC;
7
+ height: t.NumberC;
8
+ }>;
9
+ edit: t.PartialC<{
10
+ x: t.NumberC;
11
+ y: t.NumberC;
12
+ zoom: t.NumberC;
13
+ background: t.StringC;
14
+ }>;
15
+ copyright: t.UnionC<[t.StringC, t.NullC]>;
16
+ alt: t.UnionC<[t.StringC, t.NullC]>;
17
+ }>]>;
18
+ export declare const ThumbnailsValidator: t.RecordC<t.StringC, t.IntersectionC<[t.TypeC<{
19
+ id: t.StringC;
20
+ }>, t.PartialC<{
21
+ dimensions: t.PartialC<{
22
+ width: t.NumberC;
23
+ height: t.NumberC;
24
+ }>;
25
+ edit: t.PartialC<{
26
+ x: t.NumberC;
27
+ y: t.NumberC;
28
+ zoom: t.NumberC;
29
+ background: t.StringC;
30
+ }>;
31
+ copyright: t.UnionC<[t.StringC, t.NullC]>;
32
+ alt: t.UnionC<[t.StringC, t.NullC]>;
33
+ }>]>>;
@@ -4,23 +4,24 @@ exports.ThumbnailsValidator = exports.ImageFieldValidator = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const t = (0, tslib_1.__importStar)(require("io-ts"));
6
6
  const BasicTypes_1 = require("../../../../../validators/BasicTypes");
7
- exports.ImageFieldValidator = BasicTypes_1.AnyObject.pipe(t.intersection([
7
+ const function_1 = require("../../../../../validators/function");
8
+ exports.ImageFieldValidator = (0, function_1.withCustomError)(t.intersection([
8
9
  t.type({
9
10
  id: BasicTypes_1.String,
10
11
  }),
11
12
  t.partial({
12
- dimensions: BasicTypes_1.AnyObject.pipe(t.partial({
13
+ dimensions: (0, function_1.withCustomError)(t.partial({
13
14
  width: BasicTypes_1.Number,
14
15
  height: BasicTypes_1.Number,
15
- })),
16
- edit: BasicTypes_1.AnyObject.pipe(t.partial({
16
+ }), () => "The value must be an object"),
17
+ edit: (0, function_1.withCustomError)(t.partial({
17
18
  x: BasicTypes_1.Number,
18
19
  y: BasicTypes_1.Number,
19
20
  zoom: BasicTypes_1.Number,
20
21
  background: BasicTypes_1.String,
21
- })),
22
- credit: BasicTypes_1.StringOrNull,
22
+ }), () => "The value must be an object"),
23
+ copyright: BasicTypes_1.StringOrNull,
23
24
  alt: BasicTypes_1.StringOrNull,
24
25
  }),
25
- ]));
26
+ ]), () => "The value must be an object");
26
27
  exports.ThumbnailsValidator = t.record(t.string, exports.ImageFieldValidator);
@@ -5,25 +5,27 @@ const tslib_1 = require("tslib");
5
5
  const Either_1 = require("fp-ts/Either");
6
6
  const function_1 = require("fp-ts/function");
7
7
  const t = (0, tslib_1.__importStar)(require("io-ts"));
8
- const io_ts_types_1 = require("io-ts-types");
9
8
  const validators_1 = require("../../../../../validators");
9
+ const function_2 = require("../../../../../validators/function");
10
10
  const ImportContent_1 = require("../../ImportContent");
11
11
  const Decoder_1 = require("./Decoder");
12
12
  const Validator_1 = require("./Validator");
13
13
  const defaultImage = (field) => {
14
- var _a, _b;
15
- 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)) || [];
16
- const thumbnailsCodec = (0, io_ts_types_1.withMessage)(t.type(thumbnailsNames.reduce((acc, name) => ({
14
+ var _a;
15
+ // If there is not thumbnails we expect an empty object
16
+ if (!((_a = field === null || field === void 0 ? void 0 : field.config) === null || _a === void 0 ? void 0 : _a.thumbnails) || !field.config.thumbnails.length)
17
+ return validators_1.EmptyObject;
18
+ // If there are thumbnails, we expect an object with thumbnails as empty objects
19
+ return (0, function_2.withCustomError)(t.strict(field.config.thumbnails.reduce((acc, thumbnail) => ({
17
20
  ...acc,
18
- [name]: validators_1.EmptyObject,
19
- }), {})), () => "The value must be an object with thumbnails as empty objects");
20
- return validators_1.AnyObject.pipe(thumbnailsCodec);
21
+ [thumbnail.name]: validators_1.EmptyObject,
22
+ }), {})), () => "The value must be an object");
21
23
  };
22
24
  const ImageField = (field) => new t.Type("ImageField", (u) => Validator_1.ImageFieldValidator.is(u) &&
23
25
  "thumbnails" in u &&
24
26
  Validator_1.ThumbnailsValidator.is(u["thumbnails"]), (u, ctx) => {
25
27
  return (0, function_1.pipe)(Validator_1.ImageFieldValidator.validate(u, ctx), (0, Either_1.chain)((mainImage) => {
26
- const { id, dimensions, edit, credit, alt, ...maybeThumbnails } = mainImage;
28
+ const { id, dimensions, edit, copyright, alt, ...maybeThumbnails } = mainImage;
27
29
  return (0, function_1.pipe)(Validator_1.ThumbnailsValidator.validate(maybeThumbnails, ctx), (0, Either_1.map)((thumbnails) => {
28
30
  var _a;
29
31
  return ({
@@ -33,7 +35,7 @@ const ImageField = (field) => new t.Type("ImageField", (u) => Validator_1.ImageF
33
35
  }));
34
36
  }));
35
37
  }, t.identity);
36
- const ImageCodec = (field) => (0, validators_1.DefaultOrElse)(defaultImage(field))(ImageField(field));
38
+ const ImageCodec = (field) => (0, function_2.withCustomError)((0, validators_1.DefaultOrElse)(defaultImage(field))(ImageField(field)), () => "An image field must be an object");
37
39
  const ImportImage = (field) => (0, ImportContent_1.ImportContent)("Image", ImageCodec(field));
38
40
  exports.ImportImage = ImportImage;
39
41
  (0, tslib_1.__exportStar)(require("./model"), exports);
@@ -10,7 +10,7 @@ export declare type ImageField = {
10
10
  width?: number;
11
11
  height?: number;
12
12
  };
13
- credit?: string | null;
13
+ copyright?: string | null;
14
14
  alt?: string | null;
15
15
  };
16
16
  export declare type ImageFieldWithThumbnails = ImageField & {
@@ -3,12 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ImportLink = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const t = (0, tslib_1.__importStar)(require("io-ts"));
6
- const io_ts_types_1 = require("io-ts-types");
7
6
  const DocumentId_1 = require("../../../../utils/DocumentId");
8
7
  const validators_1 = require("../../../../validators");
8
+ const function_1 = require("../../../../validators/function");
9
9
  const ImportContent_1 = require("../ImportContent");
10
10
  const LinkTypeValidator = t.type({
11
- 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`"),
11
+ 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`"),
12
12
  });
13
13
  const WebLink = t.intersection([
14
14
  t.type({
@@ -1 +1,2 @@
1
- export declare const DocumentId: import("io-ts").Type<string, string, unknown>;
1
+ import * as t from "io-ts";
2
+ export declare const DocumentId: t.Type<string, string, unknown>;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DocumentId = void 0;
4
- const io_ts_types_1 = require("io-ts-types");
5
- const validators_1 = require("../validators");
4
+ const tslib_1 = require("tslib");
5
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
6
6
  const function_1 = require("../validators/function");
7
- 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");
7
+ exports.DocumentId = (0, function_1.withCustomError)((0, function_1.refineType)(t.string, "DocumentId", (s) => s.length === 16), () => "DocumentId must be a 16 character string");
@@ -8,5 +8,3 @@ export declare const EmptyObject: t.Type<Record<never, never>, {
8
8
  [key: string]: unknown;
9
9
  }, unknown>;
10
10
  export declare const EmptyArray: t.Type<never[], never[], unknown>;
11
- export declare const AnyObject: t.RecordC<t.StringC, t.UnknownC>;
12
- export declare const AnyArray: t.ArrayC<t.UnknownC>;
@@ -1,16 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AnyArray = exports.AnyObject = exports.EmptyArray = exports.EmptyObject = exports.Boolean = exports.NumberOrNull = exports.Number = exports.StringOrNull = exports.String = void 0;
3
+ exports.EmptyArray = exports.EmptyObject = exports.Boolean = exports.NumberOrNull = exports.Number = exports.StringOrNull = exports.String = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const fp_ts_1 = require("fp-ts");
6
6
  const function_1 = require("fp-ts/lib/function");
7
7
  const t = (0, tslib_1.__importStar)(require("io-ts"));
8
- const io_ts_types_1 = require("io-ts-types");
9
- exports.String = (0, io_ts_types_1.withMessage)(t.string, () => "The value must be a string");
10
- exports.StringOrNull = (0, io_ts_types_1.withMessage)(t.union([t.string, t.null]), () => "The value must be a string or null");
11
- exports.Number = (0, io_ts_types_1.withMessage)(t.number, () => "The value must be a number");
12
- exports.NumberOrNull = (0, io_ts_types_1.withMessage)(t.union([t.number, t.null]), () => "The value must be a number or null");
13
- exports.Boolean = (0, io_ts_types_1.withMessage)(t.boolean, () => "The value must be a boolean");
8
+ const function_2 = require("./function");
9
+ exports.String = (0, function_2.withCustomError)(t.string, () => "The value must be a string");
10
+ exports.StringOrNull = (0, function_2.withCustomError)(t.union([t.string, t.null]), () => "The value must be a string or null");
11
+ exports.Number = (0, function_2.withCustomError)(t.number, () => "The value must be a number");
12
+ exports.NumberOrNull = (0, function_2.withCustomError)(t.union([t.number, t.null]), () => "The value must be a number or null");
13
+ exports.Boolean = (0, function_2.withCustomError)(t.boolean, () => "The value must be a boolean");
14
14
  exports.EmptyObject = t.UnknownRecord.pipe(new t.Type("emptyObject", (u) => t.UnknownRecord.is(u), (u, c) => {
15
15
  if (Object.keys(u).length > 0)
16
16
  return t.failure(u, c, "The object is not empty.");
@@ -23,5 +23,3 @@ exports.EmptyArray = new t.Type("emptyArray", (u) => t.UnknownArray.is(u) && u.l
23
23
  return t.success(new Array());
24
24
  }));
25
25
  }, () => []);
26
- exports.AnyObject = (0, io_ts_types_1.withMessage)(t.record(t.string, t.unknown), () => "The value must be an object");
27
- exports.AnyArray = (0, io_ts_types_1.withMessage)(t.array(t.unknown), () => "The value must be an array");
@@ -6,7 +6,7 @@ const Either_1 = require("fp-ts/lib/Either");
6
6
  const t = (0, tslib_1.__importStar)(require("io-ts"));
7
7
  const BasicTypes_1 = require("./BasicTypes");
8
8
  const DefaultOrElse = (inputValidator) => {
9
- return (codec) => new t.Type("DefaultOrT", (u) => null === u || codec.is(u), (u) => {
9
+ return (codec) => new t.Type("DefaultOrElse", (u) => null === u || codec.is(u), (u) => {
10
10
  if ((0, Either_1.isRight)(inputValidator.decode(u)))
11
11
  return t.success(null);
12
12
  return codec.decode(u);
@@ -2,6 +2,5 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  const t = (0, tslib_1.__importStar)(require("io-ts"));
5
- const io_ts_types_1 = require("io-ts-types");
6
5
  const function_1 = require("./function");
7
- 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");
6
+ 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");
@@ -19,16 +19,16 @@ export declare function addType<A, O extends object, I, T extends string>(codec:
19
19
  *
20
20
  * @example
21
21
  * expect(
22
- * withFallbackMessage(
23
- * t.type({age: withMessage(t.number, () => 'Invalid child')}),
22
+ * withCustomError(
23
+ * t.type({age: withCustomError(t.number, () => 'Invalid child')}),
24
24
  * () => "Invalid parent"
25
25
  * )
26
26
  * ).toSubsetEqualLeft([{message: "Invalid child"}])
27
27
  * expect(
28
- * withFallbackMessage(
28
+ * withCustomError(
29
29
  * t.type({age: t.number}),
30
30
  * () => "Invalid parent"
31
31
  * )
32
32
  * ).toSubsetEqualLeft([{message: "Invalid parent"}])
33
33
  */
34
- export declare function withFallbackMessage<C extends t.Any>(codec: C, message: (i: t.InputOf<C>, c: t.Context) => string): C;
34
+ export declare function withCustomError<C extends t.Any>(codec: C, message: (i: t.InputOf<C>, c: t.Context) => string): C;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.withFallbackMessage = exports.addType = exports.isEmpty = exports.objectToMap = exports.grouped = exports.formatDate = exports.formatDateTime = exports.filterDouble = exports.refineType = exports.nullable = void 0;
3
+ exports.withCustomError = exports.addType = exports.isEmpty = exports.objectToMap = exports.grouped = exports.formatDate = exports.formatDateTime = exports.filterDouble = exports.refineType = exports.nullable = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const fp_ts_1 = require("fp-ts");
6
6
  const function_1 = require("fp-ts/function");
@@ -66,19 +66,19 @@ exports.addType = addType;
66
66
  *
67
67
  * @example
68
68
  * expect(
69
- * withFallbackMessage(
70
- * t.type({age: withMessage(t.number, () => 'Invalid child')}),
69
+ * withCustomError(
70
+ * t.type({age: withCustomError(t.number, () => 'Invalid child')}),
71
71
  * () => "Invalid parent"
72
72
  * )
73
73
  * ).toSubsetEqualLeft([{message: "Invalid child"}])
74
74
  * expect(
75
- * withFallbackMessage(
75
+ * withCustomError(
76
76
  * t.type({age: t.number}),
77
77
  * () => "Invalid parent"
78
78
  * )
79
79
  * ).toSubsetEqualLeft([{message: "Invalid parent"}])
80
80
  */
81
- function withFallbackMessage(codec, message) {
81
+ function withCustomError(codec, message) {
82
82
  return (0, io_ts_types_1.withValidate)(codec, (i, c) => {
83
83
  return fp_ts_1.either.mapLeft((errors) => {
84
84
  if (errors.find((error) => error.message)) {
@@ -97,4 +97,4 @@ function withFallbackMessage(codec, message) {
97
97
  })(codec.validate(i, c));
98
98
  });
99
99
  }
100
- exports.withFallbackMessage = withFallbackMessage;
100
+ exports.withCustomError = withCustomError;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "2.2.0-alpha.13",
3
+ "version": "2.2.0-alpha.15",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -4,7 +4,7 @@ export type Asset = {
4
4
  kind: "image" | "all"
5
5
  filename?: string
6
6
  extension?: string
7
- size?: string
7
+ size?: number
8
8
  origin_url: string
9
9
  url: string
10
10
  width?: number
@@ -1,12 +1,13 @@
1
1
  import * as t from "io-ts"
2
- import { withMessage } from "io-ts-types"
3
2
  import { validate as validateUUID } from "uuid"
4
3
 
4
+ import { withCustomError } from "../validators/function"
5
+
5
6
  interface UUIDBrand {
6
7
  readonly UUID: unique symbol
7
8
  }
8
9
 
9
- export const UUID = withMessage(
10
+ export const UUID = withCustomError(
10
11
  t.brand(
11
12
  t.string,
12
13
  (s): s is t.Branded<string, UUIDBrand> => validateUUID(s),
@@ -2,11 +2,7 @@ import type { Asset, Embed } from "../../common"
2
2
  import type { Document, WidgetContent } from "../../content"
3
3
  import type { ImportDocument } from "../validators"
4
4
  import type { ImportField } from "../validators/fields/ImportField"
5
- import {
6
- convertNestableWidget,
7
- importSlicesConverter,
8
- uidConverter,
9
- } from "./fields"
5
+ import { convertNestableWidget, importSlicesConverter } from "./fields"
10
6
 
11
7
  export function convertImportToContent(
12
8
  document: ImportDocument,
@@ -28,8 +24,6 @@ function convertWidget(
28
24
  embeds: Record<string, Embed | undefined>,
29
25
  ): WidgetContent | undefined {
30
26
  switch (field.type) {
31
- case "UID":
32
- return uidConverter(field.value)
33
27
  case "Slices":
34
28
  return importSlicesConverter(field.value, assets, embeds)
35
29
  default:
@@ -4,8 +4,6 @@ import type { ImportUID } from "../../validators/fields/UID"
4
4
  export const uidConverter = (
5
5
  field: ImportUID["value"],
6
6
  ): UIDContent | undefined => {
7
- if (field === null) return
8
-
9
7
  return {
10
8
  value: field,
11
9
  __TYPE__: "UIDContent",
@@ -34,7 +34,7 @@ function convertImage(imageField: ImageField, image: Asset) {
34
34
  },
35
35
  [
36
36
  ["alt", imageField?.alt || image.alt],
37
- ["credits", imageField?.credit || image.credits],
37
+ ["credits", imageField?.copyright || image.credits],
38
38
  ],
39
39
  )
40
40
  }
@@ -28,10 +28,10 @@ const documentLinkConverter = (field: DocumentLink): LinkContent => ({
28
28
  const fileLinkConverter = (field: MediaLink, asset: Asset): LinkContent => ({
29
29
  value: {
30
30
  id: field.id,
31
- url: asset.url,
31
+ url: asset.origin_url,
32
32
  name: asset.filename ?? "unknown.jpg",
33
33
  kind: asset.kind,
34
- size: asset.size ?? "0",
34
+ size: asset.size ? String(asset.size) : "0",
35
35
  __TYPE__: "FileLink",
36
36
  },
37
37
  __TYPE__: "LinkContent",
@@ -40,12 +40,12 @@ const fileLinkConverter = (field: MediaLink, asset: Asset): LinkContent => ({
40
40
  const imageLinkConverter = (field: MediaLink, asset: Asset): LinkContent => ({
41
41
  value: {
42
42
  id: field.id,
43
- url: asset.url,
43
+ url: asset.origin_url,
44
44
  height: String(asset.height ?? 1),
45
45
  width: String(asset.width ?? 1),
46
46
  name: asset.filename ?? "unknown.jpg",
47
47
  kind: asset.kind,
48
- size: asset.size ?? "0",
48
+ size: asset.size ? String(asset.size) : "0",
49
49
  __TYPE__: "ImageLink",
50
50
  },
51
51
  __TYPE__: "LinkContent",
@@ -2,16 +2,16 @@ import { either } from "fp-ts"
2
2
  import { Either, isLeft } from "fp-ts/Either"
3
3
  import { pipe } from "fp-ts/lib/function"
4
4
  import * as t from "io-ts"
5
- import { withMessage } from "io-ts-types"
6
5
 
7
6
  import type { WidgetKey } from "../../common"
8
7
  import type { StaticCustomType, StaticWidget } from "../../customtypes"
9
8
  import { flattenCustomTypeFields } from "../../customtypes"
10
9
  import { isObject } from "../../utils/Objects"
10
+ import { withCustomError } from "../../validators/function"
11
11
  import { ImportField } from "./fields/ImportField"
12
12
 
13
- const rawImportDocument = withMessage(
14
- t.record(t.string, t.unknown),
13
+ const rawImportDocument = withCustomError(
14
+ t.UnknownRecord,
15
15
  () => "document is not an object",
16
16
  )
17
17
 
@@ -1,19 +1,20 @@
1
1
  import type { StaticWidget } from "../../../customtypes"
2
2
  import { ImportSlices } from "./ImportSlices"
3
3
  import { ImportNestable } from "./nestable"
4
- import { ImportUID } from "./UID"
5
4
 
6
- export type ImportField = ImportUID | ImportSlices | ImportNestable
5
+ export type ImportField = ImportSlices | ImportNestable
7
6
 
8
7
  export const ImportField = {
9
- is(u: unknown): u is ImportNestable {
10
- return ImportUID.is(u) || ImportNestable.is(u)
8
+ is(u: unknown): u is ImportField {
9
+ return ImportField.is(u)
11
10
  },
12
11
  decode: (field: StaticWidget) => {
13
12
  return (content: unknown) => {
14
13
  switch (field.type) {
15
14
  case "UID":
16
- return { codec: ImportUID, result: ImportUID.decode(content) }
15
+ throw new Error(
16
+ `The field '${field.type}' should be put at the root level of the document`,
17
+ )
17
18
  case "Slices":
18
19
  return {
19
20
  codec: ImportSlices(field),
@@ -3,7 +3,8 @@ import { pipe } from "fp-ts/function"
3
3
  import * as t from "io-ts"
4
4
 
5
5
  import type { SharedSlice as SharedSliceCustomType } from "../../../../../customtypes"
6
- import { AnyArray, NonEmptyString, String } from "../../../../../validators"
6
+ import { NonEmptyString, String } from "../../../../../validators"
7
+ import { withCustomError } from "../../../../../validators/function"
7
8
  import {
8
9
  OptionalSharedSliceId,
9
10
  SharedSliceContent,
@@ -75,7 +76,7 @@ export const SharedSlice = (sharedSlices: SharedSliceCustomType[]) =>
75
76
  decoded.variation.data.primary ?? {},
76
77
  ),
77
78
  // We need to use this wrapper codec instead of using t.array(SharedSliceContent) directly in order to get proper custom error messages
78
- items: AnyArray.pipe(
79
+ items: withCustomError(
79
80
  t.array(
80
81
  SharedSliceContent(
81
82
  decoded.slice_type.slice_type,
@@ -83,6 +84,7 @@ export const SharedSlice = (sharedSlices: SharedSliceCustomType[]) =>
83
84
  decoded.variation.data.items ?? {},
84
85
  ),
85
86
  ),
87
+ () => "The value must be an array",
86
88
  ),
87
89
  })
88
90
  .validate(u, c),
@@ -1,6 +1,6 @@
1
1
  export const SharedSliceErrors = {
2
2
  SliceTypeNotFound: (slice_type: string) =>
3
- `Shared slice '${slice_type}' not found in document's custom type`,
3
+ `Shared slice '${slice_type}' not found in this slice zone in the document's custom type`,
4
4
  VariationNotFound: (variation: string, slice_type: string) =>
5
5
  `Variation '${variation}' not found for Slice '${slice_type}'`,
6
6
  } as const
@@ -5,7 +5,7 @@ import * as t from "io-ts"
5
5
  import { WidgetKey } from "../../../../../../../common"
6
6
  import type { NestableWidget } from "../../../../../../../customtypes"
7
7
  import { combineValidationResults } from "../../../../../../../utils/io-ts"
8
- import { AnyObject } from "../../../../../../../validators"
8
+ import { withCustomError } from "../../../../../../../validators/function"
9
9
  import type { ImportNestable } from "../../../../nestable"
10
10
  import { SharedSliceContentEntry } from "./SharedSliceContentEntry"
11
11
 
@@ -33,7 +33,10 @@ export const SharedSliceContent = (
33
33
  (u, c) =>
34
34
  pipe(
35
35
  // We first decode the raw content object in the shape of Record<WidgetKey, unknown>
36
- AnyObject.validate(u, c),
36
+ withCustomError(
37
+ t.UnknownRecord,
38
+ () => "The value must be an object",
39
+ ).validate(u, c),
37
40
  E.chain((rawContent) =>
38
41
  pipe(
39
42
  // We split the object into entries because there is no easy other way to decode each value with a different codec
@@ -1,6 +1,6 @@
1
1
  import * as t from "io-ts"
2
2
 
3
- import { NullOrElse, String } from "../../../validators"
3
+ import { String } from "../../../validators"
4
4
  import { ImportContent } from "./ImportContent"
5
5
 
6
6
  const regex = /^[a-z0-9-_.]+$/
@@ -23,5 +23,5 @@ const uidValue = String.pipe(
23
23
  ),
24
24
  )
25
25
 
26
- export const ImportUID = ImportContent("UID", NullOrElse(uidValue))
26
+ export const ImportUID = ImportContent("UID", uidValue)
27
27
  export type ImportUID = t.TypeOf<typeof ImportUID>
@@ -2,10 +2,10 @@ import type { TypeOf } from "io-ts"
2
2
  import * as t from "io-ts"
3
3
 
4
4
  import { EmptyObjectOrElse, NumberRange } from "../../../../validators"
5
- import { withFallbackMessage } from "../../../../validators/function"
5
+ import { withCustomError } from "../../../../validators/function"
6
6
  import { ImportContent } from "../ImportContent"
7
7
 
8
- const GeooPoint = withFallbackMessage(
8
+ const GeooPoint = withCustomError(
9
9
  t.strict({
10
10
  latitude: NumberRange(-90, 90, "latitude"),
11
11
  longitude: NumberRange(-180, 180, "longitude"),
@@ -23,7 +23,7 @@ export const decodeImageField = (
23
23
  image?.dimensions?.height ??
24
24
  mainImage.dimensions?.height
25
25
  const alt = image?.alt
26
- const credit = image?.credit
26
+ const copyright = image?.copyright
27
27
 
28
28
  return {
29
29
  id: image?.id ?? mainImage?.id,
@@ -38,7 +38,7 @@ export const decodeImageField = (
38
38
  ...(height !== undefined ? { height } : {}),
39
39
  },
40
40
  ...(alt !== undefined ? { alt } : {}),
41
- ...(credit !== undefined ? { credit } : {}),
41
+ ...(copyright !== undefined ? { copyright } : {}),
42
42
  }
43
43
  }
44
44
  // Merges provided thumbnails with custom type constraints.
@@ -1,35 +1,38 @@
1
1
  import * as t from "io-ts"
2
2
 
3
3
  import {
4
- AnyObject,
5
4
  Number,
6
5
  String,
7
6
  StringOrNull,
8
7
  } from "../../../../../validators/BasicTypes"
8
+ import { withCustomError } from "../../../../../validators/function"
9
9
 
10
- export const ImageFieldValidator = AnyObject.pipe(
10
+ export const ImageFieldValidator = withCustomError(
11
11
  t.intersection([
12
12
  t.type({
13
13
  id: String,
14
14
  }),
15
15
  t.partial({
16
- dimensions: AnyObject.pipe(
16
+ dimensions: withCustomError(
17
17
  t.partial({
18
18
  width: Number,
19
19
  height: Number,
20
20
  }),
21
+ () => "The value must be an object",
21
22
  ),
22
- edit: AnyObject.pipe(
23
+ edit: withCustomError(
23
24
  t.partial({
24
25
  x: Number,
25
26
  y: Number,
26
27
  zoom: Number,
27
28
  background: String,
28
29
  }),
30
+ () => "The value must be an object",
29
31
  ),
30
- credit: StringOrNull,
32
+ copyright: StringOrNull,
31
33
  alt: StringOrNull,
32
34
  }),
33
35
  ]),
36
+ () => "The value must be an object",
34
37
  )
35
38
  export const ThumbnailsValidator = t.record(t.string, ImageFieldValidator)
@@ -2,35 +2,33 @@ import { chain, map } from "fp-ts/Either"
2
2
  import { pipe } from "fp-ts/function"
3
3
  import type { Validation } from "io-ts"
4
4
  import * as t from "io-ts"
5
- import { withMessage } from "io-ts-types"
6
5
 
7
6
  import type { Image as ImageDefinition } from "../../../../../customtypes"
8
- import {
9
- AnyObject,
10
- DefaultOrElse,
11
- EmptyObject,
12
- } from "../../../../../validators"
7
+ import { DefaultOrElse, EmptyObject } from "../../../../../validators"
8
+ import { withCustomError } from "../../../../../validators/function"
13
9
  import { ImportContent } from "../../ImportContent"
14
10
  import { decodeImageField, decodeThumbnails } from "./Decoder"
15
11
  import type { ImageFieldWithThumbnails } from "./model"
16
12
  import { ImageFieldValidator, ThumbnailsValidator } from "./Validator"
17
13
 
18
14
  const defaultImage = (field?: ImageDefinition) => {
19
- const thumbnailsNames = field?.config?.thumbnails?.map((t) => t.name) || []
20
- const thumbnailsCodec = withMessage(
21
- t.type(
22
- thumbnailsNames.reduce(
23
- (acc, name) => ({
15
+ // If there is not thumbnails we expect an empty object
16
+ if (!field?.config?.thumbnails || !field.config.thumbnails.length)
17
+ return EmptyObject
18
+
19
+ // If there are thumbnails, we expect an object with thumbnails as empty objects
20
+ return withCustomError(
21
+ t.strict(
22
+ field.config.thumbnails.reduce(
23
+ (acc, thumbnail) => ({
24
24
  ...acc,
25
- [name]: EmptyObject,
25
+ [thumbnail.name]: EmptyObject,
26
26
  }),
27
27
  {},
28
28
  ),
29
29
  ),
30
- () => "The value must be an object with thumbnails as empty objects",
30
+ () => "The value must be an object",
31
31
  )
32
-
33
- return AnyObject.pipe(thumbnailsCodec)
34
32
  }
35
33
 
36
34
  const ImageField = (field?: ImageDefinition) =>
@@ -44,7 +42,7 @@ const ImageField = (field?: ImageDefinition) =>
44
42
  return pipe(
45
43
  ImageFieldValidator.validate(u, ctx),
46
44
  chain((mainImage) => {
47
- const { id, dimensions, edit, credit, alt, ...maybeThumbnails } =
45
+ const { id, dimensions, edit, copyright, alt, ...maybeThumbnails } =
48
46
  mainImage
49
47
  return pipe(
50
48
  ThumbnailsValidator.validate(maybeThumbnails, ctx),
@@ -64,11 +62,12 @@ const ImageField = (field?: ImageDefinition) =>
64
62
  )
65
63
 
66
64
  const ImageCodec = (field?: ImageDefinition) =>
67
- DefaultOrElse<
68
- Record<string, unknown>,
69
- ImageFieldWithThumbnails,
70
- ImageFieldWithThumbnails
71
- >(defaultImage(field))(ImageField(field))
65
+ withCustomError(
66
+ DefaultOrElse<Record<never, never>, ImageFieldWithThumbnails>(
67
+ defaultImage(field),
68
+ )(ImageField(field)),
69
+ () => "An image field must be an object",
70
+ )
72
71
 
73
72
  export const ImportImage = (field?: ImageDefinition) =>
74
73
  ImportContent("Image", ImageCodec(field))
@@ -10,7 +10,7 @@ export type ImageField = {
10
10
  width?: number
11
11
  height?: number
12
12
  }
13
- credit?: string | null
13
+ copyright?: string | null
14
14
  alt?: string | null
15
15
  }
16
16
  export type ImageFieldWithThumbnails = ImageField & {
@@ -1,13 +1,13 @@
1
1
  import type { OutputOf, TypeOf } from "io-ts"
2
2
  import * as t from "io-ts"
3
- import { withMessage } from "io-ts-types"
4
3
 
5
4
  import { DocumentId } from "../../../../utils/DocumentId"
6
5
  import { DefaultOrElse, String } from "../../../../validators"
6
+ import { withCustomError } from "../../../../validators/function"
7
7
  import { ImportContent } from "../ImportContent"
8
8
 
9
9
  const LinkTypeValidator = t.type({
10
- link_type: withMessage(
10
+ link_type: withCustomError(
11
11
  t.union([t.literal("Web"), t.literal("Document"), t.literal("Media")]),
12
12
  () => "The value must be `Web`, `Document` or `Media`",
13
13
  ),
@@ -1,9 +1,8 @@
1
- import { withMessage } from "io-ts-types"
1
+ import * as t from "io-ts"
2
2
 
3
- import { String } from "../validators"
4
- import { refineType } from "../validators/function"
3
+ import { refineType, withCustomError } from "../validators/function"
5
4
 
6
- export const DocumentId = withMessage(
7
- refineType(String, "DocumentId", (s) => s.length === 16),
5
+ export const DocumentId = withCustomError(
6
+ refineType(t.string, "DocumentId", (s) => s.length === 16),
8
7
  () => "DocumentId must be a 16 character string",
9
8
  )
@@ -1,19 +1,26 @@
1
1
  import { either } from "fp-ts"
2
2
  import { pipe } from "fp-ts/lib/function"
3
3
  import * as t from "io-ts"
4
- import { withMessage } from "io-ts-types"
5
4
 
6
- export const String = withMessage(t.string, () => "The value must be a string")
7
- export const StringOrNull = withMessage(
5
+ import { withCustomError } from "./function"
6
+
7
+ export const String = withCustomError(
8
+ t.string,
9
+ () => "The value must be a string",
10
+ )
11
+ export const StringOrNull = withCustomError(
8
12
  t.union([t.string, t.null]),
9
13
  () => "The value must be a string or null",
10
14
  )
11
- export const Number = withMessage(t.number, () => "The value must be a number")
12
- export const NumberOrNull = withMessage(
15
+ export const Number = withCustomError(
16
+ t.number,
17
+ () => "The value must be a number",
18
+ )
19
+ export const NumberOrNull = withCustomError(
13
20
  t.union([t.number, t.null]),
14
21
  () => "The value must be a number or null",
15
22
  )
16
- export const Boolean = withMessage(
23
+ export const Boolean = withCustomError(
17
24
  t.boolean,
18
25
  () => "The value must be a boolean",
19
26
  )
@@ -46,13 +53,3 @@ export const EmptyArray = new t.Type<never[], never[], unknown>(
46
53
  },
47
54
  () => [],
48
55
  )
49
-
50
- export const AnyObject = withMessage(
51
- t.record(t.string, t.unknown),
52
- () => "The value must be an object",
53
- )
54
-
55
- export const AnyArray = withMessage(
56
- t.array(t.unknown),
57
- () => "The value must be an array",
58
- )
@@ -6,7 +6,7 @@ import { EmptyArray, EmptyObject } from "./BasicTypes"
6
6
  export const DefaultOrElse = <I, A, O = A>(inputValidator: t.Type<I>) => {
7
7
  return (codec: t.Type<A, O>) =>
8
8
  new t.Type<A | null, O | undefined>(
9
- "DefaultOrT",
9
+ "DefaultOrElse",
10
10
  (u: unknown): u is A | null => null === u || codec.is(u),
11
11
  (u: unknown) => {
12
12
  if (isRight(inputValidator.decode(u))) return t.success(null)
@@ -1,9 +1,8 @@
1
1
  import * as t from "io-ts"
2
- import { withMessage } from "io-ts-types"
3
2
 
4
- import { refineType } from "./function"
3
+ import { refineType, withCustomError } from "./function"
5
4
 
6
- export default withMessage(
5
+ export default withCustomError(
7
6
  refineType(t.string, "nonEmptyString", (s) => s.trim().length > 0),
8
7
  () => "The value must be a non-empty string",
9
8
  )
@@ -78,19 +78,19 @@ export function addType<A, O extends object, I, T extends string>(
78
78
  *
79
79
  * @example
80
80
  * expect(
81
- * withFallbackMessage(
82
- * t.type({age: withMessage(t.number, () => 'Invalid child')}),
81
+ * withCustomError(
82
+ * t.type({age: withCustomError(t.number, () => 'Invalid child')}),
83
83
  * () => "Invalid parent"
84
84
  * )
85
85
  * ).toSubsetEqualLeft([{message: "Invalid child"}])
86
86
  * expect(
87
- * withFallbackMessage(
87
+ * withCustomError(
88
88
  * t.type({age: t.number}),
89
89
  * () => "Invalid parent"
90
90
  * )
91
91
  * ).toSubsetEqualLeft([{message: "Invalid parent"}])
92
92
  */
93
- export function withFallbackMessage<C extends t.Any>(
93
+ export function withCustomError<C extends t.Any>(
94
94
  codec: C,
95
95
  message: (i: t.InputOf<C>, c: t.Context) => string,
96
96
  ): C {