https://github.com/codemonument/deno_cliffy_zod_option
A custom type for the cliffy cli framework to allow validating options via custom zod schemas
https://github.com/codemonument/deno_cliffy_zod_option
Last synced: about 1 month ago
JSON representation
A custom type for the cliffy cli framework to allow validating options via custom zod schemas
- Host: GitHub
- URL: https://github.com/codemonument/deno_cliffy_zod_option
- Owner: codemonument
- License: mit
- Created: 2024-03-31T17:24:34.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-22T10:13:11.000Z (over 1 year ago)
- Last Synced: 2025-01-25T15:41:25.674Z (over 1 year ago)
- Language: TypeScript
- Size: 19.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Cliffy Extension: zodType
This package provides a custom options type for the cliffy cli framework.
The new Option type zodType allows to validate a cli option input with zod.
## Usage Examples
```typescript
const command = new Command()
.name('cliffy-zod-option-demo')
//1. register the custom cliffy types with the zodType function
.type('zodString', zodType(z.string()))
.type('zodInt', zodType(z.coerce.number().int()))
.type('zodBaseColor', zodType(z.enum(['red', 'green', 'blue'])))
// 2. Use the custom types in the options
.option('--zodString ', 'A string validated by zod')
.option('--zodInt ', 'An integer validated by zod')
.option(
'--zodBaseColor ',
'A base color value validated by zod enum (red, green or blue)'
)
.option('--requiredZodInt ', 'A required integer validated by zod', {
required: true,
})
.allowEmpty()
.action(options => {
if (Object.keys(options).length === 0) {
command.showHelp();
return;
}
// 3. Options are already validated by zod
console.log('CLI called with options:', options);
// Note: due to the type design of cliffy, the options always contain the 'undefined' type for each option key, parallel to the type returned by zod.
// This can be fixed by a simple if (!options.option) check or by using the zod schema again which was used for the option in the first place.
});
await command.parse(args);
```
## Release new version (for maintainers)
1. Update the version in the `deno.json` file.
2. Update the version in the `src/cli.ts` file.
---
# Changelog
## 1.0.1 - 2024-10-22
- publish to jsr with provenance and upgrade to jsr @cliffy/command@1.0.0-rc.7
## 1.0.0 - 2023-03-31
- intial release, see README.md for usage examples