Suite of AT Protocol TypeScript libraries built on web standards
1import { z } from "zod";
2
3export function toLexUri(str: string, baseUri?: string): string {
4 if (str.split("#").length > 2) {
5 throw new Error("Uri can only have one hash segment");
6 }
7
8 if (str.startsWith("lex:")) {
9 return str;
10 }
11 if (str.startsWith("#")) {
12 if (!baseUri) {
13 throw new Error(`Unable to resolve uri without anchor: ${str}`);
14 }
15 return `${baseUri}${str}`;
16 }
17 return `lex:${str}`;
18}
19
20export function requiredPropertiesRefinement<
21 ObjectType extends {
22 required?: string[];
23 properties?: Record<string, unknown>;
24 },
25>(object: ObjectType, ctx: z.RefinementCtx) {
26 // Required fields check
27 if (object.required === undefined) {
28 return;
29 }
30
31 if (!Array.isArray(object.required)) {
32 ctx.addIssue({
33 code: z.ZodIssueCode.invalid_type,
34 received: typeof object.required,
35 expected: "array",
36 });
37 return;
38 }
39
40 if (object.properties === undefined) {
41 if (object.required.length > 0) {
42 ctx.addIssue({
43 code: z.ZodIssueCode.custom,
44 message: `Required fields defined but no properties defined`,
45 });
46 }
47 return;
48 }
49
50 for (const field of object.required) {
51 if (object.properties[field] === undefined) {
52 ctx.addIssue({
53 code: z.ZodIssueCode.custom,
54 message: `Required field "${field}" not defined`,
55 });
56 }
57 }
58}