@prismicio/types-internal 3.8.0 → 3.9.0-alpha.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "3.8.0",
3
+ "version": "3.9.0-alpha.0",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -58,6 +58,132 @@ const MasksArrayString = new t.Type<ReadonlyArray<string>, object, unknown>(
58
58
  (res) => res,
59
59
  )
60
60
 
61
+ const CustomTypeLevel2Fields = new t.Type<readonly string[], readonly string[]>(
62
+ "CustomTypeLevel2Fields",
63
+ (u: unknown): u is readonly string[] =>
64
+ Array.isArray(u) && u.every((item) => typeof item === "string"),
65
+ (u: unknown, context: t.Context) =>
66
+ either.chain(t.array(t.string).validate(u, context), (fields) => {
67
+ // duplicates not allowed
68
+ const filtered = new Set(fields)
69
+ return filtered.size === fields.length
70
+ ? t.success(fields)
71
+ : t.failure(u, context, "Fields have duplicates.")
72
+ }),
73
+ (a) => a,
74
+ )
75
+
76
+ const CustomTypeLevel2 = t.union([
77
+ t.string,
78
+ t.exact(
79
+ t.intersection([
80
+ t.type({
81
+ id: t.string,
82
+ }),
83
+ t.partial({
84
+ fields: CustomTypeLevel2Fields,
85
+ }),
86
+ ]),
87
+ ),
88
+ ])
89
+
90
+ const CustomTypesLevel2 = new t.Type<
91
+ readonly t.TypeOf<typeof CustomTypeLevel2>[],
92
+ readonly t.OutputOf<typeof CustomTypeLevel2>[]
93
+ >(
94
+ "CustomTypesLevel2",
95
+ (u: unknown): u is readonly t.TypeOf<typeof CustomTypeLevel2>[] =>
96
+ Array.isArray(u) && u.every((item) => CustomTypeLevel2.is(item)),
97
+ (u: unknown, context: t.Context) =>
98
+ either.chain(t.array(CustomTypeLevel2).validate(u, context), (cts) => {
99
+ // duplicates not allowed
100
+ const filtered = new Set(
101
+ cts.map((ct) => (typeof ct === "string" ? ct : ct.id)),
102
+ )
103
+ return filtered.size === cts.length
104
+ ? t.success(cts)
105
+ : t.failure(u, context, "Custom types have duplicates.")
106
+ }),
107
+ (a) => a,
108
+ )
109
+
110
+ const CustomTypeLevel1Field = t.union([
111
+ t.string,
112
+ t.strict({
113
+ id: t.string,
114
+ customtypes: CustomTypesLevel2,
115
+ }),
116
+ ])
117
+
118
+ const CustomTypeLevel1Fields = new t.Type<
119
+ readonly t.TypeOf<typeof CustomTypeLevel1Field>[],
120
+ readonly t.OutputOf<typeof CustomTypeLevel1Field>[]
121
+ >(
122
+ "CustomTypeLevel1Fields",
123
+ (u: unknown): u is readonly t.TypeOf<typeof CustomTypeLevel1Field>[] =>
124
+ Array.isArray(u) && u.every((item) => CustomTypeLevel1Field.is(item)),
125
+ (u: unknown, context: t.Context) =>
126
+ either.chain(
127
+ t.array(CustomTypeLevel1Field).validate(u, context),
128
+ (fields) => {
129
+ // duplicates not allowed
130
+ const filtered = new Set(
131
+ fields.map((field) => (typeof field === "string" ? field : field.id)),
132
+ )
133
+ return filtered.size === fields.length
134
+ ? t.success(fields)
135
+ : t.failure(u, context, "Fields have duplicates.")
136
+ },
137
+ ),
138
+ (a) => a,
139
+ )
140
+
141
+ const CustomTypeLevel1 = t.union([
142
+ t.string,
143
+ t.exact(
144
+ t.intersection([
145
+ t.type({
146
+ id: t.string,
147
+ }),
148
+ t.partial({
149
+ fields: CustomTypeLevel1Fields,
150
+ }),
151
+ ]),
152
+ ),
153
+ ])
154
+
155
+ const CustomTypes = new t.Type<
156
+ readonly t.TypeOf<typeof CustomTypeLevel1>[],
157
+ readonly t.OutputOf<typeof CustomTypeLevel1>[]
158
+ >(
159
+ "CustomTypes",
160
+ (u: unknown): u is readonly t.TypeOf<typeof CustomTypeLevel1>[] =>
161
+ Array.isArray(u) && u.every((item) => CustomTypeLevel1.is(item)),
162
+ (u: unknown, context: t.Context) =>
163
+ either.chain(t.array(CustomTypeLevel1).validate(u, context), (cts) => {
164
+ // if a ct appears more than once as a string, we allow it (legacy)
165
+ // if a ct appears once as a string and then again via object (or vice versa), we don't allow it
166
+ // if a ct appears more than once as an object, we don't allow it
167
+ const strings = new Set<string>()
168
+ const objects = new Set<string>()
169
+ for (const ct of cts) {
170
+ let failed = false
171
+ if (typeof ct === "string") {
172
+ failed = objects.has(ct)
173
+ strings.add(ct)
174
+ } else {
175
+ const { id } = ct
176
+ failed = strings.has(id) || objects.has(id)
177
+ objects.add(id)
178
+ }
179
+ if (failed)
180
+ return t.failure(u, context, "Custom types have duplicates.")
181
+ }
182
+ return t.success(cts)
183
+ }),
184
+ (a) => a,
185
+ )
186
+
61
187
  export const LinkFieldType = "Link"
62
188
 
63
189
  export const LinkConfig = t.exact(
@@ -74,8 +200,8 @@ export const LinkConfig = t.exact(
74
200
  ]),
75
201
  null,
76
202
  ),
77
- customtypes: t.readonlyArray(t.string), // `customtypes` and `masks` are alternatives
78
- masks: MasksArrayString,
203
+ customtypes: CustomTypes,
204
+ masks: MasksArrayString, // legacy definition of the custom types allowed
79
205
  tags: MasksArrayString,
80
206
  allowTargetBlank: t.boolean,
81
207
  allowText: t.boolean,