MCPcopy Create free account
hub / github.com/SchemaStore/schemastore / getSchemaFormat

Function getSchemaFormat

scripts/build-xregistry.js:133–158  ·  view source on GitHub ↗

* Determines the schema format for a local schemastore.org schema by reading * the file directly, or for an external schema by fetching it. * @param {string} schemaUrl - The schema URL. * @returns {Promise } - The schema format (e.g., JsonSchema/draft-07).

(schemaUrl)

Source from the content-addressed store, hash-verified

131 * @returns {Promise<string>} - The schema format (e.g., JsonSchema/draft-07).
132 */
133async function getSchemaFormat(schemaUrl) {
134 try {
135 const parsed = new URL(schemaUrl)
136 if (
137 parsed.hostname === 'www.schemastore.org' ||
138 parsed.hostname === 'json.schemastore.org'
139 ) {
140 // Read directly from the local checkout instead of making a network request
141 const filename = parsed.pathname.replace(/^\//, '')
142 const schemasDir = path.resolve(__dirname, '../src/schemas/json')
143 const localPath = path.resolve(schemasDir, filename)
144 // Guard against path traversal
145 if (!localPath.startsWith(schemasDir + path.sep)) {
146 return 'JsonSchema'
147 }
148 const data = JSON.parse(await fs.readFile(localPath, 'utf-8'))
149 return parseSchemaFormat(data.$schema || '')
150 }
151 const res = await fetch(schemaUrl)
152 /** @type {any} */
153 const data = await res.json()
154 return parseSchemaFormat(data.$schema || '')
155 } catch {
156 return 'JsonSchema'
157 }
158}
159
160/**
161 * Pre-fetches schema formats for all catalog entries in parallel (batched).

Callers 1

prefetchSchemaFormatsFunction · 0.85

Calls 1

parseSchemaFormatFunction · 0.85

Tested by

no test coverage detected