@hyperjump/json-schema
Advanced tools
Comparing version 1.5.2 to 1.6.0
@@ -23,3 +23,3 @@ import type { add, Anchors } from "./schema.js"; | ||
export type OutputFormat = "FLAG" | "BASIC" | "DETAILED" | "VERBOSE" | string; | ||
export type OutputFormat = "FLAG" | "BASIC" | "DETAILED" | "VERBOSE"; | ||
@@ -26,0 +26,0 @@ export type OutputUnit = { |
@@ -1,13 +0,13 @@ | ||
import fs from "fs/promises"; | ||
import { createReadStream } from "node:fs"; | ||
import { Readable } from "node:stream"; | ||
import { fileURLToPath } from "node:url"; | ||
import { fetch, Response } from "undici"; | ||
import Url from "url"; | ||
import * as MediaTypes from "./media-types.js"; | ||
export default async (url, options) => { | ||
export default (url, options) => { | ||
if (url.startsWith("file://")) { | ||
const filePath = Url.fileURLToPath(url); | ||
const fd = await fs.open(filePath); | ||
const stream = fd.createReadStream(); | ||
const response = new Response(stream, { | ||
const filePath = fileURLToPath(url); | ||
const stream = createReadStream(filePath); | ||
const response = new Response(Readable.toWeb(stream), { | ||
headers: { "Content-Type": MediaTypes.getContentType(filePath) } | ||
@@ -14,0 +14,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
import { concat, join, empty, map, filter, every, pipe, tap } from "@hyperjump/pact"; | ||
import { concat, join, empty, map, filter, every, pipe } from "@hyperjump/pact"; | ||
import * as Schema from "../schema.js"; | ||
@@ -3,0 +3,0 @@ import * as Instance from "../instance.js"; |
import { nil as nilPointer, append as pointerAppend, get as pointerGet } from "@hyperjump/json-pointer"; | ||
import { toAbsoluteIri } from "@hyperjump/uri"; | ||
import { jsonTypeOf, resolveUri, uriFragment, pathRelative, jsonStringify } from "./common.js"; | ||
import { contextUri } from "./context-uri.js"; | ||
import fetch from "./fetch.js"; | ||
@@ -14,4 +15,2 @@ import { hasDialect, loadDialect, getKeywordName } from "./keywords.js"; | ||
const defaultDialectId = "https://json-schema.org/validation"; | ||
export const add = (schema, retrievalUri = undefined, contextDialectId = undefined) => { | ||
@@ -21,3 +20,6 @@ schema = JSON.parse(JSON.stringify(schema)); | ||
// Dialect / JSON Schema Version | ||
const dialectId = toAbsoluteIri(schema.$schema || contextDialectId || defaultDialectId); | ||
if ((typeof schema !== "object" || !("$schema" in schema)) && !contextDialectId) { | ||
throw Error("Unable to determine a dialect for the schema. The dialect can be declared in a number of ways, but the recommended way is to use the '$schema' keyword in your schema."); | ||
} | ||
const dialectId = toAbsoluteIri(schema.$schema || contextDialectId); | ||
delete schema.$schema; | ||
@@ -164,3 +166,3 @@ | ||
export const get = async (url, contextDoc = nil) => { | ||
const resolvedUrl = resolveUri(url, contextDoc.id); | ||
const resolvedUrl = resolveUri(url, contextDoc.id || contextUri()); | ||
const id = toAbsoluteIri(resolvedUrl); | ||
@@ -179,5 +181,7 @@ const fragment = uriFragment(resolvedUrl); | ||
// Try to determine the dialect from the meta-schema if it isn't already known | ||
const dialectId = toAbsoluteIri(schema.$schema || contextDialectId || defaultDialectId); | ||
if (!hasDialect(dialectId) && !hasStoredSchema(dialectId)) { | ||
await get(dialectId); | ||
if (schema.$schema || contextDialectId) { | ||
const dialectId = toAbsoluteIri(schema.$schema || contextDialectId); | ||
if (!hasDialect(dialectId) && !hasStoredSchema(dialectId)) { | ||
await get(dialectId); | ||
} | ||
} | ||
@@ -279,3 +283,3 @@ | ||
const schema = JSON.parse(jsonStringify(schemaDoc.schema, (key, value, pointer) => { | ||
const schema = JSON.parse(jsonStringify(schemaDoc.schema, (_key, value, pointer) => { | ||
if (Reference.isReference(value)) { | ||
@@ -282,0 +286,0 @@ const refValue = Reference.value(value); |
{ | ||
"name": "@hyperjump/json-schema", | ||
"version": "1.5.2", | ||
"version": "1.6.0", | ||
"description": "A JSON Schema validator with support for custom keywords, vocabularies, and dialects", | ||
@@ -24,3 +24,4 @@ "type": "module", | ||
"browser": { | ||
"./lib/fetch.js": "./lib/fetch.browser.js" | ||
"./lib/fetch.js": "./lib/fetch.browser.js", | ||
"./lib/context-uri.js": "./lib/context-uri.browser.js" | ||
}, | ||
@@ -35,2 +36,4 @@ "scripts": { | ||
"JSON Schema", | ||
"json-schema", | ||
"jsonschema", | ||
"JSON", | ||
@@ -40,2 +43,3 @@ "Schema", | ||
"2019-09", | ||
"draft-07", | ||
"draft-06", | ||
@@ -55,2 +59,3 @@ "draft-04", | ||
"@types/mocha": "*", | ||
"@types/node": "^20.6.2", | ||
"@typescript-eslint/eslint-plugin": "*", | ||
@@ -57,0 +62,0 @@ "@typescript-eslint/parser": "*", |
@@ -21,4 +21,4 @@ # Hyperjump - JSON Schema | ||
## Install | ||
Includes support for node.js (ES Modules, TypeScript) and browsers (works with | ||
CSP | ||
Includes support for node.js/bun.js (ES Modules, TypeScript) and browsers (works | ||
with CSP | ||
[`unsafe-eval`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_eval_expressions)). | ||
@@ -46,2 +46,12 @@ | ||
### TypeScript | ||
This package uses the package.json "exports" field. [TypeScript understands | ||
"exports"](https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#packagejson-exports-imports-and-self-referencing), | ||
but you need to change a couple settings in your `tsconfig.json` for it to work. | ||
```jsonc | ||
"module": "Node16", // or "NodeNext" | ||
"moduleResolution": "Node16", // or "NodeNext" | ||
``` | ||
### Versioning | ||
@@ -103,7 +113,4 @@ The API for this library is divided into two categories: Stable and | ||
You can fetch schemas from the web or from the file system, but when fetching | ||
from the file system, there are limitations for security reasons. If your schema | ||
has an identifier with an http(s) scheme (**https**://example.com), it's not | ||
allowed to reference schemas with a file scheme | ||
(**file**:///path/to/my/schemas). | ||
Schemas that are available on the web can be loaded automatically without | ||
needing to load them manually. | ||
@@ -114,2 +121,8 @@ ```javascript | ||
When running on the server, you can also load schemas directly from the | ||
filesystem using `file:` URIs. When fetching from the file system, there are | ||
limitations for security reasons. If your schema has an identifier with an | ||
http(s) scheme (**https**://example.com), it's not allowed to reference schemas | ||
with a file scheme (**file**:///path/to/my/schemas). | ||
```javascript | ||
@@ -119,2 +132,9 @@ const output = await validate(`file://${__dirname}/string.schema.json`, "foo"); | ||
If the schema URI is relative, the base URI in the browser is the browser | ||
location and the base URI on the server is the current working directory. | ||
```javascript | ||
const output = await validate(`./string.schema.json`, "foo"); | ||
``` | ||
**Media type plugins** | ||
@@ -121,0 +141,0 @@ |
@@ -100,3 +100,4 @@ import { defineVocabulary, loadDialect } from "../lib/keywords.js"; | ||
loadDialect("https://json-schema.org/validation", { | ||
const dialectId = "https://json-schema.org/validation"; | ||
loadDialect(dialectId, { | ||
"https://json-schema.org/vocab/core": true, | ||
@@ -111,12 +112,12 @@ "https://json-schema.org/vocab/applicator": true, | ||
addSchema(metaSchema); | ||
addSchema(coreMetaSchema); | ||
addSchema(applicatorMetaSchema); | ||
addSchema(validationMetaSchema); | ||
addSchema(metaDataMetaSchema); | ||
addSchema(formatAnnotationMetaSchema); | ||
addSchema(formatAssertionMetaSchema); | ||
addSchema(contentMetaSchema); | ||
addSchema(unevaluatedMetaSchema); | ||
addSchema(metaSchema, dialectId); | ||
addSchema(coreMetaSchema, "https://json-schema.org/meta/core"); | ||
addSchema(applicatorMetaSchema, "https://json-schema.org/meta/applicator"); | ||
addSchema(validationMetaSchema, "https://json-schema.org/meta/validation"); | ||
addSchema(metaDataMetaSchema, "https://json-schema.org/meta/meta-data"); | ||
addSchema(formatAnnotationMetaSchema, "https://json-schema.org/meta/format-annotation"); | ||
addSchema(formatAssertionMetaSchema, "https://json-schema.org/meta/format-assertion"); | ||
addSchema(contentMetaSchema, "https://json-schema.org/meta/content"); | ||
addSchema(unevaluatedMetaSchema, "https://json-schema.org/meta/unevaluated"); | ||
export * from "../lib/index.js"; |
export default { | ||
"$id": "https://json-schema.org/meta/applicator", | ||
"$schema": "https://json-schema.org/validation", | ||
"title": "Applicator vocabulary meta-schema", | ||
@@ -4,0 +4,0 @@ |
export default { | ||
"$id": "https://json-schema.org/meta/content", | ||
"$schema": "https://json-schema.org/validation", | ||
"title": "Content vocabulary meta-schema", | ||
@@ -4,0 +4,0 @@ |
export default { | ||
"$id": "https://json-schema.org/meta/core", | ||
"$schema": "https://json-schema.org/validation", | ||
"title": "Core vocabulary meta-schema", | ||
@@ -4,0 +4,0 @@ |
export default { | ||
"$id": "https://json-schema.org/meta/format-annotation", | ||
"$schema": "https://json-schema.org/validation", | ||
"title": "Format vocabulary meta-schema for annotation results", | ||
@@ -4,0 +4,0 @@ |
export default { | ||
"$id": "https://json-schema.org/meta/format-assertion", | ||
"$schema": "https://json-schema.org/validation", | ||
"title": "Format vocabulary meta-schema for assertion results", | ||
@@ -4,0 +4,0 @@ |
export default { | ||
"$id": "https://json-schema.org/meta/meta-data", | ||
"$schema": "https://json-schema.org/validation", | ||
"title": "Meta-data vocabulary meta-schema", | ||
@@ -4,0 +4,0 @@ |
export default { | ||
"$id": "https://json-schema.org/meta/unevaluated", | ||
"$schema": "https://json-schema.org/validation", | ||
"title": "Unevaluated applicator vocabulary meta-schema", | ||
@@ -4,0 +4,0 @@ |
export default { | ||
"$id": "https://json-schema.org/meta/validation", | ||
"$schema": "https://json-schema.org/validation", | ||
"title": "Validation vocabulary meta-schema", | ||
@@ -4,0 +4,0 @@ |
export default { | ||
"$id": "https://json-schema.org/validation", | ||
"$schema": "https://json-schema.org/validation", | ||
"$vocabulary": { | ||
@@ -4,0 +4,0 @@ "https://json-schema.org/vocab/core": true, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
356901
182
10415
804
1
15