|
5 | 5 | [](https://npmjs.com/package/@qui-cli/validators) |
6 | 6 | [](https://nodejs.org/api/esm.html) |
7 | 7 |
|
| 8 | +## Deprecated in favor of [zod](https://www.npmjs.com/package/zod) |
| 9 | + |
| 10 | +Validators were originally designed to slot into [@inquirer/prompts](https://www.npmjs.com/package/@inquirer/prompts) transparently, with meaningful error messages. |
| 11 | + |
| 12 | +```ts |
| 13 | +import { Validators } from '@qui-cli/validators'; |
| 14 | +import { input } from '@inquirer/prompts'; |
| 15 | + |
| 16 | +const result = await input({ |
| 17 | + message: 'Enter a value', |
| 18 | + validator: Validators.nonEmpty |
| 19 | +}); |
| 20 | +``` |
| 21 | + |
| 22 | +zod handles this nicely, without needing to support an extra package: |
| 23 | + |
| 24 | +```ts |
| 25 | +import z from 'zod'; |
| 26 | +import {input} from '@inquirer/prompts'; |
| 27 | + |
| 28 | +const result = await input({ |
| 29 | + message: 'Enter a value', |
| 30 | + validator (value?: string) => { |
| 31 | + const { success, error: { issues } } = z.string().nonempty().safeParse(value); |
| 32 | + return success || |
| 33 | + issues.map(i => i.message).join(', '); |
| 34 | + } |
| 35 | +}); |
| 36 | +``` |
| 37 | + |
| 38 | +or, less verbosely… |
| 39 | + |
| 40 | +```ts |
| 41 | +import z from 'zod'; |
| 42 | +import { input } from '@inquirer/prompts'; |
| 43 | + |
| 44 | +const result = await input({ |
| 45 | + message: 'Enter a value', |
| 46 | + validator: (value?: string) => |
| 47 | + z.string().nonempty().safeParse(value).success || 'error message' |
| 48 | +}); |
| 49 | +``` |
| 50 | + |
| 51 | +<table> |
| 52 | +<thead> |
| 53 | +<tr><th>@qui-cli/validators</th><th>zod</th></tr> |
| 54 | +<thead> |
| 55 | +<tbody> |
| 56 | +<tr><td> |
| 57 | + |
| 58 | +`Validators.notEmpty()`</td><td>`z.string().nonempty()`</td></tr> |
| 59 | +<tr><td> |
| 60 | + |
| 61 | +`Validators.minLength(m)`</td><td>`z.string().min(m)`</td></tr> |
| 62 | +<tr><td> |
| 63 | + |
| 64 | +`Validators.maxLength(m)`</td><td>`z.string().max(m)`</td></tr> |
| 65 | +<tr><td> |
| 66 | + |
| 67 | +`Validators.lengthBetween(m,n)`</td><td>`z.string().min(m).max(n)`</td></tr> |
| 68 | +<tr><td> |
| 69 | + |
| 70 | +`Validators.match(r)`</td><td>`z.string().regex(r)`</td></tr> |
| 71 | +<tr><td> |
| 72 | + |
| 73 | +`Validators.cron()`</td><td> |
| 74 | + |
| 75 | +```ts |
| 76 | +import cron from 'cron-validate'; |
| 77 | + |
| 78 | +z.stringFormat('cron', (val?: string) => !!val && cron(val).isValid())` |
| 79 | +``` |
| 80 | + |
| 81 | +</td></tr> |
| 82 | +<tr><td> |
| 83 | + |
| 84 | +`Validators.isPath()`</td><td> |
| 85 | + |
| 86 | +```ts |
| 87 | +import path from 'node:path'; |
| 88 | +
|
| 89 | +z.stringFormat( |
| 90 | + 'posix-path', |
| 91 | + (val?: string) => !!val && val == path.posix.normalize(val) |
| 92 | +); |
| 93 | +``` |
| 94 | + |
| 95 | +</td></tr> |
| 96 | +<tr><td> |
| 97 | + |
| 98 | +`Validators.isHostname()`</td><td>`z.string().hostname()`</td></tr> |
| 99 | +</tbody> |
| 100 | +</table> |
| 101 | + |
8 | 102 | ## Install |
9 | 103 |
|
10 | 104 | ```sh |
|
0 commit comments