Skip to content

Commit 2493dd5

Browse files
committed
Fixes and documentation for types
1 parent f81635b commit 2493dd5

5 files changed

Lines changed: 94 additions & 20 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"lint": "oxlint",
3333
"test": "vitest run",
3434
"type-check": "tsc --noEmit",
35-
"docs": "typedoc --excludeExternals"
35+
"docs": "typedoc --excludeExternals src/index.d.ts"
3636
},
3737
"dependencies": {
3838
"@fluent/bundle": "^0.19.1",

src/error-handlers/contains.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import * as Schema from "@hyperjump/browser";
33
import * as Instance from "@hyperjump/json-schema/instance/experimental";
44

55
/**
6-
* @import { ContainsRange } from "../localization.js"
7-
* @import { ErrorHandler, ErrorObject } from "../index.d.ts"
6+
* @import { ContainsRange, ErrorHandler, ErrorObject } from "../index.d.ts"
87
*/
98

109
/** @type ErrorHandler */

src/index.d.ts

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,40 @@ import { AST, EvaluationPlugin } from "@hyperjump/json-schema/experimental";
22
import { JsonNode } from "@hyperjump/json-schema/instance/experimental";
33
import { Localization } from "./localization.js";
44

5+
/**
6+
* Converts standard JSON Schema validation output into human-oriented, localized
7+
* messages. Schemas need to be registered with `@hyperjump/json-schema`'s
8+
* `registerSchema` function. The default locale is `en-US`.
9+
*
10+
* @param errorOutput - The validation output in standard JSON Schema output format.
11+
* @param schemaUri - The URI of the JSON Schema the data was validated against
12+
* @param instance - The JSON data that was validated
13+
* @param options - Options to configure the error handler (default locale is "en-US")
14+
*/
515
export const jsonSchemaErrors: (
616
errorOutput: OutputFormat,
717
schemaUri: string,
818
instance: Json,
9-
options?: Options
19+
options?: JsonSchemaErrorsOptions
1020
) => Promise<JsonSchemaErrors>;
1121

12-
export const setNormalizationHandler: (uri: string, handler: NormalizationHandler) => void;
22+
/**
23+
* Sets a normalization handler for a specific keyword URI. Normalization handlers
24+
* process keyword values during schema validation to produce normalized output.
25+
*/
26+
export const setNormalizationHandler: (keywordUri: string, handler: NormalizationHandler) => void;
1327

28+
/**
29+
* The standard JSON Schema output format. Supports the "basic", "detailed", and
30+
* "verbose" formats.
31+
*/
1432
export type OutputFormat = OutputUnit & {
1533
valid: boolean;
1634
};
1735

36+
/**
37+
* A single node of the JSON Schema output format.
38+
*/
1839
export type OutputUnit = {
1940
valid?: boolean;
2041
absoluteKeywordLocation?: string;
@@ -28,21 +49,51 @@ export type JsonObject = {
2849
[property: string]: Json;
2950
};
3051

31-
export type Options = {
32-
language?: string;
52+
export type JsonSchemaErrorsOptions = {
53+
/**
54+
* A locale identifier in the form of "{language}-{region}".
55+
*
56+
* @example "en-US"
57+
*/
58+
locale?: string;
3359
};
3460

61+
/**
62+
* An array of error objects representing validation failures.
63+
*/
3564
export type JsonSchemaErrors = ErrorObject[];
3665

66+
/**
67+
* Represents a single validation error with message and schema location
68+
* information.
69+
*/
3770
export type ErrorObject = {
3871
message: string;
3972
alternatives?: ErrorObject[][];
4073
instanceLocation: string;
4174
schemaLocations: string[];
4275
};
4376

77+
/**
78+
* Used to convert a specific keyword to the normalized format used by the error
79+
* handlers.
80+
*/
4481
export type NormalizationHandler<KeywordValue = unknown, Context extends EvaluationContext = EvaluationContext> = {
82+
/**
83+
* For non-applicator keywords, this doesn't need to do anything. Just return void.
84+
*
85+
* For applicator keywords, it should call `evaluateSchema` on each subschema and
86+
* return an array with each result.
87+
*/
4588
evaluate(value: KeywordValue, instance: JsonNode, context: Context): NormalizedOutput[] | void;
89+
90+
/**
91+
* Simple applicators just apply subschemas and don't have any validation behavior
92+
* of their own. For example, `allOf` and `properties` are simple applicators. They
93+
* never fail. Only their subschema can fail. `anyOf` and `oneOf` are not simple
94+
* applicators because they can fail independently of the validation result of
95+
* their subschemas.
96+
*/
4697
simpleApplicator?: true;
4798
};
4899

@@ -58,20 +109,51 @@ export type ErrorIndex = {
58109
};
59110
};
60111

112+
/**
113+
* The normalized keyword result keyed by keyword URI and keyword location. If the
114+
* keyword is an applicator the values can be `false` or `NormalizedOutput[]`. If
115+
* the value is not an applicator, the value is just a boolean.
116+
*/
61117
export type InstanceOutput = {
62118
[keywordUri: string]: {
63119
[keywordLocation: string]: boolean | NormalizedOutput[];
64120
};
65121
};
66122

123+
/**
124+
* A map of an instance location to the normalized keyword result for that location.
125+
*/
67126
export type NormalizedOutput = {
68127
[instanceLocation: string]: InstanceOutput;
69128
};
70129

130+
/**
131+
* Builds the normalized output format for a schema. It's used in normalization
132+
* handlers to evaluate an applicator's subschemas.
133+
*
134+
* @param schemaLocation - A URI with a JSON Pointer fragment
135+
* @param instance
136+
* @param context
137+
*/
138+
export const evaluateSchema: (schemaLocation: string, instance: JsonNode, context: EvaluationContext) => NormalizedOutput;
139+
71140
export const addErrorHandler: (handler: ErrorHandler) => void;
72141

142+
/**
143+
* A function that transforms normalized errors for one or more keywords into human
144+
* readable messages.
145+
*/
73146
export type ErrorHandler = (normalizedErrors: InstanceOutput, instance: JsonNode, localization: Localization) => Promise<ErrorObject[]>;
74147

75-
export const evaluateSchema: (schemaLocation: string, instance: JsonNode, context: EvaluationContext) => NormalizedOutput;
148+
/**
149+
* Converts the normalized error format to human readable errors. It's used to
150+
* build errors in applicator error handlers.
151+
*/
152+
export const getErrors: (normalizedErrors: NormalizedOutput, instance: JsonNode, localization: Localization) => Promise<ErrorObject[]>;
153+
154+
export type { Localization };
76155

77-
export const getErrors: (normalizedErrors: NormalizedOutput, rootInstance: JsonNode, language: Localization) => Promise<ErrorObject[]>;
156+
export type ContainsRange = {
157+
minContains?: number;
158+
maxContains?: number;
159+
};

src/json-schema-errors.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ import { Localization } from "./localization.js";
1616
export const jsonSchemaErrors = async (errorOutput, schemaUri, instance, options = {}) => {
1717
const normalizedErrors = await normalizedOutput(instance, errorOutput, schemaUri);
1818
const rootInstance = Instance.fromJs(instance);
19-
const localization = await Localization.forLocale(options.language ?? "en-US");
19+
const localization = await Localization.forLocale(options.locale ?? "en-US");
2020
return await getErrors(normalizedErrors, rootInstance, localization);
2121
};
2222

2323
/** @type Record<string, API.NormalizationHandler> */
2424
const normalizationHandlers = {};
2525

2626
/** @type API.setNormalizationHandler */
27-
export const setNormalizationHandler = (uri, handler) => {
28-
normalizationHandlers[uri] = handler;
27+
export const setNormalizationHandler = (schemaUri, handler) => {
28+
normalizationHandlers[schemaUri] = handler;
2929
};
3030

3131
/** @type (instance: API.Json, errorOutput: API.OutputUnit, subjectUri: string) => Promise<API.NormalizedOutput> */

src/localization.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,7 @@ import { FluentBundle, FluentResource } from "@fluent/bundle";
33

44
/**
55
* @import { FluentVariable} from "@fluent/bundle"
6-
* @import { Json } from "./index.js"
7-
*/
8-
9-
/**
10-
* @typedef {{
11-
* minContains?: number;
12-
* maxContains?: number;
13-
* }} ContainsRange
6+
* @import { ContainsRange, Json } from "./index.d.ts"
147
*/
158

169
export class Localization {

0 commit comments

Comments
 (0)