From 9c871e00b177590225df27d73398300d4c303048 Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Tue, 30 Jul 2024 12:19:54 -0500 Subject: [PATCH 1/2] Parse all schemas as JSON --- src/commands/ajv.ts | 2 +- src/commands/compile.ts | 2 +- src/commands/migrate.ts | 2 +- src/commands/util.ts | 7 +++---- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/commands/ajv.ts b/src/commands/ajv.ts index d06acb5..d29c2c6 100644 --- a/src/commands/ajv.ts +++ b/src/commands/ajv.ts @@ -49,7 +49,7 @@ export default function (argv: ParsedArgs): AjvCore { if (!args) return const files = util.getFiles(args) files.forEach((file) => { - const schema = util.openFile(file, fileType) + const schema = util.openFile(file, fileType, "json") try { ajv[method](schema) } catch (err) { diff --git a/src/commands/compile.ts b/src/commands/compile.ts index e43a1dc..98fb960 100644 --- a/src/commands/compile.ts +++ b/src/commands/compile.ts @@ -47,7 +47,7 @@ function execute(argv: ParsedArgs): boolean { } function compileSchema(file: string): AnyValidateFunction | undefined { - const sch = openFile(file, `schema ${file}`) + const sch = openFile(file, `schema ${file}`, "json") try { const id = sch?.$id ajv.addSchema(sch, id ? undefined : file) diff --git a/src/commands/migrate.ts b/src/commands/migrate.ts index d07dcf0..27dd07b 100644 --- a/src/commands/migrate.ts +++ b/src/commands/migrate.ts @@ -33,7 +33,7 @@ function execute(argv: ParsedArgs): boolean { return schemaFiles.map(migrateSchema).every((x) => x) function migrateSchema(file: string): boolean { - const sch = openFile(file, `schema ${file}`) + const sch = openFile(file, `schema ${file}`, "json") const migratedSchema: AnySchemaObject = JSON.parse(JSON.stringify(sch)) const spec = (argv.spec || "draft7") as JSONSchemaDraft migrate[spec](migratedSchema) diff --git a/src/commands/util.ts b/src/commands/util.ts index 42b4ed1..6637134 100644 --- a/src/commands/util.ts +++ b/src/commands/util.ts @@ -41,13 +41,12 @@ function decodeFile(contents: string, format: string): any { } } -export function openFile(filename: string, suffix: string): any { +export function openFile(filename: string, suffix: string, format?: string): any { let json = null const file = path.resolve(process.cwd(), filename) try { try { - const format = getFormatFromFileName(filename) - json = decodeFile(fs.readFileSync(file).toString(), format) + json = decodeFile(fs.readFileSync(file).toString(), format ?? getFormatFromFileName(filename)) } catch (e) { json = require(file) } @@ -77,7 +76,7 @@ export function logJSON(mode: string, data: any, ajv?: Ajv): string { } export function compile(ajv: Ajv, schemaFile: string): AnyValidateFunction { - const schema = openFile(schemaFile, "schema") + const schema = openFile(schemaFile, "schema", "json") try { return ajv.compile(schema) } catch (err) { From a6bc7e6c2fd05c13b6077e35c70c7219f00bd762 Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Tue, 30 Jul 2024 12:52:28 -0500 Subject: [PATCH 2/2] Fix ts errors --- src/commands/ajv.ts | 2 +- src/commands/util.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/commands/ajv.ts b/src/commands/ajv.ts index d29c2c6..60241a2 100644 --- a/src/commands/ajv.ts +++ b/src/commands/ajv.ts @@ -84,7 +84,7 @@ export default function (argv: ParsedArgs): AjvCore { try { registerer = require("ts-node").register() - } catch (err) { + } catch (err: any) { /* istanbul ignore next */ if (err.code === "MODULE_NOT_FOUND") { throw new Error( diff --git a/src/commands/util.ts b/src/commands/util.ts index 6637134..56ca8f6 100644 --- a/src/commands/util.ts +++ b/src/commands/util.ts @@ -50,7 +50,7 @@ export function openFile(filename: string, suffix: string, format?: string): any } catch (e) { json = require(file) } - } catch (err) { + } catch (err: any) { const msg: string = err.message console.error(`error: ${msg.replace(" module", " " + suffix)}`) process.exit(2) @@ -79,7 +79,7 @@ export function compile(ajv: Ajv, schemaFile: string): AnyValidateFunction { const schema = openFile(schemaFile, "schema", "json") try { return ajv.compile(schema) - } catch (err) { + } catch (err: any) { console.error(`schema ${schemaFile} is invalid`) console.error(`error: ${err.message}`) process.exit(1)