···
221
221
assert(!v({ $bytes: "x" }));
222
222
});
223
223
224
224
-
test("record", () => {
224
224
+
test("record: requires $type matching the lexicon NSID", () => {
225
225
const v = validator({
226
226
type: "record",
227
227
record: { type: "object", required: ["x"], properties: { x: { type: "integer" } } },
228
228
});
229
229
-
assert(v({ x: 1 }));
230
230
-
assert(!v({}));
229
229
+
assert(v({ $type: "test.x", x: 1 }));
230
230
+
assert(v({ $type: "test.x#main", x: 1 }));
231
231
+
assert(!v({ x: 1 }));
232
232
+
assert(!v({ $type: "test.other", x: 1 }));
233
233
+
assert(!v({ $type: "test.x" }));
234
234
+
assert(!v(null));
235
235
+
assert(!v([]));
231
236
});
232
237
233
238
test("token has no data representation", () => {
···
118
118
* : Def extends { type: "blob" } ? Blob
119
119
* : Def extends { type: "array"; items: infer Items } ? Infer<Items, Catalog, CurrentNsid>[]
120
120
* : Def extends { type: "object" | "params" } ? InferObject<Def, Catalog, CurrentNsid>
121
121
-
* : Def extends { type: "record"; record: infer R } ? InferObject<R, Catalog, CurrentNsid>
121
121
+
* : Def extends { type: "record"; record: infer R } ? InferObject<R, Catalog, CurrentNsid> & { readonly $type: CurrentNsid }
122
122
* : Def extends { type: "ref"; ref: infer R extends string } ? ResolveRef<R, Catalog, CurrentNsid>
123
123
* : Def extends { type: "union"; refs: ReadonlyArray<infer R extends string> } ? ResolveRef<R, Catalog, CurrentNsid>
124
124
* : Def extends { type: "unknown" } ? Record<string, unknown>
···
556
556
}
557
557
558
558
/** @type {Validator} */
559
559
-
const validateRecord = (data, def, opts, path) => validateObject(data, def.record, opts, path);
559
559
+
function validateRecord(data, def, opts, path) {
560
560
+
if (!isPlainObject(data))
561
561
+
return fail(`Expected object, got ${Array.isArray(data) ? "array" : typeof data}`, path);
562
562
+
if (opts.nsid) {
563
563
+
if (typeof data.$type !== "string") return fail(`Record must have "$type"`, path);
564
564
+
if (data.$type !== opts.nsid && data.$type !== `${opts.nsid}#main`)
565
565
+
return fail(`$type "${data.$type}" does not match "${opts.nsid}"`, path);
566
566
+
}
567
567
+
return validateObject(data, def.record, opts, path);
568
568
+
}
560
569
561
570
/** @type {Validator} */
562
571
function validateRef(data, def, opts, path) {