-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdc59e2
commit a0e27ec
Showing
446 changed files
with
5,974 additions
and
26,033 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,23 @@ | ||
import { subscribe, unsubscribe } from "../lib/pubsub.js"; | ||
import { AnnotatedJsInstance } from "./annotated-instance.js"; | ||
import { ValidationError } from "./validation-error.js"; | ||
import { getSchema, getKeyword, compile, interpret as validate, BASIC } from "../lib/experimental.js"; | ||
import { getSchema, compile, interpret as validate, BASIC } from "../lib/experimental.js"; | ||
import { jsonNodeFromJs } from "../lib/json-node.js"; | ||
|
||
|
||
export const annotate = async (schemaUri, json = undefined, outputFormat = undefined) => { | ||
const schema = await getSchema(schemaUri); | ||
const compiled = await compile(schema); | ||
const interpretAst = (json, outputFormat) => interpret(compiled, new AnnotatedJsInstance(json), outputFormat); | ||
const interpretAst = (json, outputFormat) => interpret(compiled, jsonNodeFromJs(json), outputFormat); | ||
|
||
return json === undefined ? interpretAst : interpretAst(json, outputFormat); | ||
}; | ||
|
||
export const interpret = ({ ast, schemaUri }, instance, outputFormat = BASIC) => { | ||
loadKeywordSupport(); | ||
|
||
const output = [instance]; | ||
const subscriptionToken = subscribe("result", outputHandler(output)); | ||
|
||
try { | ||
const result = validate({ ast, schemaUri }, instance, outputFormat); | ||
if (!result.valid) { | ||
throw new ValidationError(result); | ||
} | ||
} finally { | ||
unsubscribe("result", subscriptionToken); | ||
} | ||
|
||
return output[0]; | ||
}; | ||
|
||
const outputHandler = (output) => { | ||
let isPassing = true; | ||
const instanceStack = []; | ||
|
||
return (message, resultNode) => { | ||
if (message === "result.start") { | ||
instanceStack.push(output[0]); | ||
isPassing = true; | ||
} else if (message === "result" && isPassing) { | ||
output[0] = output[0].get(resultNode.instanceLocation); | ||
|
||
if (resultNode.valid) { | ||
const keywordHandler = getKeyword(resultNode.keyword); | ||
if (keywordHandler?.annotation) { | ||
const annotation = keywordHandler.annotation(resultNode.ast); | ||
output[0] = output[0].annotate(resultNode.keyword, annotation); | ||
} | ||
} else { | ||
output[0] = instanceStack[instanceStack.length - 1]; | ||
isPassing = false; | ||
} | ||
} else if (message === "result.end") { | ||
instanceStack.pop(); | ||
} | ||
}; | ||
}; | ||
|
||
const identity = (a) => a; | ||
|
||
const loadKeywordSupport = () => { | ||
const title = getKeyword("https://json-schema.org/keyword/title"); | ||
if (title) { | ||
title.annotation = title.annotation ?? identity; | ||
} | ||
|
||
const description = getKeyword("https://json-schema.org/keyword/description"); | ||
if (description) { | ||
description.annotation = description.annotation ?? identity; | ||
} | ||
|
||
const _default = getKeyword("https://json-schema.org/keyword/default"); | ||
if (_default) { | ||
_default.annotation = _default.annotation ?? identity; | ||
} | ||
|
||
const deprecated = getKeyword("https://json-schema.org/keyword/deprecated"); | ||
if (deprecated) { | ||
deprecated.annotation = deprecated.annotation ?? identity; | ||
} | ||
|
||
const readOnly = getKeyword("https://json-schema.org/keyword/readOnly"); | ||
if (readOnly) { | ||
readOnly.annotation = readOnly.annotation ?? identity; | ||
} | ||
|
||
const writeOnly = getKeyword("https://json-schema.org/keyword/writeOnly"); | ||
if (writeOnly) { | ||
writeOnly.annotation = writeOnly.annotation ?? identity; | ||
} | ||
|
||
const examples = getKeyword("https://json-schema.org/keyword/examples"); | ||
if (examples) { | ||
examples.annotation = examples.annotation ?? identity; | ||
} | ||
|
||
const format = getKeyword("https://json-schema.org/keyword/format"); | ||
if (format) { | ||
format.annotation = format.annotation ?? identity; | ||
} | ||
|
||
const contentMediaType = getKeyword("https://json-schema.org/keyword/contentMediaType"); | ||
if (contentMediaType) { | ||
contentMediaType.annotation = contentMediaType.annotation ?? identity; | ||
} | ||
|
||
const contentEncoding = getKeyword("https://json-schema.org/keyword/contentEncoding"); | ||
if (contentEncoding) { | ||
contentEncoding.annotation = contentEncoding.annotation ?? identity; | ||
} | ||
|
||
const contentSchema = getKeyword("https://json-schema.org/keyword/contentSchema"); | ||
if (contentSchema) { | ||
contentSchema.annotation = contentSchema.annotation ?? identity; | ||
const result = validate({ ast, schemaUri }, instance, outputFormat); | ||
if (!result.valid) { | ||
throw new ValidationError(result); | ||
} | ||
|
||
const unknown = getKeyword("https://json-schema.org/keyword/unknown"); | ||
if (unknown) { | ||
unknown.annotation = unknown.annotation ?? identity; | ||
} | ||
return instance; | ||
}; | ||
|
||
export { ValidationError } from "./validation-error.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,9 @@ | ||
{ | ||
"keyword": "https://json-schema.org/evaluation/validate", | ||
"absoluteKeywordLocation": "https://bundler.hyperjump.io/main#", | ||
"instanceLocation": "#", | ||
"valid": false, | ||
"errors": [ | ||
{ | ||
"keyword": "https://json-schema.org/keyword/type", | ||
"absoluteKeywordLocation": "https://bundler.hyperjump.io/main#/type", | ||
"instanceLocation": "#", | ||
"valid": false, | ||
"errors": [] | ||
"#": { | ||
"errors": { | ||
"https://bundler.hyperjump.io/main#/type": "https://json-schema.org/keyword/type", | ||
"https://bundler.hyperjump.io/main#": "https://json-schema.org/evaluation/validate" | ||
}, | ||
{ | ||
"keyword": "https://json-schema.org/keyword/definitions", | ||
"absoluteKeywordLocation": "https://bundler.hyperjump.io/main#/$defs", | ||
"instanceLocation": "#", | ||
"valid": true, | ||
"errors": [] | ||
} | ||
] | ||
"annotations": {} | ||
} | ||
} |
Oops, something went wrong.