import { schemaParser, TSchemaValidationError } from './parser.js' import { ProcedureRegistrationError } from '../errors.js' import { TJSONSchema } from './types.js' /** * This function is used to compute the JSON schema and validation functions * for a given schema. * * @param name The name of the procedure * @param schema Procedure schema */ export function computeSchema( name: string, schema?: { args?: TArgsSchemaType data?: TDataSchemaType }, ): { jsonSchema: { args?: TJSONSchema data?: TJSONSchema } validations: { args?: (args?: any) => { errors?: TSchemaValidationError[] } } } { const jsonSchema: { args?: TJSONSchema; data?: TJSONSchema } = { args: undefined, data: undefined, } const validations: { args?: (args?: any) => { errors?: TSchemaValidationError[] } } = {} if (schema) { const { jsonSchema: { args, data }, validation, } = schemaParser(schema, (errors) => { throw new ProcedureRegistrationError( name, `Error parsing schema for ${name} - ${Object.entries(errors) .map(([key, error]) => `${key}: ${error}`) .join(', ')}`, ) }) jsonSchema.args = args jsonSchema.data = data validations.args = validation.args } return { jsonSchema, validations } }