@prismicio/types-internal 2.2.0-alpha.13 → 2.2.0-alpha.14
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.
- package/lib/common/Asset.d.ts +1 -1
- package/lib/common/UUID.js +2 -2
- package/lib/import/converters/fields/nestable/Image.js +1 -1
- package/lib/import/converters/fields/nestable/Link.js +4 -4
- package/lib/import/validators/Document.js +2 -2
- package/lib/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.js +2 -1
- package/lib/import/validators/fields/ImportSlices/SharedSlice/errors.js +1 -1
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.js +2 -2
- package/lib/import/validators/fields/nestable/GeoPoint.js +1 -1
- package/lib/import/validators/fields/nestable/Image/Decoder.js +2 -2
- package/lib/import/validators/fields/nestable/Image/Validator.d.ts +32 -36
- package/lib/import/validators/fields/nestable/Image/Validator.js +8 -7
- package/lib/import/validators/fields/nestable/Image/index.js +11 -9
- package/lib/import/validators/fields/nestable/Image/model.d.ts +1 -1
- package/lib/import/validators/fields/nestable/Link.js +2 -2
- package/lib/utils/DocumentId.d.ts +2 -1
- package/lib/utils/DocumentId.js +3 -3
- package/lib/validators/BasicTypes.d.ts +0 -2
- package/lib/validators/BasicTypes.js +7 -9
- package/lib/validators/DefaultOrElse.js +1 -1
- package/lib/validators/NonEmptyString.js +1 -2
- package/lib/validators/function.d.ts +4 -4
- package/lib/validators/function.js +6 -6
- package/package.json +1 -1
- package/src/common/Asset.ts +1 -1
- package/src/common/UUID.ts +3 -2
- package/src/import/converters/fields/nestable/Image.ts +1 -1
- package/src/import/converters/fields/nestable/Link.ts +2 -2
- package/src/import/validators/Document.ts +3 -3
- package/src/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.ts +4 -2
- package/src/import/validators/fields/ImportSlices/SharedSlice/errors.ts +1 -1
- package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.ts +5 -2
- package/src/import/validators/fields/nestable/GeoPoint.ts +2 -2
- package/src/import/validators/fields/nestable/Image/Decoder.ts +2 -2
- package/src/import/validators/fields/nestable/Image/Validator.ts +8 -5
- package/src/import/validators/fields/nestable/Image/index.ts +20 -21
- package/src/import/validators/fields/nestable/Image/model.ts +1 -1
- package/src/import/validators/fields/nestable/Link.ts +2 -2
- package/src/utils/DocumentId.ts +4 -5
- package/src/validators/BasicTypes.ts +13 -16
- package/src/validators/DefaultOrElse.ts +1 -1
- package/src/validators/NonEmptyString.ts +2 -3
- package/src/validators/function.ts +4 -4
package/lib/common/Asset.d.ts
CHANGED
package/lib/common/UUID.js
CHANGED
|
@@ -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
|
-
|
|
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");
|
|
@@ -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.
|
|
33
|
+
["credits", (imageField === null || imageField === void 0 ? void 0 : imageField.copyright) || image.credits],
|
|
34
34
|
]);
|
|
35
35
|
}
|
|
36
36
|
function convertThumbnails(imageField, assets) {
|
|
@@ -18,21 +18,21 @@ const documentLinkConverter = (field) => ({
|
|
|
18
18
|
__TYPE__: "LinkContent",
|
|
19
19
|
});
|
|
20
20
|
const fileLinkConverter = (field, asset) => {
|
|
21
|
-
var _a
|
|
21
|
+
var _a;
|
|
22
22
|
return ({
|
|
23
23
|
value: {
|
|
24
24
|
id: field.id,
|
|
25
25
|
url: asset.url,
|
|
26
26
|
name: (_a = asset.filename) !== null && _a !== void 0 ? _a : "unknown.jpg",
|
|
27
27
|
kind: asset.kind,
|
|
28
|
-
size:
|
|
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
|
|
35
|
+
var _a, _b, _c;
|
|
36
36
|
return ({
|
|
37
37
|
value: {
|
|
38
38
|
id: field.id,
|
|
@@ -41,7 +41,7 @@ const imageLinkConverter = (field, asset) => {
|
|
|
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:
|
|
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,
|
|
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
|
|
@@ -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:
|
|
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
|
|
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
|
-
|
|
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>[]),
|
|
@@ -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.
|
|
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
|
|
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
|
-
...(
|
|
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.
|
|
3
|
-
id:
|
|
4
|
-
}
|
|
5
|
-
dimensions
|
|
6
|
-
width
|
|
7
|
-
height
|
|
8
|
-
}
|
|
9
|
-
edit
|
|
10
|
-
x
|
|
11
|
-
y
|
|
12
|
-
zoom
|
|
13
|
-
background
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
alt
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
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:
|
|
13
|
+
dimensions: (0, function_1.withCustomError)(t.partial({
|
|
13
14
|
width: BasicTypes_1.Number,
|
|
14
15
|
height: BasicTypes_1.Number,
|
|
15
|
-
})),
|
|
16
|
-
edit:
|
|
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
|
-
|
|
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
|
|
15
|
-
|
|
16
|
-
|
|
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
|
|
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,
|
|
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);
|
|
@@ -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,
|
|
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
|
-
|
|
1
|
+
import * as t from "io-ts";
|
|
2
|
+
export declare const DocumentId: t.Type<string, string, unknown>;
|
package/lib/utils/DocumentId.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DocumentId = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
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,
|
|
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.
|
|
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
|
|
9
|
-
exports.String = (0,
|
|
10
|
-
exports.StringOrNull = (0,
|
|
11
|
-
exports.Number = (0,
|
|
12
|
-
exports.NumberOrNull = (0,
|
|
13
|
-
exports.Boolean = (0,
|
|
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("
|
|
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,
|
|
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
|
-
*
|
|
23
|
-
* t.type({age:
|
|
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
|
-
*
|
|
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
|
|
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.
|
|
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
|
-
*
|
|
70
|
-
* t.type({age:
|
|
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
|
-
*
|
|
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
|
|
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.
|
|
100
|
+
exports.withCustomError = withCustomError;
|
package/package.json
CHANGED
package/src/common/Asset.ts
CHANGED
package/src/common/UUID.ts
CHANGED
|
@@ -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 =
|
|
10
|
+
export const UUID = withCustomError(
|
|
10
11
|
t.brand(
|
|
11
12
|
t.string,
|
|
12
13
|
(s): s is t.Branded<string, UUIDBrand> => validateUUID(s),
|
|
@@ -31,7 +31,7 @@ const fileLinkConverter = (field: MediaLink, asset: Asset): LinkContent => ({
|
|
|
31
31
|
url: asset.url,
|
|
32
32
|
name: asset.filename ?? "unknown.jpg",
|
|
33
33
|
kind: asset.kind,
|
|
34
|
-
size: asset.size
|
|
34
|
+
size: asset.size ? String(asset.size) : "0",
|
|
35
35
|
__TYPE__: "FileLink",
|
|
36
36
|
},
|
|
37
37
|
__TYPE__: "LinkContent",
|
|
@@ -45,7 +45,7 @@ const imageLinkConverter = (field: MediaLink, asset: Asset): LinkContent => ({
|
|
|
45
45
|
width: String(asset.width ?? 1),
|
|
46
46
|
name: asset.filename ?? "unknown.jpg",
|
|
47
47
|
kind: asset.kind,
|
|
48
|
-
size: asset.size
|
|
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 =
|
|
14
|
-
t.
|
|
13
|
+
const rawImportDocument = withCustomError(
|
|
14
|
+
t.UnknownRecord,
|
|
15
15
|
() => "document is not an object",
|
|
16
16
|
)
|
|
17
17
|
|
|
@@ -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 {
|
|
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:
|
|
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 {
|
|
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
|
-
|
|
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
|
|
@@ -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 {
|
|
5
|
+
import { withCustomError } from "../../../../validators/function"
|
|
6
6
|
import { ImportContent } from "../ImportContent"
|
|
7
7
|
|
|
8
|
-
const GeooPoint =
|
|
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
|
|
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
|
-
...(
|
|
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 =
|
|
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:
|
|
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:
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
|
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,
|
|
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
|
-
|
|
68
|
-
Record<
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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))
|
|
@@ -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:
|
|
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
|
),
|
package/src/utils/DocumentId.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as t from "io-ts"
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import { refineType } from "../validators/function"
|
|
3
|
+
import { refineType, withCustomError } from "../validators/function"
|
|
5
4
|
|
|
6
|
-
export const DocumentId =
|
|
7
|
-
refineType(
|
|
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
|
-
|
|
7
|
-
|
|
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 =
|
|
12
|
-
|
|
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 =
|
|
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
|
-
"
|
|
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
|
|
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
|
-
*
|
|
82
|
-
* t.type({age:
|
|
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
|
-
*
|
|
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
|
|
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 {
|