From 697dbe570e6bbd686e5b39b80488e748a68b4d57 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 25 Jun 2025 16:16:04 +0200 Subject: [PATCH] Improve type-safety in readSchemas and warn for legacy schemas --- packages/ts-codegen/src/utils/schemas.ts | 25 ++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/ts-codegen/src/utils/schemas.ts b/packages/ts-codegen/src/utils/schemas.ts index 937cda02..e3066f55 100644 --- a/packages/ts-codegen/src/utils/schemas.ts +++ b/packages/ts-codegen/src/utils/schemas.ts @@ -34,33 +34,37 @@ export const readSchemas = async ({ const fn = clean ? cleanse : (schema: JSONSchema[] | Partial) => schema; - const files = await findSchemaFiles(schemaDir); - const schemas: JSONSchema[] = files.map((file) => - JSON.parse(readFileSync(file, 'utf-8')) + const files = (await findSchemaFiles(schemaDir)).map((path) => + readFileSync(path, 'utf-8') ); - if (schemas.length > 1) { + if (files.length > 1) { // legacy // TODO add console.warn here + console.warn( + 'Found a multiple schema files. This mode will be removed in the next major version. Please migrate the schemas that contain a single .json IDL file (CosmWasm 1.1+).' + ); + + const schemas: JSONSchema[] = files.map((file) => JSON.parse(file)); return { schemas: fn(schemas), }; } - if (schemas.length === 0) { + if (files.length === 0) { throw new Error( 'Error [too few files]: requires one schema file per contract' ); } - if (schemas.length !== 1) { + if (files.length !== 1) { throw new Error( 'Error [too many files]: CosmWasm v1.1 schemas supports one file' ); } - const idlObject: Partial = schemas[0] as Partial; + const idlObject: Partial = JSON.parse(files[0]); const { // contract_name, // contract_version, @@ -75,8 +79,13 @@ export const readSchemas = async ({ if (typeof idl_version !== 'string') { // legacy + // fall back to a single JSON Schema file + console.warn( + 'Found a single schema file with missing idl_version. This mode will be removed in the next major version. Please migrate the schemas that contain a single .json IDL file (CosmWasm 1.1+).' + ); + const schema: JSONSchema = JSON.parse(files[0]); return { - schemas: fn(schemas), + schemas: fn([schema]), }; }