Skip to content

Commit

Permalink
fix(ajv-validator#43): Load all schemas before validating them
Browse files Browse the repository at this point in the history
  • Loading branch information
iainjreid committed Jun 24, 2022
1 parent 58d6f07 commit ff0d874
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/commands/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default cmd

function execute(argv: ParsedArgs): boolean {
const ajv = getAjv(argv)
const schemaFiles = getFiles(argv.s)
const schemaFiles = getFiles(argv.s).map(loadSchema)
if ("o" in argv && schemaFiles.length > 1) return compileMultiExportModule(schemaFiles)
return schemaFiles.map(compileSchemaAndSave).every((x) => x)

Expand All @@ -46,11 +46,23 @@ function execute(argv: ParsedArgs): boolean {
return false
}

function compileSchema(file: string): AnyValidateFunction | undefined {
function loadSchema(file: string): string | never {
const sch = openFile(file, `schema ${file}`)
try {
const id = sch?.$id
ajv.addSchema(sch, id ? undefined : file)
return file
} catch (err) {
console.error(`schema ${file} is invalid`)
console.error(`error: ${(err as Error).message}`)
process.exit(1)
}
}

function compileSchema(file: string): AnyValidateFunction | undefined {
const sch = openFile(file, `schema ${file}`)
try {
const id = sch?.$id
const validate = ajv.getSchema(id || file)
if (argv.o !== true) console.log(`schema ${file} is valid`)
return validate
Expand Down

0 comments on commit ff0d874

Please sign in to comment.