@@ -2,19 +2,40 @@ import { AST, EvaluationPlugin } from "@hyperjump/json-schema/experimental";
22import { JsonNode } from "@hyperjump/json-schema/instance/experimental" ;
33import { 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+ */
515export 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+ */
1432export type OutputFormat = OutputUnit & {
1533 valid : boolean ;
1634} ;
1735
36+ /**
37+ * A single node of the JSON Schema output format.
38+ */
1839export 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+ */
3564export type JsonSchemaErrors = ErrorObject [ ] ;
3665
66+ /**
67+ * Represents a single validation error with message and schema location
68+ * information.
69+ */
3770export 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+ */
4481export 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+ */
61117export 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+ */
67126export 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+
71140export 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+ */
73146export 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+ } ;
0 commit comments