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

fix(#43): Load all schemas before validating them #214

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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"prettier": "^2.2.1",
"rimraf": "^3.0.2",
"ts-node": "^9.1.0",
"typescript": "^4.1.2"
"typescript": "4.1.2"
},
"peerDependencies": {
"ts-node": ">=9.0.0"
Expand Down
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}`)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line could be removed if the schema name were set to the filename by default, and ignoring the IDs, would that be helpful to add to this PR?

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