Skip to content

Commit bbbff4a

Browse files
committedJan 6, 2023
fix!: improve consistency and fidelity of error conditions & messages
Ref: ipld/codec-fixtures#81 This should only be a BREAKING CHANGE for users that are matching error messages, otherwise this should not have a material impact on use.
1 parent d2255e2 commit bbbff4a

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed
 

‎src/util.js

+17-10
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ export function validate (node) {
157157
Data optional Bytes
158158
}
159159
*/
160-
if (!node || typeof node !== 'object' || Array.isArray(node)) {
160+
// @ts-ignore private property for TS
161+
if (!node || typeof node !== 'object' || Array.isArray(node) || node instanceof Uint8Array || (node['/'] && node['/'] === node.bytes)) {
161162
throw new TypeError('Invalid DAG-PB form')
162163
}
163164

@@ -166,38 +167,44 @@ export function validate (node) {
166167
}
167168

168169
if (node.Data !== undefined && !(node.Data instanceof Uint8Array)) {
169-
throw new TypeError('Invalid DAG-PB form (Data must be a Uint8Array)')
170+
throw new TypeError('Invalid DAG-PB form (Data must be bytes)')
170171
}
171172

172173
if (!Array.isArray(node.Links)) {
173-
throw new TypeError('Invalid DAG-PB form (Links must be an array)')
174+
throw new TypeError('Invalid DAG-PB form (Links must be a list)')
174175
}
175176

176177
for (let i = 0; i < node.Links.length; i++) {
177178
const link = node.Links[i]
178-
if (!link || typeof link !== 'object' || Array.isArray(link)) {
179-
throw new TypeError('Invalid DAG-PB form (bad link object)')
179+
// @ts-ignore private property for TS
180+
if (!link || typeof link !== 'object' || Array.isArray(link) || link instanceof Uint8Array || (link['/'] && link['/'] === link.bytes)) {
181+
throw new TypeError('Invalid DAG-PB form (bad link)')
180182
}
181183

182184
if (!hasOnlyProperties(link, pbLinkProperties)) {
183-
throw new TypeError('Invalid DAG-PB form (extraneous properties on link object)')
185+
throw new TypeError('Invalid DAG-PB form (extraneous properties on link)')
184186
}
185187

186-
if (!link.Hash) {
188+
if (link.Hash === undefined) {
187189
throw new TypeError('Invalid DAG-PB form (link must have a Hash)')
188190
}
189191

190192
// @ts-ignore private property for TS
191-
if (link.Hash.asCID !== link.Hash) {
193+
if (link.Hash == null || !link.Hash['/'] || link.Hash['/'] !== link.Hash.bytes) {
192194
throw new TypeError('Invalid DAG-PB form (link Hash must be a CID)')
193195
}
194196

195197
if (link.Name !== undefined && typeof link.Name !== 'string') {
196198
throw new TypeError('Invalid DAG-PB form (link Name must be a string)')
197199
}
198200

199-
if (link.Tsize !== undefined && (typeof link.Tsize !== 'number' || link.Tsize % 1 !== 0)) {
200-
throw new TypeError('Invalid DAG-PB form (link Tsize must be an integer)')
201+
if (link.Tsize !== undefined) {
202+
if (typeof link.Tsize !== 'number' || link.Tsize % 1 !== 0) {
203+
throw new TypeError('Invalid DAG-PB form (link Tsize must be an integer)')
204+
}
205+
if (link.Tsize < 0) {
206+
throw new TypeError('Invalid DAG-PB form (link Tsize cannot be negative)')
207+
}
201208
}
202209

203210
if (i > 0 && linkComparator(link, node.Links[i - 1]) === -1) {

0 commit comments

Comments
 (0)
Please sign in to comment.