Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse all schemas as JSON #242

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/commands/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/commands/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions src/commands/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,16 @@
}
}

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)
}
} catch (err) {
} catch (err: any) {
const msg: string = err.message
console.error(`error: ${msg.replace(" module", " " + suffix)}`)
process.exit(2)
Expand All @@ -59,7 +58,7 @@
return json
}

export function logJSON(mode: string, data: any, ajv?: Ajv): string {

Check warning on line 61 in src/commands/util.ts

View workflow job for this annotation

GitHub Actions / build (10.x)

Argument 'data' should be typed with a non-any type

Check warning on line 61 in src/commands/util.ts

View workflow job for this annotation

GitHub Actions / build (12.x)

Argument 'data' should be typed with a non-any type

Check warning on line 61 in src/commands/util.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

Argument 'data' should be typed with a non-any type
switch (mode) {
case "json":
data = JSON.stringify(data, null, " ")
Expand All @@ -77,10 +76,10 @@
}

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) {
} catch (err: any) {
console.error(`schema ${schemaFile} is invalid`)
console.error(`error: ${err.message}`)
process.exit(1)
Expand Down
Loading