@atcute/lex-cli 2.2.1 → 2.3.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.
@@ -0,0 +1,59 @@
1
+ import * as v from 'valibot';
2
+
3
+ import { isNsid } from '@atcute/lexicons/syntax';
4
+
5
+ /**
6
+ * Validates if a string is a valid NSID pattern (exact or wildcard)
7
+ * - Exact: "com.atproto.repo.getRecord"
8
+ * - Wildcard: "com.atproto.*"
9
+ */
10
+ const isValidLexiconPattern = (pattern: string): boolean => {
11
+ if (pattern.endsWith('.*')) {
12
+ // For wildcards, remove the .* and validate the prefix as an NSID segment
13
+ const prefix = pattern.slice(0, -2);
14
+ // Add a dummy segment to make it a valid NSID for validation
15
+ return isNsid(prefix + '.x');
16
+ }
17
+ return isNsid(pattern);
18
+ };
19
+
20
+ /**
21
+ * Schema for a single lexicon mapping entry
22
+ */
23
+ const lexiconMappingEntry = v.object({
24
+ type: v.picklist(['namespace', 'named']),
25
+ path: v.pipe(v.string(), v.regex(/^\.$|^\.\//, `path must be "." or start with "./"`)),
26
+ });
27
+
28
+ /**
29
+ * Schema for the atcute:lexicons field in package.json
30
+ */
31
+ const atcuteLexiconsField = v.object({
32
+ mappings: v.optional(
33
+ v.record(
34
+ v.pipe(
35
+ v.string(),
36
+ v.check(isValidLexiconPattern, `invalid NSID pattern (must be valid NSID or end with .*)`),
37
+ ),
38
+ lexiconMappingEntry,
39
+ ),
40
+ ),
41
+ });
42
+
43
+ /**
44
+ * Schema for package.json with atcute:lexicons field
45
+ */
46
+ export const packageJsonSchema = v.looseObject({
47
+ 'atcute:lexicons': v.optional(atcuteLexiconsField),
48
+ });
49
+
50
+ export type LexiconMappingEntry = v.InferOutput<typeof lexiconMappingEntry>;
51
+ export type AtcuteLexiconsField = v.InferOutput<typeof atcuteLexiconsField>;
52
+ export type PackageJsonWithLexicons = v.InferOutput<typeof packageJsonSchema>;
53
+
54
+ /**
55
+ * Validates a package.json object against the schema
56
+ */
57
+ export const validatePackageJson = (data: unknown): v.SafeParseResult<typeof packageJsonSchema> => {
58
+ return v.safeParse(packageJsonSchema, data);
59
+ };