|
| 1 | +import z from 'zod/v4'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Registry for argument options metadata |
| 5 | + */ |
| 6 | +export const argMetadata = z.registry<ArgumentMetadata>(); |
| 7 | + |
| 8 | +/** |
| 9 | + * Metadata that can be used to define field's parsing behavior |
| 10 | + */ |
| 11 | +export type ArgumentMetadata = { |
| 12 | + /** If set, sets this field as deprecated and replaces this field with the set field. */ |
| 13 | + deprecationReplacement?: string; |
| 14 | + /** If set, gets replaced with a different field name (without deprecation) */ |
| 15 | + replacement?: string; |
| 16 | + /** Whether this argument is unsupported. Always throws an error if set to true. */ |
| 17 | + unsupported?: boolean; |
| 18 | + /** Aliases for this argument. */ |
| 19 | + alias?: string[]; |
| 20 | +}; |
| 21 | + |
| 22 | +/** |
| 23 | + * Extract metadata for a field using the custom registry |
| 24 | + */ |
| 25 | +export function getArgumentMetadata( |
| 26 | + schema: z.ZodObject, |
| 27 | + fieldName: string |
| 28 | +): ArgumentMetadata | undefined { |
| 29 | + const fieldSchema = schema.shape[fieldName]; |
| 30 | + if (!fieldSchema) { |
| 31 | + return undefined; |
| 32 | + } |
| 33 | + return argMetadata.get(fieldSchema); |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Maps deprecated arguments to their new counterparts, derived from schema metadata. |
| 38 | + */ |
| 39 | +export function getDeprecatedArgsWithReplacement( |
| 40 | + schema: z.ZodObject |
| 41 | +): Record<string, keyof z.infer<typeof schema>> { |
| 42 | + const deprecated: Record<string, keyof z.infer<typeof schema>> = {}; |
| 43 | + for (const fieldName of Object.keys(schema.shape)) { |
| 44 | + const meta = getArgumentMetadata(schema, fieldName); |
| 45 | + if (meta?.deprecationReplacement) { |
| 46 | + deprecated[fieldName] = meta.deprecationReplacement; |
| 47 | + } |
| 48 | + } |
| 49 | + return deprecated; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Get list of unsupported arguments, derived from schema metadata. |
| 54 | + */ |
| 55 | +export function getUnsupportedArgs(schema: z.ZodObject): string[] { |
| 56 | + const unsupported: string[] = []; |
| 57 | + for (const fieldName of Object.keys(schema.shape)) { |
| 58 | + const meta = getArgumentMetadata(schema, fieldName); |
| 59 | + if (meta?.unsupported) { |
| 60 | + unsupported.push(fieldName); |
| 61 | + } |
| 62 | + } |
| 63 | + return unsupported; |
| 64 | +} |
| 65 | + |
| 66 | +export class InvalidArgumentError extends Error { |
| 67 | + constructor(message: string) { |
| 68 | + super(message); |
| 69 | + this.name = 'InvalidArgumentError'; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +export class UnknownArgumentError extends Error { |
| 74 | + /** The argument that was not parsed. */ |
| 75 | + readonly argument: string; |
| 76 | + constructor(argument: string) { |
| 77 | + super(`Unknown argument: ${argument}`); |
| 78 | + this.name = 'UnknownArgumentError'; |
| 79 | + this.argument = argument; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +export class UnsupportedArgumentError extends Error { |
| 84 | + /** The argument that was not supported. */ |
| 85 | + readonly argument: string; |
| 86 | + constructor(argument: string) { |
| 87 | + super(`Unsupported argument: ${argument}`); |
| 88 | + this.name = 'UnsupportedArgumentError'; |
| 89 | + this.argument = argument; |
| 90 | + } |
| 91 | +} |
0 commit comments