|
1 | | -import { AST, CompiledSchema, EvaluationPlugin } from "@hyperjump/json-schema/experimental"; |
| 1 | +import { AST, EvaluationPlugin } from "@hyperjump/json-schema/experimental"; |
2 | 2 | import { JsonNode } from "@hyperjump/json-schema/instance/experimental"; |
3 | 3 | import { Localization } from "./localization.js"; |
4 | 4 |
|
@@ -89,25 +89,15 @@ export type NormalizationHandler<KeywordValue = unknown, Context extends Evaluat |
89 | 89 | evaluate(value: KeywordValue, instance: JsonNode, context: Context): NormalizedOutput[] | void; |
90 | 90 |
|
91 | 91 | /** |
92 | | - * Simple applicators just apply subschemas and don't have any validation behavior |
93 | | - * of their own. For example, `allOf` and `properties` are simple applicators. They |
94 | | - * never fail. Only their subschema can fail. `anyOf` and `oneOf` are not simple |
95 | | - * applicators because they can fail independently of the validation result of |
96 | | - * their subschemas. |
97 | | - * |
98 | | - * The results of a simple applicator's subschemas are merged into the results |
99 | | - * of its parent schema. Its own result is recorded too so it can be described, |
100 | | - * and like `validityFromSubschemas`, it fails if any of its subschemas fail. |
101 | | - */ |
102 | | - simpleApplicator?: true; |
103 | | - |
104 | | - /** |
105 | | - * Some applicators, like `then` and `else`, only fail when their subschema |
106 | | - * fails. Validators often only report the subschema's errors and not the |
107 | | - * keyword itself, so this tells us to use the subschema's results when the |
108 | | - * validator's output doesn't include the keyword. |
| 92 | + * Conditional applicators, like `then` and `dependentSchemas`, are simple |
| 93 | + * applicators whose subschemas only apply when a condition holds. Whether a |
| 94 | + * keyword is a simple applicator comes from its `@hyperjump/json-schema` |
| 95 | + * keyword definition. The results of a simple applicator's subschemas are |
| 96 | + * merged into the results of its parent schema, but the merged results of a |
| 97 | + * conditional applicator are left out when describing the parent schema |
| 98 | + * because its error handler describes them along with the condition. |
109 | 99 | */ |
110 | | - validityFromSubschemas?: true; |
| 100 | + conditional?: true; |
111 | 101 |
|
112 | 102 | /** |
113 | 103 | * Annotations, such as `title` and `description`, never affect validation. A |
@@ -224,39 +214,33 @@ export type ContainsRange = { |
224 | 214 | }; |
225 | 215 |
|
226 | 216 | /** |
227 | | - * Validate an instance against a schema and get error messages in one step instead |
228 | | - * of getting output from validation and passing it to jsonSchemaErrors. The |
229 | | - * function is curried so you can compile the schema one time and evaluate multiple |
230 | | - * instances against the same compiled schema. |
| 217 | + * An output format for `@hyperjump/json-schema` that returns human readable error |
| 218 | + * messages. Importing this package registers it. |
231 | 219 | * |
232 | | - * Ideally, this function should be in @hyperjump/json-schema instead and this will |
233 | | - * be removed in the future. |
234 | | - * |
235 | | - * @deprecated |
| 220 | + * @example |
| 221 | + * const output = await validate(schemaUri, instance, { outputFormat: JSE, locale: "en-US" }); |
236 | 222 | */ |
237 | | -export const validate: ( |
238 | | - (schemaUri: string) => Promise<EvaluateInstance> |
239 | | -) & ( |
240 | | - (schemaUri: string, instance: Json, options?: ValidationOptions) => Promise<ValidationResult> |
241 | | -); |
242 | | - |
243 | | -export const evaluateCompiledSchema: (compiledSchema: CompiledSchema, instance: Json, options?: ValidationOptions) => ValidationResult; |
244 | | - |
245 | | -export type EvaluateInstance = (instance: Json, options?: ValidationOptions) => ValidationResult; |
246 | | - |
247 | | -export type ValidationOptions = { |
248 | | - /** |
249 | | - * A locale identifier in the form of "{language}-{region}". |
250 | | - * |
251 | | - * @example "en-US" |
252 | | - */ |
253 | | - locale?: string; |
254 | | - plugins?: EvaluationPlugin[]; |
255 | | -}; |
| 223 | +export const JSE: "JSE"; |
256 | 224 |
|
257 | | -export type ValidationResult = { |
| 225 | +export type JSEOutput = { |
258 | 226 | valid: true; |
259 | 227 | } | { |
260 | 228 | valid: false; |
261 | 229 | errors: JsonSchemaErrors; |
262 | 230 | }; |
| 231 | + |
| 232 | +declare module "@hyperjump/json-schema" { |
| 233 | + interface OutputFormats { |
| 234 | + JSE: JSEOutput; |
| 235 | + } |
| 236 | + |
| 237 | + interface ValidationOptions<F extends OutputFormat = OutputFormat> { |
| 238 | + /** |
| 239 | + * A locale identifier in the form of "{language}-{region}" used for the |
| 240 | + * messages of the JSE output format. |
| 241 | + * |
| 242 | + * @example "en-US" |
| 243 | + */ |
| 244 | + locale?: string; |
| 245 | + } |
| 246 | +} |
0 commit comments