Skip to content
Merged
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
33 changes: 33 additions & 0 deletions npm/cjs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,36 @@ test('loads schema from nested path', () => {
assert.strictEqual(typeof schema, 'object');
assert.strictEqual(schema.$schema, 'https://json-schema.org/draft/2020-12/schema');
});

test('lazy loads schema via schemas object', () => {
const { schemas } = require('..');
const schema = schemas['2020-12'].misc['schema-like'];
assert.strictEqual(typeof schema, 'object');
assert.strictEqual(schema.title, 'JSON Schema Document');
});

test('caches loaded schemas', () => {
const { schemas } = require('..');
const schema1 = schemas['2020-12'].misc['schema-like'];
const schema2 = schemas['2020-12'].misc['schema-like'];
assert.strictEqual(schema1, schema2);
});

test('handles deeply nested paths', () => {
const { schemas } = require('..');
const schema = schemas['2020-12'].w3c.xmlschema['2001']['hex-binary'];
assert.strictEqual(typeof schema, 'object');
assert.strictEqual(schema.$schema, 'https://json-schema.org/draft/2020-12/schema');
});

test('returns undefined for non-existent directories', () => {
const { schemas } = require('..');
const result = schemas['2020-12'].nonexistent;
assert.strictEqual(result, undefined);
});

test('returns undefined for non-existent files', () => {
const { schemas } = require('..');
const result = schemas['2020-12'].misc['nonexistent-file'];
assert.strictEqual(result, undefined);
});
30 changes: 29 additions & 1 deletion npm/esm.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'node:test';
import assert from 'node:assert';
import getSchema from '@sourcemeta/std';
import getSchema, { schemas } from '@sourcemeta/std';

test('loads a valid schema', () => {
const schema = getSchema('2020-12/misc/schema-like');
Expand Down Expand Up @@ -36,3 +36,31 @@ test('loads schema from nested path', () => {
assert.strictEqual(typeof schema, 'object');
assert.strictEqual(schema.$schema, 'https://json-schema.org/draft/2020-12/schema');
});

test('lazy loads schema via schemas object', () => {
const schema = schemas['2020-12'].misc['schema-like'];
assert.strictEqual(typeof schema, 'object');
assert.strictEqual(schema.title, 'JSON Schema Document');
});

test('caches loaded schemas', () => {
const schema1 = schemas['2020-12'].misc['schema-like'];
const schema2 = schemas['2020-12'].misc['schema-like'];
assert.strictEqual(schema1, schema2);
});

test('handles deeply nested paths', () => {
const schema = schemas['2020-12'].w3c.xmlschema['2001']['hex-binary'];
assert.strictEqual(typeof schema, 'object');
assert.strictEqual(schema.$schema, 'https://json-schema.org/draft/2020-12/schema');
});

test('returns undefined for non-existent directories', () => {
const result = schemas['2020-12'].nonexistent;
assert.strictEqual(result, undefined);
});

test('returns undefined for non-existent files', () => {
const result = schemas['2020-12'].misc['nonexistent-file'];
assert.strictEqual(result, undefined);
});
42 changes: 42 additions & 0 deletions npm/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,46 @@ function getSchema(schemaPath) {
}
}

const cache = new Map();

function createSchemaProxy(currentPath = '') {
return new Proxy({}, {
get(target, property) {
if (typeof property !== 'string') {
return undefined;
}

const newPath = currentPath ? `${currentPath}/${property}` : property;

if (cache.has(newPath)) {
return cache.get(newPath);
}

const schemaBasePath = path.join(__dirname, '..', 'schemas');
const fullPath = path.join(schemaBasePath, newPath);

const jsonPath = `${fullPath}.json`;
if (fs.existsSync(jsonPath)) {
try {
const content = fs.readFileSync(jsonPath, 'utf8');
const schema = JSON.parse(content);
cache.set(newPath, schema);
return schema;
} catch (error) {
return undefined;
}
}

if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
const proxy = createSchemaProxy(newPath);
cache.set(newPath, proxy);
return proxy;
}

return undefined;
}
});
}

module.exports = getSchema;
module.exports.schemas = createSchemaProxy();
27 changes: 4 additions & 23 deletions npm/main.mjs
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { createRequire } from 'node:module';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

function getSchema(schemaPath) {
if (!schemaPath || typeof schemaPath !== 'string') {
return null;
}

if (schemaPath.includes('..')) {
return null;
}

const absolutePath = path.join(__dirname, '..', 'schemas', `${schemaPath}.json`);

try {
const content = fs.readFileSync(absolutePath, 'utf8');
return JSON.parse(content);
} catch (error) {
return null;
}
}
const require = createRequire(import.meta.url);
const getSchema = require('./main.js');

export const schemas = getSchema.schemas;
export default getSchema;