|
| 1 | +/** |
| 2 | + * Standard Schema utilities for user-provided schemas. |
| 3 | + * Supports Zod v4, Valibot, ArkType, and other Standard Schema implementations. |
| 4 | + * @see https://standardschema.dev |
| 5 | + */ |
| 6 | + |
| 7 | +/* eslint-disable @typescript-eslint/no-namespace */ |
| 8 | + |
| 9 | +import type { JsonSchemaType, jsonSchemaValidator } from '../validators/types.js'; |
| 10 | + |
| 11 | +// Standard Schema interfaces (from https://standardschema.dev) |
| 12 | + |
| 13 | +export interface StandardTypedV1<Input = unknown, Output = Input> { |
| 14 | + readonly '~standard': StandardTypedV1.Props<Input, Output>; |
| 15 | +} |
| 16 | + |
| 17 | +export namespace StandardTypedV1 { |
| 18 | + export interface Props<Input = unknown, Output = Input> { |
| 19 | + readonly version: 1; |
| 20 | + readonly vendor: string; |
| 21 | + readonly types?: Types<Input, Output> | undefined; |
| 22 | + } |
| 23 | + |
| 24 | + export interface Types<Input = unknown, Output = Input> { |
| 25 | + readonly input: Input; |
| 26 | + readonly output: Output; |
| 27 | + } |
| 28 | + |
| 29 | + export type InferInput<Schema extends StandardTypedV1> = NonNullable<Schema['~standard']['types']>['input']; |
| 30 | + export type InferOutput<Schema extends StandardTypedV1> = NonNullable<Schema['~standard']['types']>['output']; |
| 31 | +} |
| 32 | + |
| 33 | +export interface StandardSchemaV1<Input = unknown, Output = Input> { |
| 34 | + readonly '~standard': StandardSchemaV1.Props<Input, Output>; |
| 35 | +} |
| 36 | + |
| 37 | +export namespace StandardSchemaV1 { |
| 38 | + export interface Props<Input = unknown, Output = Input> extends StandardTypedV1.Props<Input, Output> { |
| 39 | + readonly validate: (value: unknown, options?: Options | undefined) => Result<Output> | Promise<Result<Output>>; |
| 40 | + } |
| 41 | + |
| 42 | + export interface Options { |
| 43 | + readonly libraryOptions?: Record<string, unknown> | undefined; |
| 44 | + } |
| 45 | + |
| 46 | + export type Result<Output> = SuccessResult<Output> | FailureResult; |
| 47 | + |
| 48 | + export interface SuccessResult<Output> { |
| 49 | + readonly value: Output; |
| 50 | + readonly issues?: undefined; |
| 51 | + } |
| 52 | + |
| 53 | + export interface FailureResult { |
| 54 | + readonly issues: ReadonlyArray<Issue>; |
| 55 | + } |
| 56 | + |
| 57 | + export interface Issue { |
| 58 | + readonly message: string; |
| 59 | + readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined; |
| 60 | + } |
| 61 | + |
| 62 | + export interface PathSegment { |
| 63 | + readonly key: PropertyKey; |
| 64 | + } |
| 65 | + |
| 66 | + export type InferInput<Schema extends StandardTypedV1> = StandardTypedV1.InferInput<Schema>; |
| 67 | + export type InferOutput<Schema extends StandardTypedV1> = StandardTypedV1.InferOutput<Schema>; |
| 68 | +} |
| 69 | + |
| 70 | +export interface StandardJSONSchemaV1<Input = unknown, Output = Input> { |
| 71 | + readonly '~standard': StandardJSONSchemaV1.Props<Input, Output>; |
| 72 | +} |
| 73 | + |
| 74 | +export namespace StandardJSONSchemaV1 { |
| 75 | + export interface Props<Input = unknown, Output = Input> extends StandardTypedV1.Props<Input, Output> { |
| 76 | + readonly jsonSchema: Converter; |
| 77 | + } |
| 78 | + |
| 79 | + export interface Converter { |
| 80 | + readonly input: (options: Options) => Record<string, unknown>; |
| 81 | + readonly output: (options: Options) => Record<string, unknown>; |
| 82 | + } |
| 83 | + |
| 84 | + export type Target = 'draft-2020-12' | 'draft-07' | 'openapi-3.0' | (object & string); |
| 85 | + |
| 86 | + export interface Options { |
| 87 | + readonly target: Target; |
| 88 | + readonly libraryOptions?: Record<string, unknown> | undefined; |
| 89 | + } |
| 90 | + |
| 91 | + export type InferInput<Schema extends StandardTypedV1> = StandardTypedV1.InferInput<Schema>; |
| 92 | + export type InferOutput<Schema extends StandardTypedV1> = StandardTypedV1.InferOutput<Schema>; |
| 93 | +} |
| 94 | + |
| 95 | +/** Combined interface for schemas with both validation and JSON Schema conversion (e.g., Zod v4). */ |
| 96 | +export interface StandardSchemaWithJSON<Input = unknown, Output = Input> { |
| 97 | + readonly '~standard': StandardSchemaV1.Props<Input, Output> & StandardJSONSchemaV1.Props<Input, Output>; |
| 98 | +} |
| 99 | + |
| 100 | +// Type guards |
| 101 | + |
| 102 | +export function isStandardJSONSchema(schema: unknown): schema is StandardJSONSchemaV1 { |
| 103 | + if (schema == null) return false; |
| 104 | + const schemaType = typeof schema; |
| 105 | + if (schemaType !== 'object' && schemaType !== 'function') return false; |
| 106 | + if (!('~standard' in (schema as object))) return false; |
| 107 | + const std = (schema as StandardJSONSchemaV1)['~standard']; |
| 108 | + return typeof std?.jsonSchema?.input === 'function' && typeof std?.jsonSchema?.output === 'function'; |
| 109 | +} |
| 110 | + |
| 111 | +export function isStandardSchema(schema: unknown): schema is StandardSchemaV1 { |
| 112 | + if (schema == null) return false; |
| 113 | + const schemaType = typeof schema; |
| 114 | + if (schemaType !== 'object' && schemaType !== 'function') return false; |
| 115 | + if (!('~standard' in (schema as object))) return false; |
| 116 | + const std = (schema as StandardSchemaV1)['~standard']; |
| 117 | + return typeof std?.validate === 'function'; |
| 118 | +} |
| 119 | + |
| 120 | +export function isStandardSchemaWithJSON(schema: unknown): schema is StandardSchemaWithJSON { |
| 121 | + return isStandardJSONSchema(schema) && isStandardSchema(schema); |
| 122 | +} |
| 123 | + |
| 124 | +// JSON Schema conversion |
| 125 | + |
| 126 | +export function standardSchemaToJsonSchema(schema: StandardJSONSchemaV1, io: 'input' | 'output' = 'input'): Record<string, unknown> { |
| 127 | + return schema['~standard'].jsonSchema[io]({ target: 'draft-2020-12' }); |
| 128 | +} |
| 129 | + |
| 130 | +// Validation |
| 131 | + |
| 132 | +export type StandardSchemaValidationResult<T> = { success: true; data: T } | { success: false; error: string }; |
| 133 | + |
| 134 | +export async function validateStandardSchema<T extends StandardJSONSchemaV1>( |
| 135 | + schema: T, |
| 136 | + data: unknown, |
| 137 | + jsonSchemaValidatorInstance?: jsonSchemaValidator |
| 138 | +): Promise<StandardSchemaValidationResult<StandardJSONSchemaV1.InferOutput<T>>> { |
| 139 | + // Use native validation if available |
| 140 | + if (isStandardSchema(schema)) { |
| 141 | + const result = await schema['~standard'].validate(data); |
| 142 | + if (result.issues && result.issues.length > 0) { |
| 143 | + const errorMessage = result.issues.map((i: StandardSchemaV1.Issue) => i.message).join(', '); |
| 144 | + return { success: false, error: errorMessage }; |
| 145 | + } |
| 146 | + return { success: true, data: (result as StandardSchemaV1.SuccessResult<unknown>).value as StandardJSONSchemaV1.InferOutput<T> }; |
| 147 | + } |
| 148 | + |
| 149 | + // Fall back to JSON Schema validation |
| 150 | + if (jsonSchemaValidatorInstance) { |
| 151 | + const jsonSchema = standardSchemaToJsonSchema(schema, 'input'); |
| 152 | + const validator = jsonSchemaValidatorInstance.getValidator<StandardJSONSchemaV1.InferOutput<T>>(jsonSchema as JsonSchemaType); |
| 153 | + const validationResult = validator(data); |
| 154 | + |
| 155 | + if (validationResult.valid) { |
| 156 | + return { success: true, data: validationResult.data }; |
| 157 | + } |
| 158 | + return { success: false, error: validationResult.errorMessage ?? 'Validation failed' }; |
| 159 | + } |
| 160 | + |
| 161 | + // No validation - trust the data |
| 162 | + return { success: true, data: data as StandardJSONSchemaV1.InferOutput<T> }; |
| 163 | +} |
| 164 | + |
| 165 | +// Prompt argument extraction |
| 166 | + |
| 167 | +export function promptArgumentsFromStandardSchema( |
| 168 | + schema: StandardJSONSchemaV1 |
| 169 | +): Array<{ name: string; description?: string; required: boolean }> { |
| 170 | + const jsonSchema = standardSchemaToJsonSchema(schema, 'input'); |
| 171 | + const properties = (jsonSchema.properties as Record<string, { description?: string }>) || {}; |
| 172 | + const required = (jsonSchema.required as string[]) || []; |
| 173 | + |
| 174 | + return Object.entries(properties).map(([name, prop]) => ({ |
| 175 | + name, |
| 176 | + description: prop?.description, |
| 177 | + required: required.includes(name) |
| 178 | + })); |
| 179 | +} |
0 commit comments