Skip to content

Commit

Permalink
fix: Load schema with fetch, default to JSR package
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Aug 27, 2024
1 parent b087c35 commit da8a99e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion bids-validator/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@std/log": "jsr:@std/[email protected]",
"@std/path": "jsr:@std/[email protected]",
"@ajv": "npm:[email protected]",
"@bids/schema": "jsr:@bids/schema@0.0.202408141737",
"@bids/schema": "jsr:@bids/schema@0.11.1-dev.2+aa884b7e",
"@cliffy/table": "jsr:@cliffy/[email protected]",
"@cliffy/command": "jsr:@cliffy/[email protected]",
"@hed/validator": "npm:[email protected]",
Expand Down
45 changes: 23 additions & 22 deletions bids-validator/src/setup/loadSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,35 @@ import { setCustomMetadataFormats } from '../validators/json.ts'
*
* version is ignored when the network cannot be accessed
*/
export async function loadSchema(version = 'latest'): Promise<Schema> {
const versionRegex = /^v\d/
export async function loadSchema(version?: string): Promise<Schema> {
let schemaUrl = version
const bidsSchema = typeof Deno !== 'undefined' ? Deno.env.get('BIDS_SCHEMA') : undefined
if (bidsSchema !== undefined) {
schemaUrl = bidsSchema
} else if (version === 'latest' || versionRegex.test(version)) {
} else if (version?.match(/^(v\d+\.\d+\.\d+|stable|latest)$/)) {
schemaUrl = `https://bids-specification.readthedocs.io/en/${version}/schema.json`
}
let schema: Schema | undefined = undefined
try {
const schemaModule = await import(/* @vite-ignore */ schemaUrl, {
with: { type: 'json' },
})
schema = new Proxy(
schemaModule.default as object,
objectPathHandler,
) as Schema
} catch (error) {
// No network access or other errors
console.error(error)
console.error(
`Warning, could not load schema from ${schemaUrl}, falling back to internal version`,
)
schema = new Proxy(
schemaDefault as object,
objectPathHandler,
) as Schema

let schema: Schema = new Proxy(
schemaDefault as object,
objectPathHandler,
) as Schema

if (schemaUrl !== undefined) {
try {
const jsonResponse = await fetch(schemaUrl)
const jsonData = await jsonResponse.json()
schema = new Proxy(
jsonData as object,
objectPathHandler,
) as Schema
} catch (error) {
// No network access or other errors
console.error(error)
console.error(
`Warning, could not load schema from ${schemaUrl}, falling back to internal version`,
)
}
}
setCustomMetadataFormats(schema)
return schema
Expand Down

0 comments on commit da8a99e

Please sign in to comment.