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
9 changes: 9 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*
!LICENSE
!README.markdown
!schemas
!schemas/**
!npm
!npm/**
npm/**/*.test.js
npm/**/*.test.mjs
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ UNZIP ?= unzip
GZIP ?= gzip
MKDIRP ?= mkdir -p
RMRF ?= rm -rf
NODE ?= node
NPM ?= npm

include generated.mk

Expand Down Expand Up @@ -37,6 +39,8 @@ lint: common
.PHONY: test
test:
$(JSONSCHEMA) test ./test
$(NODE) npm/cjs.test.js
$(NODE) npm/esm.test.mjs

# TODO: Add a `jsonschema pkg` command instead
.PHONY: dist
Expand All @@ -51,3 +55,6 @@ dist:
$(TAR) -rf $@/sourcemeta-std-v$(VERSION).tar LICENSE
$(GZIP) $@/sourcemeta-std-v$(VERSION).tar
$(TAR) -tzf $@/sourcemeta-std-v$(VERSION).tar.gz
$(MKDIRP) $@/npm
$(NPM) version --no-git-tag-version --allow-same-version "$(VERSION)"
$(NPM) pack --pack-destination $@/npm
38 changes: 38 additions & 0 deletions npm/cjs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const test = require('node:test');
const assert = require('node:assert');
const getSchema = require('..');

test('loads a valid schema', () => {
const schema = getSchema('2020-12/misc/schema-like');
assert.strictEqual(typeof schema, 'object');
assert.strictEqual(schema.$schema, 'https://json-schema.org/draft/2020-12/schema');
assert.strictEqual(schema.title, 'JSON Schema Document');
});

test('returns null for non-existent schema', () => {
const schema = getSchema('nonexistent/schema/path');
assert.strictEqual(schema, null);
});

test('returns null for invalid input types', () => {
assert.strictEqual(getSchema(null), null);
assert.strictEqual(getSchema(undefined), null);
assert.strictEqual(getSchema(123), null);
assert.strictEqual(getSchema({}), null);
});

test('blocks directory traversal attempts', () => {
const schema = getSchema('../package');
assert.strictEqual(schema, null);
});

test('blocks directory traversal with multiple levels', () => {
const schema = getSchema('../../etc/passwd');
assert.strictEqual(schema, null);
});

test('loads schema from nested path', () => {
const schema = getSchema('2020-12/w3c/xmlschema/2001/hex-binary');
assert.strictEqual(typeof schema, 'object');
assert.strictEqual(schema.$schema, 'https://json-schema.org/draft/2020-12/schema');
});
38 changes: 38 additions & 0 deletions npm/esm.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import test from 'node:test';
import assert from 'node:assert';
import getSchema from '@sourcemeta/std';

test('loads a valid schema', () => {
const schema = getSchema('2020-12/misc/schema-like');
assert.strictEqual(typeof schema, 'object');
assert.strictEqual(schema.$schema, 'https://json-schema.org/draft/2020-12/schema');
assert.strictEqual(schema.title, 'JSON Schema Document');
});

test('returns null for non-existent schema', () => {
const schema = getSchema('nonexistent/schema/path');
assert.strictEqual(schema, null);
});

test('returns null for invalid input types', () => {
assert.strictEqual(getSchema(null), null);
assert.strictEqual(getSchema(undefined), null);
assert.strictEqual(getSchema(123), null);
assert.strictEqual(getSchema({}), null);
});

test('blocks directory traversal attempts', () => {
const schema = getSchema('../package');
assert.strictEqual(schema, null);
});

test('blocks directory traversal with multiple levels', () => {
const schema = getSchema('../../etc/passwd');
assert.strictEqual(schema, null);
});

test('loads schema from nested path', () => {
const schema = getSchema('2020-12/w3c/xmlschema/2001/hex-binary');
assert.strictEqual(typeof schema, 'object');
assert.strictEqual(schema.$schema, 'https://json-schema.org/draft/2020-12/schema');
});
23 changes: 23 additions & 0 deletions npm/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require('fs');
const path = require('path');

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;
}
}

module.exports = getSchema;
26 changes: 26 additions & 0 deletions npm/main.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

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;
}
}

export default getSchema;
53 changes: 53 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "@sourcemeta/std",
"version": "0.3.0",
"description": "A growing collection of hand-crafted high-quality schemas, From RFC-compliant Email Address schemas to ISO-compliant Currency Codes",
"main": "npm/main.js",
"module": "npm/main.mjs",
"exports": {
".": {
"import": "./npm/main.mjs",
"require": "./npm/main.js"
}
},
"license": "UNLICENSED",
"homepage": "https://github.com/sourcemeta/std",
"author": {
"email": "hello@sourcemeta.com",
"name": "Sourcemeta",
"url": "https://www.sourcemeta.com"
},
"funding": "https://github.com/sponsors/sourcemeta",
"keywords": [
"jsonschema",
"json",
"schema",
"json-schema",
"cli",
"$ref",
"dereference",
"reference",
"resolve",
"json-pointer",
"validator",
"validation",
"bundle",
"json-schema-validator",
"json-schema-validation",
"lint",
"format",
"draft",
"library"
],
"bugs": {
"url": "https://github.com/sourcemeta/std/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/sourcemeta/std.git"
},
"publishConfig": {
"provenance": true,
"access": "public"
}
}