-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
87 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Autocomplete Zod Validator | ||
|
||
A Zod Validator for the Fig Spec format. This can be used to ensure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "zod", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "tsc" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"zod": "^3.22.4" | ||
}, | ||
"devDependencies": { | ||
"@tsconfig/recommended": "^1.0.3", | ||
"@withfig/autocomplete-types": "workspace:^", | ||
"typescript": "^5.3.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as z from "zod"; | ||
|
||
type NotOptional<T> = { | ||
[P in keyof T]-?: T[P] | undefined; | ||
}; | ||
|
||
function singleOrArrayValidator<T>(t: z.ZodType<T>): z.ZodType<Fig.SingleOrArray<T>> { | ||
return z.union([t, z.array(t)]); | ||
} | ||
|
||
export const baseSuggestionSchema: z.ZodType<Fig.BaseSuggestion> = z.object({ | ||
displayName: z.string().optional(), | ||
insertValue: z.string().optional(), | ||
replaceValue: z.string().optional(), | ||
description: z.string().optional(), | ||
icon: z.string().optional(), | ||
isDangerous: z.boolean().optional(), | ||
priority: z.number().optional(), | ||
hidden: z.boolean().optional(), | ||
deprecated: z.boolean().optional(), | ||
previewComponent: z.string().optional(), | ||
_internal: z.record(z.unknown()), | ||
}) satisfies z.ZodType<Fig.BaseSuggestion>; | ||
|
||
baseSubcommandSchema. | ||
|
||
export const baseSubcommandSchema: z.ZodType<Fig.Subcommand> = baseSuggestionSchema.and( | ||
z.object({ | ||
name: singleOrArrayValidator(z.string()), | ||
requiresSubcommand: z.boolean().optional(), | ||
filterStrategy: z.enum(["fuzzy", "prefix", "default"]), | ||
cache: z.boolean().optional(), | ||
}) | ||
) satisfies z.ZodType<NotOptional<Fig.Subcommand>>; | ||
|
||
export const optionSchema: z.ZodType<Fig.Option> = z.object({ | ||
name: singleOrArrayValidator(z.string()), | ||
isPersistent: z.boolean().optional(), | ||
isRequired: z.boolean().optional(), | ||
requiresEquals: z.boolean().optional(), | ||
requiresSeparator: z.union([z.boolean(), z.string()]).optional(), | ||
isRepeatable: z.union([z.boolean(), z.number()]).optional(), | ||
exclusiveOn: z.array(z.string()).optional(), | ||
dependsOn: z.array(z.string()).optional(), | ||
}) satisfies z.ZodType<Fig.Option>; | ||
|
||
export const argSchema: z.ZodType<Fig.Arg> = z.object({ | ||
name: z.string().optional(), | ||
}) satisfies z.ZodType<Fig.Arg>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "@tsconfig/recommended", | ||
"compilerOptions": { | ||
"lib": ["ESNext", "DOM"], | ||
"types": ["@withfig/autocomplete-types"], | ||
"outDir": "dist" | ||
}, | ||
"exclude": ["node_modules/", "dist/", "test/"], | ||
"include": ["./"] | ||
} |